Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Style/NonNilCheck to check unless #3419

Closed
madwort opened this issue Aug 17, 2016 · 1 comment
Closed

Feature request: Style/NonNilCheck to check unless #3419

madwort opened this issue Aug 17, 2016 · 1 comment
Labels

Comments

@madwort
Copy link
Contributor

madwort commented Aug 17, 2016


Expected behavior

$ cat test.rb 
puts 'OMG' if !myvar.nil?
puts 'WTF' unless myvar.nil?

$ rubocop test.rb
Inspecting 1 file
C

Offenses:

test.rb:1:1: C: Favor unless over if for negative conditions.
puts 'OMG' if !myvar.nil?
^^^^^^^^^^^^^^^^^^^^^^^^^
test.rb:1:15: C: Explicit non-nil checks are usually redundant.
puts 'OMG' if !myvar.nil?
              ^^^^^^^^^^^
test.rb:2:15: C: Explicit non-nil checks are usually redundant.
puts 'WTF' unless myvar.nil?
           ^^^^^^^^^^^^^^^^^

1 file inspected, 3 offenses detected

Actual behavior

$ cat test.rb 
puts 'OMG' if !myvar.nil?
puts 'WTF' unless myvar.nil?

$ rubocop test.rb
Inspecting 1 file
C

Offenses:

test.rb:1:1: C: Favor unless over if for negative conditions.
puts 'OMG' if !myvar.nil?
^^^^^^^^^^^^^^^^^^^^^^^^^
test.rb:1:15: C: Explicit non-nil checks are usually redundant.
puts 'OMG' if !myvar.nil?
              ^^^^^^^^^^^

1 file inspected, 2 offenses detected

RuboCop version

$ bundle exec rubocop -V
0.42.0 (using Parser 2.3.1.2, running on ruby 2.2.2 x86_64-linux)
@bbatsov bbatsov added the bug label Aug 29, 2016
jonas054 added a commit to jonas054/rubocop that referenced this issue Oct 2, 2016
This reporting depends on IncludeSemanticChanges being true,
which it's not by default. Auto-correct to `if x`.

Expressions such as `unless x.nil? && y.nil?` are still not reported.
@bbatsov bbatsov closed this as completed in fdcfc8b Oct 2, 2016
@madwort
Copy link
Contributor Author

madwort commented Oct 2, 2016

Awesome! Thanks @jonas054 !

Neodelf pushed a commit to Neodelf/rubocop that referenced this issue Oct 15, 2016
This reporting depends on IncludeSemanticChanges being true,
which it's not by default. Auto-correct to `if x`.

Expressions such as `unless x.nil? && y.nil?` are still not reported.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants