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

Style/InverseMethods not being safe in auto-correct #6969

Closed
andreasklinger opened this issue Apr 25, 2019 · 3 comments · Fixed by #7016
Closed

Style/InverseMethods not being safe in auto-correct #6969

andreasklinger opened this issue Apr 25, 2019 · 3 comments · Fixed by #7016
Labels

Comments

@andreasklinger
Copy link

Expected behavior

running Style/InverseMethods should consider return arguments of next statements

Actual behavior

it did change it to a result that's not the same

Steps to reproduce the problem

Simplified example:

class TestClass
  def test_method
    [1, 2, 3, 4].select do |number|
      next(false) if number == 4

      !number.odd?
    end
  end
end

print TestClass.new.test_method

gets autocorrected to

class TestClass
  def test_method
    [1, 2, 3, 4].reject do |number|
      next(false) if number == 4

      number.odd?
    end
  end
end

print TestClass.new.test_method

Which is not the same result:

Console:

$ ruby test_class.rb      
[2]                                                                                                                                                                                     
$ rubocop -a test_class.rb
Inspecting 1 file
C

Offenses:

test_class.rb:3:5: C: [Corrected] Style/InverseMethods: Use reject instead of inverting select.
    [1, 2, 3, 4].select do |number| ...
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
$ ruby test_class.rb      
[2, 4]                                                                                                                                                                                  

RuboCop version

$ [bundle exec] rubocop -V
0.67.2 (using Parser 2.6.2.1, running on ruby 2.4.5 x86_64-darwin18)
@Drenmi Drenmi added the bug label Apr 26, 2019
@Drenmi
Copy link
Collaborator

Drenmi commented Apr 26, 2019

Thanks for the report!

It is likely better if we don't register an offense at all if there's a next present.

@andreasklinger
Copy link
Author

@Drenmi i think that's a fair approach 🙌

@dduugg
Copy link
Contributor

dduugg commented May 2, 2019

Hi, I'd be happy to take fixing this, if no one else is on it already.

dduugg added a commit to dduugg/rubocop that referenced this issue May 10, 2019
koic added a commit that referenced this issue May 10, 2019
[Fix #6969] Fix a false positive in `Style/InverseMethods`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants