Skip to content

Commit

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

This PR fixes an error for `Lint/UselessAssignment`
when a variable is assigned and unreferenced in `for`.
  • Loading branch information
koic authored and bbatsov committed Jun 4, 2023
1 parent 0bbe96c commit 728457d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_error_for_lint_useless_assignment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11919](https://github.com/rubocop/rubocop/issues/11919): Fix an error for `Lint/UselessAssignment` when a variable is assigned and unreferenced in `for`. ([@koic][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/lint/useless_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def variable_like_method_invocation?(node)
def autocorrect(corrector, assignment)
if assignment.exception_assignment?
remove_exception_assignment_part(corrector, assignment.node)
elsif assignment.multiple_assignment? || assignment.rest_assignment?
elsif assignment.multiple_assignment? || assignment.rest_assignment? ||
assignment.for_assignment?
rename_variable_with_underscore(corrector, assignment.node)
elsif assignment.operator_assignment?
remove_trailing_character_from_operator(corrector, assignment.node)
Expand Down
15 changes: 14 additions & 1 deletion lib/rubocop/cop/variable_force/assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def rest_assignment?
meta_assignment_node.type == REST_ASSIGNMENT_TYPE
end

def for_assignment?
return false unless meta_assignment_node

meta_assignment_node.for_type?
end

def operator
assignment_node = meta_assignment_node || @node
assignment_node.loc.operator.source
Expand All @@ -78,7 +84,8 @@ def meta_assignment_node
unless instance_variable_defined?(:@meta_assignment_node)
@meta_assignment_node = operator_assignment_node ||
multiple_assignment_node ||
rest_assignment_node
rest_assignment_node ||
for_assignment_node
end

@meta_assignment_node
Expand Down Expand Up @@ -109,6 +116,12 @@ def rest_assignment_node

node.parent
end

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

node.parent
end
end
end
end
Expand Down
25 changes: 25 additions & 0 deletions spec/rubocop/cop/lint/useless_assignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,31 @@ module x::Foo
end
end

context 'when a variable is assigned and unreferenced in `for`' do
it 'registers an offense' do
expect_offense(<<~RUBY)
for item in items
^^^^ Useless assignment to variable - `item`. Did you mean `items`?
end
RUBY

expect_correction(<<~RUBY)
for _ in items
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)
for item in items
do_something(item)
end
RUBY
end
end

context 'when a variable is assigned and unreferenced in top level' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/variable_force/assignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ def some_method
expect(assignment.meta_assignment_node.type).to eq(:splat)
end
end

context 'when it is `for` assignment' do
let(:source) do
<<~RUBY
def some_method
for item in items
end
end
RUBY
end

it 'returns splat node' do
expect(assignment.meta_assignment_node.type).to eq(:for)
end
end
end

describe '#operator' do
Expand Down

0 comments on commit 728457d

Please sign in to comment.