Skip to content

Commit

Permalink
[Fix #10846] Fix a false negative for Style/DoubleNegation when the…
Browse files Browse the repository at this point in the history
…re is a hash or an array at return location of method
  • Loading branch information
nobuyo authored and bbatsov committed Aug 5, 2022
1 parent 7593ec8 commit 76c2285
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_fix_a_false_negative_for_double_negation.md
@@ -0,0 +1 @@
* [#10846](https://github.com/rubocop/rubocop/issues/10846): Fix a false negative for `Style/DoubleNegation` when there is a hash or an array at return location of method. ([@nobuyo][])
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/double_negation.rb
Expand Up @@ -93,6 +93,8 @@ def end_of_method_definition?(node)

if conditional_node
double_negative_condition_return_value?(node, last_child, conditional_node)
elsif last_child.pair_type? || last_child.hash_type? || last_child.parent.array_type?
false
else
last_child.last_line <= node.last_line
end
Expand Down
141 changes: 141 additions & 0 deletions spec/rubocop/cop/style/double_negation_spec.rb
Expand Up @@ -291,6 +291,147 @@ def foo?
RUBY
end

it 'registers an offense and corrects for `!!` with multi-line array at return location' do
expect_offense(<<~RUBY)
def foo
[
foo1,
!!bar1,
^ Avoid the use of double negation (`!!`).
!!baz1
^ Avoid the use of double negation (`!!`).
]
end
RUBY

expect_correction(<<~RUBY)
def foo
[
foo1,
!bar1.nil?,
!baz1.nil?
]
end
RUBY
end

it 'registers an offense and corrects for `!!` with single-line array at return location' do
expect_offense(<<~RUBY)
def foo
[foo1, !!bar1, baz1]
^ Avoid the use of double negation (`!!`).
end
RUBY

expect_correction(<<~RUBY)
def foo
[foo1, !bar1.nil?, baz1]
end
RUBY
end

it 'registers an offense and corrects for `!!` with multi-line hash at return location' do
expect_offense(<<~RUBY)
def foo
{
foo: foo1,
bar: !!bar1,
^ Avoid the use of double negation (`!!`).
baz: !!baz1
^ Avoid the use of double negation (`!!`).
}
end
RUBY

expect_correction(<<~RUBY)
def foo
{
foo: foo1,
bar: !bar1.nil?,
baz: !baz1.nil?
}
end
RUBY
end

it 'registers an offense and corrects for `!!` with single-line hash at return location' do
expect_offense(<<~RUBY)
def foo
{ foo: foo1, bar: !!bar1, baz: baz1 }
^ Avoid the use of double negation (`!!`).
end
RUBY

expect_correction(<<~RUBY)
def foo
{ foo: foo1, bar: !bar1.nil?, baz: baz1 }
end
RUBY
end

it 'registers an offense and corrects for `!!` with nested hash at return location' do
expect_offense(<<~RUBY)
def foo
{
foo: foo1,
bar: { baz: !!quux }
^ Avoid the use of double negation (`!!`).
}
end
RUBY

expect_correction(<<~RUBY)
def foo
{
foo: foo1,
bar: { baz: !quux.nil? }
}
end
RUBY
end

it 'registers an offense and corrects for `!!` with nested array at return location' do
expect_offense(<<~RUBY)
def foo
[
foo1,
[baz, !!quux]
^ Avoid the use of double negation (`!!`).
]
end
RUBY

expect_correction(<<~RUBY)
def foo
[
foo1,
[baz, !quux.nil?]
]
end
RUBY
end

it 'registers an offense and corrects for `!!` with complex array at return location' do
expect_offense(<<~RUBY)
def foo
[
foo1,
{ baz: !!quux }
^ Avoid the use of double negation (`!!`).
]
end
RUBY

expect_correction(<<~RUBY)
def foo
[
foo1,
{ baz: !quux.nil? }
]
end
RUBY
end

# rubocop:disable RSpec/RepeatedExampleGroupDescription
context 'Ruby >= 2.7', :ruby27 do
# rubocop:enable RSpec/RepeatedExampleGroupDescription
Expand Down

0 comments on commit 76c2285

Please sign in to comment.