diff --git a/changelog/fix_false_positive_for_style_redundant_self_cop.md b/changelog/fix_false_positive_for_style_redundant_self_cop.md new file mode 100644 index 00000000000..5ca75ff3406 --- /dev/null +++ b/changelog/fix_false_positive_for_style_redundant_self_cop.md @@ -0,0 +1 @@ +* [#9613](https://github.com/rubocop/rubocop/issues/9613): Fix a false positive for `Style/RedundantSelf` when a self receiver on an lvalue of mlhs arguments. ([@koic][]) diff --git a/lib/rubocop/cop/style/redundant_self.rb b/lib/rubocop/cop/style/redundant_self.rb index 02decdee870..2a4df86e899 100644 --- a/lib/rubocop/cop/style/redundant_self.rb +++ b/lib/rubocop/cop/style/redundant_self.rb @@ -144,8 +144,12 @@ def regular_method_call?(node) end def on_argument(node) - name, = *node - @local_variables_scopes[node] << name + if node.mlhs_type? + on_args(node) + else + name, = *node + @local_variables_scopes[node] << name + end end def allow_self(node) diff --git a/spec/rubocop/cop/style/redundant_self_spec.rb b/spec/rubocop/cop/style/redundant_self_spec.rb index 72eaca162d8..045532ee8a2 100644 --- a/spec/rubocop/cop/style/redundant_self_spec.rb +++ b/spec/rubocop/cop/style/redundant_self_spec.rb @@ -234,4 +234,13 @@ def self.requested_specs it 'accepts a self receiver of methods also defined on `Kernel`' do expect_no_offenses('self.open') end + + it 'accepts a self receiver on an lvalue of mlhs arguments' do + expect_no_offenses(<<~RUBY) + def do_something((a, b)) # This method expects Array that has 2 elements as argument. + self.a = a + self.b.some_method_call b + end + RUBY + end end