From e921db90dc4d16ce69d464dae7deb69299aa280c Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 10 Aug 2022 18:48:33 +0900 Subject: [PATCH] [Fix #10899] Fix an error for `Lint/ShadowingOuterLocalVariable` 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`. --- ..._error_for_lint_shadowing_outer_local_variable.md | 1 + .../cop/lint/shadowing_outer_local_variable.rb | 1 + .../cop/lint/shadowing_outer_local_variable_spec.rb | 12 ++++++++++++ 3 files changed, 14 insertions(+) create mode 100644 changelog/fix_an_error_for_lint_shadowing_outer_local_variable.md 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)