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

Wrong auto-correct with block by method name replacement #6725

Closed
rssdev10 opened this issue Feb 3, 2019 · 1 comment
Closed

Wrong auto-correct with block by method name replacement #6725

rssdev10 opened this issue Feb 3, 2019 · 1 comment
Labels
enhancement good first issue Easy task, suitable for newcomers to the project

Comments

@rssdev10
Copy link

rssdev10 commented Feb 3, 2019

Wrong replacement of a block with 2 actual arguments by SymbolProc causes an error with wrong number of arguments

slice_when{ |x| x.nil? } => slice_when(&:nil?)
=> nil? ArgumentError (wrong number of arguments (given 1, expected 0))

Expected behavior

slice_when{ |x| x.nil? } is incorrectly used because of two block variable should be specified here instead of one. Good behavior is to give recommendation that other method like slice_after should be used.

Actual behavior

C: Style/SymbolProc: Pass &:nil? as an argument to slice_when instead of a block.
[1, nil, 2, 3, nil, 4, 5, 6].slice_when{|x| x.nil?}.map(&:compact)
                                       ^^^^^^^^^^^^

Auto-correct produces unworkable code:

[1, nil, 2, 3, nil, 4, 5, 6].slice_when(&:nil?).map(&:compact)
#…
#>> ArgumentError (wrong number of arguments (given 1, expected 0))

Steps to reproduce the problem

[1, nil, 2, 3, nil, 4, 5, 6].slice_when{ |x| x.nil? }.map(&:compact)

RuboCop version

rubocop -V
0.61.1 (using Parser 2.5.3.0, running on ruby 2.6.0 x86_64-darwin17)
@Drenmi
Copy link
Collaborator

Drenmi commented Feb 4, 2019

Thank you for the report! 🙇

The root cause of this problem is that the original code does not properly reveal the yielded arguments, e.g.:

slice_when { |x, _| x.nil? }

Because the second argument is implicitly discarded, RuboCop gets the signature wrong. Unfortunately it's not realistic to keep a lookup table of the block arity of all standard library methods. Even if we did, the above would still be a false positive for user implemented methods.

What we can do is mark the auto-correct of Style/SymbolProc as unsafe, which will make it disabled by default.

@Drenmi Drenmi added enhancement good first issue Easy task, suitable for newcomers to the project labels Feb 4, 2019
@koic koic closed this as completed in b85b6d1 Feb 5, 2019
koic added a commit that referenced this issue Feb 5, 2019
[Fix #6725][Fix #6540] Mark Style/SymbolProc as unsafe for auto-correct
@Drenmi Drenmi mentioned this issue Feb 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement good first issue Easy task, suitable for newcomers to the project
Projects
None yet
Development

No branches or pull requests

2 participants