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

Invalid warning / correction about UnusedMethodArgument when using numbered parameters in 1.44 #11494

Closed
PatrickLerner opened this issue Jan 24, 2023 · 1 comment

Comments

@PatrickLerner
Copy link

Given a code such as:

class Foo
  def bar(name)
    foos.find { _1.name == name }
  end
end

In 1.44 I am seeing an error:

patrick@znovu ~/P/I/P/app (dependabot-bundler-ruboc…*) $ bin/rubocop foo.rb
Inspecting 1 file
W

Offenses:

foo.rb:4:11: W: [Correctable] Lint/UnusedMethodArgument: Unused method argument - name. If it's necessary, use _ or _name as an argument name to indicate that it won't be used. If it's unnecessary, remove it. You can also write as bar(*) if you want the method to accept any arguments but don't care about them.
  def bar(name)
          ^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable

while in 1.43 the code was accepted as valid (which it is)

patrick@znovu ~/P/I/P/app (master*) $ bin/rubocop foo.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

Expected behavior

The code is perfectly valid, and the suggested correction is dangerous and will render it non-functioning. I expect no error to be given. The behavior is related to numbered parameters, if the numbered parameter is removed, it works as expected.

Actual behavior

See above.

RuboCop version

$ rubocop -V
1.44.0 (using Parser 3.2.0.0, rubocop-ast 1.24.1, running on ruby 2.7.2) [arm64-darwin21]
  - rubocop-graphql 0.19.0
  - rubocop-rails 2.17.4
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.18.1
@PatrickLerner
Copy link
Author

I am also seeing a likely related issue given a rspec file like this:

# frozen_string_literal: true

shared_examples 'FooBar' do
  foo_values = %w(foo bar)

  it 'contains nested attributes' do
    expect(data[:foos].all? { _1.keys.sort == foo_values }).to be true
  end
end

where rubocop reports:

patrick@znovu ~/P/I/P/app (dependabot-bundler-ruboc…*) $ bin/rubocop foo_spec.rb
Inspecting 1 file
W

Offenses:

foo_spec.rb:4:3: W: Lint/UselessAssignment: Useless assignment to variable - foo_values.
  foo_values = %w(foo bar)
  ^^^^^^^^^^

1 file inspected, 1 offense detected

which likewise is incorrect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant