Skip to content

Commit

Permalink
[Fix #10489] Fix a false positive for Lint/LambdaWithoutLiteralBlock
Browse files Browse the repository at this point in the history
Fixes #10489.

This PR fixes a false positive for `Lint/LambdaWithoutLiteralBlock`
when using lambda with a symbol proc.
  • Loading branch information
koic authored and bbatsov committed Apr 2, 2022
1 parent 7a192de commit 6ff094b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10489](https://github.com/rubocop/rubocop/issues/10489): Fix a false positive for `Lint/LambdaWithoutLiteralBlock` when using lambda with a symbol proc. ([@koic][])
9 changes: 8 additions & 1 deletion lib/rubocop/cop/lint/lambda_without_literal_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ class LambdaWithoutLiteralBlock < Base
MSG = 'lambda without a literal block is deprecated; use the proc without lambda instead.'
RESTRICT_ON_SEND = %i[lambda].freeze

# @!method lambda_with_symbol_proc?(node)
def_node_matcher :lambda_with_symbol_proc?, <<~PATTERN
(send nil? :lambda (block_pass (sym _)))
PATTERN

def on_send(node)
return if node.parent&.block_type? || !node.first_argument
if node.parent&.block_type? || !node.first_argument || lambda_with_symbol_proc?(node)
return
end

add_offense(node) do |corrector|
corrector.replace(node, node.first_argument.source.delete('&'))
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/lint/lambda_without_literal_block_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@
lambda.call
RUBY
end

it 'does not register an offense when using lambda with a symbol proc' do
expect_no_offenses(<<~RUBY)
lambda(&:do_something)
RUBY
end
end

0 comments on commit 6ff094b

Please sign in to comment.