diff --git a/changelog/fix_an_error_for_lint_shadowing_outer_local_variable.md b/changelog/fix_an_error_for_lint_shadowing_outer_local_variable.md new file mode 100644 index 00000000000..20a8e407165 --- /dev/null +++ b/changelog/fix_an_error_for_lint_shadowing_outer_local_variable.md @@ -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][]) diff --git a/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb b/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb index a381f8e87a1..ff715d30c6e 100644 --- a/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +++ b/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb @@ -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 diff --git a/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb b/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb index 29f557ce6a8..0f1e2b3f408 100644 --- a/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +++ b/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb @@ -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)