Skip to content

Commit ee26d06

Browse files
koicbbatsov
authored andcommitted
[Fix #12082] Fix an error for Lint/UselessAssignment
Fixes #12082. This PR fixes an error for `Lint/UselessAssignment` when a variable is assigned and unreferenced in `for` with multiple variables.
1 parent 59e940b commit ee26d06

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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][])

lib/rubocop/cop/variable_force/assignment.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ def rest_assignment_node
119119
end
120120

121121
def for_assignment_node
122-
return nil unless node.parent&.for_type?
123-
124-
node.parent
122+
node.ancestors.find(&:for_type?)
125123
end
126124

127125
def find_multiple_assignment_node(grandparent_node)

spec/rubocop/cop/lint/useless_assignment_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,23 @@ module x::Foo
270270
end
271271
end
272272

273+
context 'when a variable is assigned and unreferenced in `for` with multiple variables' do
274+
it 'registers an offense' do
275+
expect_offense(<<~RUBY)
276+
for i, j in items
277+
^ Useless assignment to variable - `j`.
278+
do_something(i)
279+
end
280+
RUBY
281+
282+
expect_correction(<<~RUBY)
283+
for i, _ in items
284+
do_something(i)
285+
end
286+
RUBY
287+
end
288+
end
289+
273290
context 'when a variable is assigned and referenced in `for`' do
274291
it 'does not register an offense' do
275292
expect_no_offenses(<<~RUBY)

0 commit comments

Comments
 (0)