Skip to content

Commit

Permalink
[Fix #12082] Fix an error for Lint/UselessAssignment
Browse files Browse the repository at this point in the history
Fixes #12082.

This PR fixes an error for `Lint/UselessAssignment` when a variable is assigned
and unreferenced in `for` with multiple variables.
  • Loading branch information
koic authored and bbatsov committed Jul 31, 2023
1 parent 59e940b commit ee26d06
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_lint_useless_assignment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12082](https://github.com/rubocop/rubocop/issues/12082): Fix an error for `Lint/UselessAssignment` when a variable is assigned and unreferenced in `for` with multiple variables. ([@koic][])
4 changes: 1 addition & 3 deletions lib/rubocop/cop/variable_force/assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ def rest_assignment_node
end

def for_assignment_node
return nil unless node.parent&.for_type?

node.parent
node.ancestors.find(&:for_type?)
end

def find_multiple_assignment_node(grandparent_node)
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/lint/useless_assignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,23 @@ module x::Foo
end
end

context 'when a variable is assigned and unreferenced in `for` with multiple variables' do
it 'registers an offense' do
expect_offense(<<~RUBY)
for i, j in items
^ Useless assignment to variable - `j`.
do_something(i)
end
RUBY

expect_correction(<<~RUBY)
for i, _ in items
do_something(i)
end
RUBY
end
end

context 'when a variable is assigned and referenced in `for`' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit ee26d06

Please sign in to comment.