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

auto-correct Style/InverseMethods and Style/SymbolProc together inverts meaning of code #7236

Closed
manlon opened this issue Jul 26, 2019 · 2 comments
Labels
bug stale Issues that haven't been active in a while

Comments

@manlon
Copy link

manlon commented Jul 26, 2019

Code like

[:a, false, :b, false].reject { |x| !x }

violates Style/InverseMethods and Style/SymbolProc. Fixing one or the other should be enough but when --auto-correct'd we get

[:a, false, :b, false].select(&:!)

which has the opposite meaning. I realize Style/SymbolProc is not auto-correct-safe but here the correction would be safe except for interfering with Style/InverseMethods (and this can be seen with rubocop --safe --auto-correct).


Expected behavior

After applying the auto-correction for Style/InverseMethods there should be no violation for Style/SymbolProc so it should not apply.

Actual behavior

Style/SymbolProc correction was applied changing the meaning of the code.

Steps to reproduce the problem

  • have a file test.rb:
puts([:a, false, :b, false].reject { |x| !x })
  • rubocop --auto-correct test.rb

test.rb:3:6: C: [Corrected] Style/InverseMethods: Use select instead of inverting reject.
puts([:a, false, :b, false].reject { |x| !x })
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test.rb:3:36: C: [Corrected] Style/SymbolProc: Pass &:! as an argument to reject instead of a block.
puts([:a, false, :b, false].reject { |x| !x })
                                   ^^^^^^^^^^
  • ruby test.rb now produces different output

RuboCop version

0.73.0 (using Parser 2.6.3.0, running on ruby 2.6.0 x86_64-darwin18)

@Drenmi Drenmi added the bug label Jul 28, 2019
@Drenmi
Copy link
Collaborator

Drenmi commented Jul 30, 2019

Thanks for your report @manlon! 🙏

I have made a PR that marks the auto-correct of Style/InverseMethods as incompatible with that of Style/SymbolProc, so in the case they both want to correct, the former should back off.

Drenmi added a commit to Drenmi/rubocop that referenced this issue Sep 9, 2019
…ods and Style/SymbolProc

These two cops had an interaction between their auto-corrections which produced
code that wasn't semantically equivalent to the original.

This change fixes that by marking `Style/InverseMethods` auto-correct as
incompatible with that of `Style/SymbolProc`.
@stale
Copy link

stale bot commented Jan 26, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

@stale stale bot added the stale Issues that haven't been active in a while label Jan 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug stale Issues that haven't been active in a while
Projects
None yet
Development

No branches or pull requests

2 participants