Skip to content

Commit

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

This PR fixes an error for `Lint/ShadowingOuterLocalVariable`
when the same variable name as a block variable is used in return
value assignment of `if`.
  • Loading branch information
koic authored and bbatsov committed Aug 12, 2022
1 parent 71ea79c commit e921db9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
@@ -0,0 +1 @@
* [#10899](https://github.com/rubocop/rubocop/issues/10899): Fix an error for `Lint/ShadowingOuterLocalVariable` when the same variable name as a block variable is used in return value assignment of `if`. ([@koic][])
1 change: 1 addition & 0 deletions lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
Expand Up @@ -69,6 +69,7 @@ def same_conditions_node_different_branch?(variable, outer_local_variable)

outer_local_variable_node =
find_conditional_node_from_ascendant(outer_local_variable.declaration_node)
return true unless outer_local_variable_node

outer_local_variable_node.conditional? && variable_node == outer_local_variable_node
end
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb
Expand Up @@ -251,6 +251,18 @@ def some_method
end
end

context 'when the same variable name as a block variable is used in return value assignment of `if`' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
def some_method
foo = if condition
bar { |foo| baz(foo) }
end
end
RUBY
end
end

context 'when multiple block arguments have same name "_"' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit e921db9

Please sign in to comment.