Skip to content

Commit

Permalink
Fix a false positive for Style/DoubleNegation
Browse files Browse the repository at this point in the history
This PR fixes the following false positive for `Style/DoubleNegation`
when inside returned conditional clauses with Ruby 2.7's pattern matching.

The following is a reproduction case.

```ruby
def foo?
  case condition
  in foo
    !!foo
  in bar
    !!bar
  else
    !!baz
  end
end
```

It is expected that an offense will not be registered, but in actually
an offense will be registered.

```console
% bundle exec rubocop --only Style/DoubleNegation
(snip)

Inspecting 2 files
C.

Offenses:

example.rb:4:5: C: [Correctable] Style/DoubleNegation: Avoid the use of double negation (!!).
    !!foo
    ^
example.rb:6:5: C: [Correctable] Style/DoubleNegation: Avoid the use of double negation (!!).
    !!bar
    ^

2 files inspected, 2 offenses detected, 2 offenses auto-correctable
```

This is a follow-up to the same issue as #10474 and requires RuboCop AST 1.7 to fix.
Therefore it bumps RuboCop AST required to use rubocop/rubocop-ast#227
to 1.7 or higher.
  • Loading branch information
koic committed Apr 9, 2022
1 parent 3426137 commit 1ef4d6e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10518](https://github.com/rubocop/rubocop/pull/10518): Fix a false positive for `Style/DoubleNegation` when inside returned conditional clauses with Ruby 2.7's pattern matching. ([@koic][])
2 changes: 1 addition & 1 deletion rubocop.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0')
s.add_runtime_dependency('regexp_parser', '>= 1.8', '< 3.0')
s.add_runtime_dependency('rexml')
s.add_runtime_dependency('rubocop-ast', '>= 1.16.0', '< 2.0')
s.add_runtime_dependency('rubocop-ast', '>= 1.17.0', '< 2.0')
s.add_runtime_dependency('ruby-progressbar', '~> 1.7')
s.add_runtime_dependency('unicode-display_width', '>= 1.4.0', '< 3.0')

Expand Down
75 changes: 75 additions & 0 deletions spec/rubocop/cop/style/double_negation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,48 @@ def foo?
RUBY
end

# rubocop:disable RSpec/RepeatedExampleGroupDescription
context 'Ruby >= 2.7', :ruby27 do
# rubocop:enable RSpec/RepeatedExampleGroupDescription
it 'registers an offense and corrects for `!!` when not return location' \
'and using `case`, `in`, and `else`' do
expect_offense(<<~RUBY)
def foo?
case pattern
in foo
!!foo
^ Avoid the use of double negation (`!!`).
do_something
in bar
!!bar
^ Avoid the use of double negation (`!!`).
do_something
else
!!baz
^ Avoid the use of double negation (`!!`).
do_something
end
end
RUBY

expect_correction(<<~RUBY)
def foo?
case pattern
in foo
!foo.nil?
do_something
in bar
!bar.nil?
do_something
else
!baz.nil?
do_something
end
end
RUBY
end
end

it 'does not register an offense for `!!` when return location' do
expect_no_offenses(<<~RUBY)
def foo?
Expand Down Expand Up @@ -544,6 +586,39 @@ def foo?
end
RUBY
end

# rubocop:disable RSpec/RepeatedExampleGroupDescription
context 'Ruby >= 2.7', :ruby27 do
# rubocop:enable RSpec/RepeatedExampleGroupDescription
it 'does not register an offense for `!!` when return location and using `case`, `in`, and `else`' do
expect_no_offenses(<<~RUBY)
def foo?
case condition
in foo
!!foo
in bar
!!bar
else
!!baz
end
end
def bar?
case condition
in foo
do_something
!!foo
in bar
do_something
!!bar
else
do_something
!!baz
end
end
RUBY
end
end
end

context 'when `EnforcedStyle: forbidden`' do
Expand Down

0 comments on commit 1ef4d6e

Please sign in to comment.