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

[Fix #10846] Fix a false negative for Style/DoubleNegation when there is a hash or an array at return location of method. #10855

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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