Skip to content

Commit

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

This PR fixes a false negative for `Lint/Debugger`
when using debugger method inside block.
  • Loading branch information
koic authored and bbatsov committed Oct 5, 2023
1 parent 76c2bc5 commit bd214bd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
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 @@
* [#12244](https://github.com/rubocop/rubocop/issues/12244): Fix a false negative for `Lint/Debugger` when using debugger method inside block. ([@koic][])
11 changes: 10 additions & 1 deletion lib/rubocop/cop/lint/debugger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ def debugger_method?(send_node)
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?
return true if assumed_argument?(node)

node.each_ancestor.none?(&:lambda_or_proc?)
node.each_ancestor.none? do |ancestor|
ancestor.block_type? || ancestor.numblock_type? || ancestor.lambda_or_proc?
end
end

def chained_method_name(send_node)
Expand All @@ -109,6 +112,12 @@ def chained_method_name(send_node)
end
chained_method_name
end

def assumed_argument?(node)
parent = node.parent

parent.call_type? || parent.literal? || parent.pair_type?
end
end
end
end
Expand Down
25 changes: 25 additions & 0 deletions spec/rubocop/cop/lint/debugger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@
RUBY
end

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

it 'registers an offense for a `custom_debugger` call when used in numbered block' do
expect_offense(<<~RUBY)
x.y = do_something do
z(_1)
custom_debugger
^^^^^^^^^^^^^^^ Remove debugger entry point `custom_debugger`.
end
RUBY
end

it 'registers an offense for a `custom_debugger` call when used in lambda literal' do
expect_offense(<<~RUBY)
x.y = -> { custom_debugger }
Expand Down Expand Up @@ -172,6 +189,14 @@
it { expect(do_something(k: p)).to eq bar }
RUBY
end

it 'does not register an offense when `p` is a array argument of method call' do
expect_no_offenses(<<~RUBY)
let(:p) { foo }
it { expect(do_something([k, p])).to eq bar }
RUBY
end
end

context 'byebug' do
Expand Down

0 comments on commit bd214bd

Please sign in to comment.