diff --git a/changelog/fix_fix_a_false_positive_for.md b/changelog/fix_fix_a_false_positive_for.md new file mode 100644 index 00000000000..901f21b375e --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/cop/lint/suppressed_exception.rb b/lib/rubocop/cop/lint/suppressed_exception.rb index ebb23cbfe45..d2872d928bb 100644 --- a/lib/rubocop/cop/lint/suppressed_exception.rb +++ b/lib/rubocop/cop/lint/suppressed_exception.rb @@ -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 diff --git a/spec/rubocop/cop/lint/suppressed_exception_spec.rb b/spec/rubocop/cop/lint/suppressed_exception_spec.rb index b4360f54a0f..50e40a2ce9e 100644 --- a/spec/rubocop/cop/lint/suppressed_exception_spec.rb +++ b/spec/rubocop/cop/lint/suppressed_exception_spec.rb @@ -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