Skip to content

Commit

Permalink
Fix a false positive for Lint/SuppressedException
Browse files Browse the repository at this point in the history
When there is a rescue with no statement but a comment in a `do` block
with a numbered parameter, it incorrectly registered an offense even if
AllowComments is set true.

Cover the case of `do` block with a numbered parameter to fix the issue.
  • Loading branch information
akihikodaki authored and bbatsov committed Nov 11, 2022
1 parent 40dfd9f commit 8bfc4c5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_fix_a_false_positive_for.md
@@ -0,0 +1 @@
* [#11151](https://github.com/rubocop/rubocop/pull/11151): Fix a false positive for `Lint/SuppressedException`. ([@akihikodaki][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/suppressed_exception.rb
Expand Up @@ -116,7 +116,7 @@ def on_resbody(node)
private

def comment_between_rescue_and_end?(node)
ancestor = node.each_ancestor(:kwbegin, :def, :defs, :block).first
ancestor = node.each_ancestor(:kwbegin, :def, :defs, :block, :numblock).first
return unless ancestor

end_line = ancestor.loc.end.line
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/lint/suppressed_exception_spec.rb
Expand Up @@ -220,6 +220,30 @@ def self.foo
end
end

context 'Ruby 2.7 or higher', :ruby27 do
context 'when empty rescue for `do` block with a numbered parameter' do
it 'registers an offense for empty rescue without comment' do
expect_offense(<<~RUBY)
foo do
_1
rescue
^^^^^^ Do not suppress exceptions.
end
RUBY
end

it 'does not register an offense for empty rescue with comment' do
expect_no_offenses(<<~RUBY)
foo do
_1
rescue
# do nothing
end
RUBY
end
end
end

it 'registers an offense for empty rescue on single line with a comment after it' do
expect_offense(<<~RUBY)
RSpec.describe Dummy do
Expand Down

0 comments on commit 8bfc4c5

Please sign in to comment.