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

[Fix #11485] Fix a false positive for Lint/UselessAssignment #11488

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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