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

False positive and bad autocorrect in Style/TrailingUnderscoreVariable #4757

Closed
mvz opened this issue Sep 18, 2017 · 0 comments
Closed

False positive and bad autocorrect in Style/TrailingUnderscoreVariable #4757

mvz opened this issue Sep 18, 2017 · 0 comments
Labels

Comments

@mvz
Copy link
Contributor

mvz commented Sep 18, 2017

For complex destructuring assignments, Style/TrailingUnderscoreVariable gives false positives and performs the wrong autocorrect.

Expected behavior

Given the following code:

a = [1, 2, [3, 4, 5]]
_, b, (_, c,) = a
_, d, (_, e, _) = a

p a, b, c, d, e

I expect RuboCop to:

  • report no offense on the second line
  • report an offense on the third line, suggesting a change to _, d, (_, e,) = a
  • autocorrect the third line to _, d, (_, e,) = a

Actual behavior

RuboCop reports:

foo.rb:2:7: C: Do not use trailing _s in parallel assignment. Prefer _, b, = a.
_, b, (_, c,) = a
      ^^^^^^^^
foo.rb:3:7: C: Do not use trailing _s in parallel assignment. Prefer _, d, = a.
_, d, (_, e, _) = a
      ^^^^^^^^^^

RuboCop autocorrects the code to:

a = [1, 2, [3, 4, 5]]
_, b, = a
_, d, = a

p a, b, c, d, e

Steps to reproduce the problem

Given a file foo.rb with:

a = [1, 2, [3, 4, 5]]
_, b, (_, c,) = a
_, d, (_, e, _) = a

p a, b, c, d, e
  • run rubocop foo.rb to show the false positive
  • run rubocop -a foo.rb to show the broken autocorrect

RuboCop version

Include the output of rubocop -V. Here's an example:

$ rubocop -V
0.50.0 (using Parser 2.4.0.0, running on ruby 2.4.1 x86_64-linux)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants