Skip to content

Commit

Permalink
[Fix #11485] Fix a false positive for Lint/UselessAssignment
Browse files Browse the repository at this point in the history
Fixes #11485.

This PR fixes a false positive for `Lint/UselessAssignment`
when using numbered block parameter.
  • Loading branch information
koic committed Jan 24, 2023
1 parent a6620d5 commit 80e98bb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11485](https://github.com/rubocop/rubocop/issues/11485): Fix a false positive for `Lint/UselessAssignment` when using numbered block parameter. ([@koic][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/variable_force/variable_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ def find_variable(name)
scope_stack.reverse_each do |scope|
variable = scope.variables[name]
return variable if variable

# Only block scope allows referencing outer scope variables.
return nil unless scope.node.block_type?
node = scope.node
return nil unless node.block_type? || node.numblock_type?
end

nil
Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/lint/useless_assignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,16 @@ def some_method(name: value, **)
end
end

context 'using numbered block parameter', :ruby27 do
it 'does not register an offense when the variable is used' do
expect_no_offenses(<<~RUBY)
var = 42
do_something { _1 == var }
RUBY
end
end

# regression test, from problem in Locatable
context 'when a variable is assigned in 2 identical if branches' do
it "doesn't think 1 of the 2 assignments is useless" do
Expand Down

0 comments on commit 80e98bb

Please sign in to comment.