Skip to content

Commit

Permalink
[Fix rubocop#10010] Fix a false positive for Style/DoubleNegation
Browse files Browse the repository at this point in the history
Fixes rubocop#10010.

This PR fixes a false positive for `Style/DoubleNegation`
when `!!` is used at return location and before `rescue` keyword.
  • Loading branch information
koic committed Aug 15, 2021
1 parent 94609c9 commit c2cd8dd
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_false_positive_for_style_double_negation.md
@@ -0,0 +1 @@
* [#10010](https://github.com/rubocop/rubocop/issues/10010): Fix a false positive for `Style/DoubleNegation` when `!!` is used at return location and before `rescue` keyword. ([@koic][])
13 changes: 12 additions & 1 deletion lib/rubocop/cop/style/double_negation.rb
Expand Up @@ -62,7 +62,7 @@ def allowed_in_returns?(node)
def end_of_method_definition?(node)
return false unless (def_node = find_def_node_from_ascendant(node))

last_child = def_node.child_nodes.last
last_child = find_last_child(def_node.body)

last_child.last_line == node.last_line
end
Expand All @@ -73,6 +73,17 @@ def find_def_node_from_ascendant(node)

find_def_node_from_ascendant(node.parent)
end

def find_last_child(node)
case node.type
when :rescue
find_last_child(node.body)
when :ensure
find_last_child(node.child_nodes.first)
else
node.child_nodes.last
end
end
end
end
end
Expand Down
146 changes: 146 additions & 0 deletions spec/rubocop/cop/style/double_negation_spec.rb
Expand Up @@ -66,6 +66,56 @@ def foo?
end
RUBY
end

it 'does not register an offense for `!!` when return location and using `rescue`' do
expect_no_offenses(<<~RUBY)
def foo?
bar
!!baz.do_something
rescue
qux
end
RUBY
end

it 'does not register an offense for `!!` when return location and using `ensure`' do
expect_no_offenses(<<~RUBY)
def foo?
bar
!!baz.do_something
ensure
qux
end
RUBY
end

it 'does not register an offense for `!!` when return location and using `rescue` and `ensure`' do
expect_no_offenses(<<~RUBY)
def foo?
bar
!!baz.do_something
rescue
qux
ensure
corge
end
RUBY
end

it 'does not register an offense for `!!` when return location and using `rescue`, `else`, and `ensure`' do
expect_no_offenses(<<~RUBY)
def foo?
bar
!!baz.do_something
rescue
qux
else
quux
ensure
corge
end
RUBY
end
end

context 'when `EnforcedStyle: forbidden`' do
Expand Down Expand Up @@ -109,5 +159,101 @@ def foo?
end
RUBY
end

it 'registers an offense for `!!` when return location and using `rescue`' do
expect_offense(<<~RUBY)
def foo?
bar
!!baz.do_something
^ Avoid the use of double negation (`!!`).
rescue
qux
end
RUBY

expect_correction(<<~RUBY)
def foo?
bar
!baz.do_something.nil?
rescue
qux
end
RUBY
end

it 'registers an offense for `!!` when return location and using `ensure`' do
expect_offense(<<~RUBY)
def foo?
bar
!!baz.do_something
^ Avoid the use of double negation (`!!`).
ensure
qux
end
RUBY

expect_correction(<<~RUBY)
def foo?
bar
!baz.do_something.nil?
ensure
qux
end
RUBY
end

it 'registers an offense for `!!` when return location and using `rescue` and `ensure`' do
expect_offense(<<~RUBY)
def foo?
bar
!!baz.do_something
^ Avoid the use of double negation (`!!`).
rescue
baz
ensure
qux
end
RUBY

expect_correction(<<~RUBY)
def foo?
bar
!baz.do_something.nil?
rescue
baz
ensure
qux
end
RUBY
end

it 'registers an offense for `!!` when return location and using `rescue`, `else`, and `ensure`' do
expect_offense(<<~RUBY)
def foo?
bar
!!baz.do_something
^ Avoid the use of double negation (`!!`).
rescue
qux
else
quux
ensure
corge
end
RUBY

expect_correction(<<~RUBY)
def foo?
bar
!baz.do_something.nil?
rescue
qux
else
quux
ensure
corge
end
RUBY
end
end
end

0 comments on commit c2cd8dd

Please sign in to comment.