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

Lint/UselessAssignment false positive when variables are used in blocks with numbered parameters #11485

Closed
Fustrate opened this issue Jan 23, 2023 · 0 comments · Fixed by #11488
Labels

Comments

@Fustrate
Copy link

Expected behavior

Variables that are set and then used inside blocks with numbered parameters should not be marked as unused.

It looks like it was caused by b48ebef - I tried the commit before and it passed. This also causes Lint/UnusedMethodArgument and Lint/UnusedBlockArgument warnings.

Actual behavior

For /Users/Fustrate/Development/DummyProject: configuration from /Users/Fustrate/Development/DummyProject/.rubocop.yml
configuration from /Users/Fustrate/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-performance-1.15.2/config/default.yml
configuration from /Users/Fustrate/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-performance-1.15.2/config/default.yml
Default configuration from /Users/Fustrate/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-1.44.0/config/default.yml
configuration from /Users/Fustrate/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.17.4/config/default.yml
configuration from /Users/Fustrate/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.17.4/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/Fustrate/Development/DummyProject/test.rb
W

Offenses:

test.rb:3:1: W: Lint/UselessAssignment: Useless assignment to variable - target.
target = 6
^^^^^^

1 file inspected, 1 offense detected
Finished in 0.47589999999945576 seconds

Steps to reproduce the problem

# rubocop:disable all
# rubocop:enable Lint/UselessAssignment
target = 6

[1, 2, 3].each { puts _1 if _1 * 2 == target }

Changing it to a normal block with named arguments passes:

target = 6

[1, 2, 3].each do |num|
  puts  num if num * 2 == target
end

RuboCop version

$ rubocop -V
1.44.0 (using Parser 3.2.0.0, rubocop-ast 1.24.1, running on ruby 3.2.0) [x86_64-darwin22]
  - rubocop-performance 1.15.2
  - rubocop-rails 2.17.4

Tested with both 1.44 and current master (99c8b17)

@koic koic added the bug label Jan 24, 2023
koic added a commit to koic/rubocop that referenced this issue Jan 24, 2023
Fixes rubocop#11485.

This PR fixes a false positive for `Lint/UselessAssignment`
when using numbered block parameter.
koic added a commit that referenced this issue Jan 24, 2023
…less_assignment

[Fix #11485] Fix a false positive for `Lint/UselessAssignment`
@koic koic pinned this issue Jan 24, 2023
@koic koic unpinned this issue Jan 25, 2023
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