diff --git a/changelog/fix_fix_styleredundantself_for_pattern.md b/changelog/fix_fix_styleredundantself_for_pattern.md new file mode 100644 index 00000000000..957cf9375a2 --- /dev/null +++ b/changelog/fix_fix_styleredundantself_for_pattern.md @@ -0,0 +1 @@ +* [#10106](https://github.com/rubocop/rubocop/issues/10106): Fix `Style/RedundantSelf` for pattern matching. ([@dvandersluis][]) diff --git a/lib/rubocop/cop/style/redundant_self.rb b/lib/rubocop/cop/style/redundant_self.rb index 1539b1673b5..51ea738c4e6 100644 --- a/lib/rubocop/cop/style/redundant_self.rb +++ b/lib/rubocop/cop/style/redundant_self.rb @@ -100,6 +100,10 @@ def on_lvasgn(node) add_lhs_to_local_variables_scopes(rhs, lhs) end + def on_in_pattern(node) + add_match_var_scopes(node) + end + def on_send(node) return unless node.self_receiver? && regular_method_call?(node) return if node.parent&.mlhs_type? @@ -185,6 +189,12 @@ def add_masgn_lhs_variables(rhs, lhs) add_lhs_to_local_variables_scopes(rhs, child.to_a.first) end end + + def add_match_var_scopes(in_pattern_node) + in_pattern_node.each_descendant(:match_var) do |match_var_node| + @local_variables_scopes[in_pattern_node] << match_var_node.children.first + end + end end end end diff --git a/spec/rubocop/cop/style/redundant_self_spec.rb b/spec/rubocop/cop/style/redundant_self_spec.rb index ac89d8e67e9..951143e56ce 100644 --- a/spec/rubocop/cop/style/redundant_self_spec.rb +++ b/spec/rubocop/cop/style/redundant_self_spec.rb @@ -292,4 +292,103 @@ def do_something((a, b)) # This method expects Array that has 2 elements as argu end RUBY end + + context 'with ruby >= 2.7', :ruby27 do + context 'with pattern matching' do + it 'accepts a self receiver on an `match-var`' do + expect_no_offenses(<<~RUBY) + case foo + in Integer => bar + self.bar + bar + end + RUBY + end + + it 'accepts a self receiver on a `hash-pattern`' do + expect_no_offenses(<<~RUBY) + case pattern + in {x: foo} + self.foo + foo + end + RUBY + end + + it 'accepts a self receiver on a `array-pattern`' do + expect_no_offenses(<<~RUBY) + case pattern + in [foo, bar] + self.foo + foo + end + RUBY + end + + it 'accepts a self receiver with a `match-alt`' do + expect_no_offenses(<<~RUBY) + case pattern + in [foo] | { x: bar } + self.foo + self.bar + foo + bar + end + RUBY + end + + it 'accepts a self receiver in a nested pattern`' do + expect_no_offenses(<<~RUBY) + case pattern + in { foo: [bar, baz] } + self.bar + self.baz + end + RUBY + end + + it 'accepts a self receiver in a conditional pattern' do + expect_no_offenses(<<~RUBY) + case pattern + in a, b if b == a * 2 + self.a + b + end + RUBY + end + + it 'accepts a self receiver in a `if-guard`' do + expect_no_offenses(<<~RUBY) + case pattern + in a, b if b == self.a * 2 + a + b + end + RUBY + end + + it 'registers an offense when using a self receiver with a pin' do + expect_offense(<<~RUBY) + foo = 17 + case pattern + in ^foo, *bar + self.foo + self.bar + foo + bar + ^^^^ Redundant `self` detected. + end + RUBY + end + + it 'registers an offense when using self with a different match var' do + expect_offense(<<~RUBY) + case foo + in Integer => bar + self.bar + bar + in Float => baz + self.bar + baz + ^^^^ Redundant `self` detected. + end + RUBY + + expect_correction(<<~RUBY) + case foo + in Integer => bar + self.bar + bar + in Float => baz + bar + baz + end + RUBY + end + end + end end