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/NumberedParametersLimit incorrectly(?) counting largest parameter number instead of number of parameters used #11535

Closed
owst opened this issue Feb 3, 2023 · 0 comments · Fixed by #11536
Labels

Comments

@owst
Copy link
Contributor

owst commented Feb 3, 2023

Added in #10111 , Style/NumberedParametersLimit reports that more than one numbered parameter is used, when there is only one parameter used but it is "higher" than _1 e.g. _2.

For example, .group_by { _2 } causes an offense of

Avoid using more than 1 numbered parameter; 2 detected.

(but group_by { _1 } does not)

I suspect that the intention of the cop was to avoid using more than one numbered parameter in a single block (e.g. { _1 + _2 + _3 }), rather than a single reference to a not-first numbered parameter (e.g. { _2 }).


Expected behavior

No offense - a single parameter is used

Actual behavior

An offense was raised.

Steps to reproduce the problem

TargetRubyVersion must be (at least) 2.7. Then the following repro.rb:

grouped = { a: 1, b: 1, c: 2 }.group_by { _2 }

gives:

$ rubocop --config =(echo -e "AllCops:\n  TargetRubyVersion: 2.7") --debug --cache false --only Style/NumberedParametersLimit re
pro.rb
configuration from /tmp/zshJPIBdI
Default configuration from /Users/owenstephens/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rubocop-1.41.0/config/default.yml
Inspecting 1 file
Scanning /private/tmp/repro.rb
C

Offenses:

repro.rb:1:11: C: Style/NumberedParametersLimit: Avoid using more than 1 numbered parameter; 2 detected.
grouped = { a: 1, b: 1, c: 2 }.group_by { _2 }
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

RuboCop version

$ [bundle exec] rubocop -V
1.44.1 (using Parser 3.2.0.0, rubocop-ast 1.24.1, running on ruby 3.1.3) [x86_64-darwin21]
@koic koic added the bug label Feb 3, 2023
koic added a commit to koic/rubocop that referenced this issue Feb 3, 2023
…sLimit`

Fixes rubocop#11535.

This PR fixes a false positive for `Style/NumberedParametersLimit`
when only `_2` or higher numbered parameter is used.
bbatsov pushed a commit that referenced this issue Feb 5, 2023
Fixes #11535.

This PR fixes a false positive for `Style/NumberedParametersLimit`
when only `_2` or higher numbered parameter is used.
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.

2 participants