Skip to content

Commit

Permalink
[Fix #9387] Fix incorrect auto-correct for Style/NilComparison
Browse files Browse the repository at this point in the history
Fixes #9387.

This PR fixes incorrect auto-correct for `Style/NilComparison` when using
`!x.nil?` and `EnforcedStyle: comparison`.
  • Loading branch information
koic authored and bbatsov committed Jan 18, 2021
1 parent 7bf9d46 commit c570f55
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9387](https://github.com/rubocop-hq/rubocop/issues/9387): Fix incorrect auto-correct for `Style/NilComparison` when using `!x.nil?` and `EnforcedStyle: comparison`. ([@koic][])
1 change: 1 addition & 0 deletions lib/rubocop/cop/style/nil_comparison.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def on_send(node)
end

corrector.replace(node, new_code)
corrector.wrap(node, '(', ')') if node.parent&.method?(:!)
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/nil_comparison_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,16 @@
x == nil
RUBY
end

it 'registers and corrects an offense for `!x.nil?`' do
expect_offense(<<~RUBY)
!x.nil?
^^^^ Prefer the use of the `==` comparison.
RUBY

expect_correction(<<~RUBY)
!(x == nil)
RUBY
end
end
end

0 comments on commit c570f55

Please sign in to comment.