Skip to content

Commit

Permalink
[Fix #12005] Fix a false negative for Lint/Debugger
Browse files Browse the repository at this point in the history
Fixes #12005.

This PR fixes a false negative for `Lint/Debugger`
when using debugger method inside lambda.
  • Loading branch information
koic authored and bbatsov committed Jul 1, 2023
1 parent 7e8c47d commit 2a606c4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/fix_a_false_negative_for_lint_debugger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12005](https://github.com/rubocop/rubocop/issues/12005): Fix a false negative for `Lint/Debugger` when using debugger method inside lambda. ([@koic][])
12 changes: 8 additions & 4 deletions lib/rubocop/cop/lint/debugger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ class Debugger < Base
MSG = 'Remove debugger entry point `%<source>s`.'

def on_send(node)
return unless debugger_method?(node)

# Basically, debugger methods are not used as a method argument without arguments.
return if node.arguments.empty? && node.each_ancestor(:send, :csend).any?
return if !debugger_method?(node) || assumed_usage_context?(node)

add_offense(node)
end
Expand All @@ -95,6 +92,13 @@ def debugger_method?(send_node)
debugger_methods.include?(chained_method_name(send_node))
end

def assumed_usage_context?(node)
# Basically, debugger methods are not used as a method argument without arguments.
return false unless node.arguments.empty? && node.each_ancestor(:send, :csend).any?

node.each_ancestor.none?(&:lambda_or_proc?)
end

def chained_method_name(send_node)
chained_method_name = send_node.method_name.to_s
receiver = send_node.receiver
Expand Down
34 changes: 34 additions & 0 deletions spec/rubocop/cop/lint/debugger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,47 @@
RUBY
end

it 'does not register an offense for a `custom_debugger` call when used in assignment' do
expect_no_offenses(<<~RUBY)
x.y = custom_debugger
RUBY
end

it 'registers an offense for a `custom_debugger` call' do
expect_offense(<<~RUBY)
custom_debugger
^^^^^^^^^^^^^^^ Remove debugger entry point `custom_debugger`.
RUBY
end

it 'registers an offense for a `custom_debugger` call when used in lambda literal' do
expect_offense(<<~RUBY)
x.y = -> { custom_debugger }
^^^^^^^^^^^^^^^ Remove debugger entry point `custom_debugger`.
RUBY
end

it 'registers an offense for a `custom_debugger` call when used in `lambda`' do
expect_offense(<<~RUBY)
x.y = lambda { custom_debugger }
^^^^^^^^^^^^^^^ Remove debugger entry point `custom_debugger`.
RUBY
end

it 'registers an offense for a `custom_debugger` call when used in `proc`' do
expect_offense(<<~RUBY)
x.y = proc { custom_debugger }
^^^^^^^^^^^^^^^ Remove debugger entry point `custom_debugger`.
RUBY
end

it 'registers an offense for a `custom_debugger` call when used in `Proc.new`' do
expect_offense(<<~RUBY)
x.y = Proc.new { custom_debugger }
^^^^^^^^^^^^^^^ Remove debugger entry point `custom_debugger`.
RUBY
end

context 'nested custom configurations' do
let(:cop_config) { { 'DebuggerMethods' => { 'Custom' => %w[custom_debugger] } } }

Expand Down

0 comments on commit 2a606c4

Please sign in to comment.