Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #9257] Fix false positive for Style/SymbolProc when the block uses a variable from outside the block #9259

Merged
merged 1 commit into from
Dec 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) $_))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a double take at $1! 😆

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it’s needed to balance each option in the {} but not actually used for anything (the capture for block is though so it’s needed)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I figured 😆

}
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