diff --git a/CHANGELOG.md b/CHANGELOG.md index e6c93bc1157..dddf3382155 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * [#7274](https://github.com/rubocop-hq/rubocop/issues/7274): Add new `Lint/SendWithMixinArgument` cop. ([@koic][]) * [#7272](https://github.com/rubocop-hq/rubocop/pull/7272): Show warning message if passed string to `Enabled`, `Safe`, `SafeAutocorrect`, and `AutoCorrect` keys in .rubocop.yml. ([@unasuke][]) * [#7295](https://github.com/rubocop-hq/rubocop/pull/7295): Make it possible to set `StyleGuideBaseURL` per department. ([@koic][]) +* [#7052](https://github.com/rubocop-hq/rubocop/issues/7052): Make `Lint/HandleExceptions` register offense for rescue nil. ([@tejasbubane][]) ### Bug fixes diff --git a/lib/rubocop/cop/lint/handle_exceptions.rb b/lib/rubocop/cop/lint/handle_exceptions.rb index c313be13c81..6da0e479573 100644 --- a/lib/rubocop/cop/lint/handle_exceptions.rb +++ b/lib/rubocop/cop/lint/handle_exceptions.rb @@ -78,7 +78,7 @@ class HandleExceptions < Cop MSG = 'Do not suppress exceptions.' def on_resbody(node) - return if node.body + return if node.body && !node.body.nil_type? return if cop_config['AllowComments'] && comment_lines?(node) add_offense(node) diff --git a/spec/rubocop/cop/lint/handle_exceptions_spec.rb b/spec/rubocop/cop/lint/handle_exceptions_spec.rb index 031ca471e97..d12a3aaa4cf 100644 --- a/spec/rubocop/cop/lint/handle_exceptions_spec.rb +++ b/spec/rubocop/cop/lint/handle_exceptions_spec.rb @@ -14,6 +14,24 @@ RUBY end + it 'registers an offense for rescue nil' do + expect_offense(<<~RUBY) + begin + something + rescue + ^^^^^^ Do not suppress exceptions. + nil + end + RUBY + end + + it 'registers an offense for inline nil rescue' do + expect_offense(<<~RUBY) + something rescue nil + ^^^^^^^^^^ Do not suppress exceptions. + RUBY + end + it 'does not register an offense for rescue with body' do expect_no_offenses(<<~RUBY) begin