Skip to content

Commit

Permalink
[Fix #10106] Fix Style/RedundantSelf for pattern matching.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvandersluis authored and bbatsov committed Sep 27, 2021
1 parent 9815b39 commit dd2a0b7
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions 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][])
10 changes: 10 additions & 0 deletions lib/rubocop/cop/style/redundant_self.rb
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down
99 changes: 99 additions & 0 deletions spec/rubocop/cop/style/redundant_self_spec.rb
Expand Up @@ -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

0 comments on commit dd2a0b7

Please sign in to comment.