Skip to content

Commit

Permalink
[Fix #9257] Fix false positive for Style/SymbolProc when the block …
Browse files Browse the repository at this point in the history
…uses a variable from outside the block.
  • Loading branch information
dvandersluis authored and marcandre committed Dec 19, 2020
1 parent 245ae04 commit 65b3814
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/fix_fix_false_positive_for_stylesymbolproc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9257](https://github.com/rubocop-hq/rubocop/issues/9257): Fix false positive for `Style/SymbolProc` when the block uses a variable from outside the block. ([@dvandersluis][])
9 changes: 5 additions & 4 deletions lib/rubocop/cop/style/symbol_proc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ class SymbolProc < Base
SUPER_TYPES = %i[super zsuper].freeze

def_node_matcher :proc_node?, '(send (const {nil? cbase} :Proc) :new)'
def_node_matcher :symbol_proc_receiver?, '{(send ...) (super ...) zsuper}'
def_node_matcher :symbol_proc?, <<~PATTERN
({block numblock}
${(send ...) (super ...) zsuper}
${(args (arg _)) 1}
(send (lvar _var) $_))
{
(block $#symbol_proc_receiver? $(args (arg _var)) (send (lvar _var) $_))
(numblock $#symbol_proc_receiver? $1 (send (lvar :_1) $_))
}
PATTERN

def self.autocorrect_incompatible_with
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/symbol_proc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@
expect_no_offenses('something { |x,| x.first }')
end

it 'accepts a block with an unused argument with an method call' do
expect_no_offenses('something { |_x| y.call }')
end

it 'accepts a block with an unused argument with an lvar' do
expect_no_offenses(<<~RUBY)
y = Y.new
something { |_x| y.call }
RUBY
end

context 'when the method has arguments' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 65b3814

Please sign in to comment.