From 9d32b6230b5b932d4b7221ce1f31728186734555 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 24 Feb 2024 16:36:38 +0900 Subject: [PATCH] Fix false negative for `Style/RedundantAssignment` This PR fixes false negative for `Style/RedundantAssignment` when using pattern matching. --- ...negative_for_style_redundant_assignment.md | 1 + lib/rubocop/cop/style/redundant_assignment.rb | 12 ++++- .../cop/style/redundant_assignment_spec.rb | 53 +++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_false_negative_for_style_redundant_assignment.md diff --git a/changelog/fix_false_negative_for_style_redundant_assignment.md b/changelog/fix_false_negative_for_style_redundant_assignment.md new file mode 100644 index 000000000000..85a4aaee2773 --- /dev/null +++ b/changelog/fix_false_negative_for_style_redundant_assignment.md @@ -0,0 +1 @@ +* [#12706](https://github.com/rubocop/rubocop/pull/12706): Fix false negative for `Style/RedundantAssignment` when using pattern matching. ([@koic][]) diff --git a/lib/rubocop/cop/style/redundant_assignment.rb b/lib/rubocop/cop/style/redundant_assignment.rb index ba006fead64b..20fc0e13aafa 100644 --- a/lib/rubocop/cop/style/redundant_assignment.rb +++ b/lib/rubocop/cop/style/redundant_assignment.rb @@ -54,12 +54,14 @@ def on_def(node) private + # rubocop:disable Metrics/CyclomaticComplexity def check_branch(node) return unless node case node.type - when :case then check_case_node(node) - when :if then check_if_node(node) + when :case then check_case_node(node) + when :case_match then check_case_match_node(node) + when :if then check_if_node(node) when :rescue, :resbody check_rescue_node(node) when :ensure then check_ensure_node(node) @@ -67,12 +69,18 @@ def check_branch(node) check_begin_node(node) end end + # rubocop:enable Metrics/CyclomaticComplexity def check_case_node(node) node.when_branches.each { |when_node| check_branch(when_node.body) } check_branch(node.else_branch) end + def check_case_match_node(node) + node.in_pattern_branches.each { |in_pattern_node| check_branch(in_pattern_node.body) } + check_branch(node.else_branch) + end + def check_if_node(node) return if node.modifier_form? || node.ternary? diff --git a/spec/rubocop/cop/style/redundant_assignment_spec.rb b/spec/rubocop/cop/style/redundant_assignment_spec.rb index 1bacfa50d310..14c0357d9a7c 100644 --- a/spec/rubocop/cop/style/redundant_assignment_spec.rb +++ b/spec/rubocop/cop/style/redundant_assignment_spec.rb @@ -180,6 +180,59 @@ def func RUBY end + context 'when inside an `in` branch' do + it 'registers an offense and autocorrects' do + expect_offense(<<~RUBY) + def func + some_preceding_statements + case x + in y + res = 1 + ^^^^^^^ Redundant assignment before returning detected. + res + in z + 2 + in q + else + res = 3 + ^^^^^^^ Redundant assignment before returning detected. + res + end + end + RUBY + + expect_correction(<<~RUBY) + def func + some_preceding_statements + case x + in y + 1 + #{trailing_whitespace} + in z + 2 + in q + else + 3 + #{trailing_whitespace} + end + end + RUBY + end + end + + it 'accepts empty `in` nodes' do + expect_no_offenses(<<~RUBY) + def func + case x + in y then 1 + in z # do nothing + else + 3 + end + end + RUBY + end + it 'accepts empty method body' do expect_no_offenses(<<~RUBY) def func