diff --git a/CHANGELOG.md b/CHANGELOG.md index dbe8d6f930e..4dce4c7eca0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### Bug fixes + +* [#7842](https://github.com/rubocop-hq/rubocop/issues/7842): Fix a false positive for `Lint/RaiseException` when raising Exception with explicit namespace. ([@koic][]) + ## 0.81.0 (2020-04-01) ### New features diff --git a/lib/rubocop/cop/lint/raise_exception.rb b/lib/rubocop/cop/lint/raise_exception.rb index fed736c57e8..e49175198d4 100644 --- a/lib/rubocop/cop/lint/raise_exception.rb +++ b/lib/rubocop/cop/lint/raise_exception.rb @@ -16,12 +16,12 @@ class RaiseException < Cop MSG = 'Use `StandardError` over `Exception`.' def_node_matcher :exception?, <<~PATTERN - (send nil? ${:raise :fail} (const _ :Exception) ... ) + (send nil? ${:raise :fail} (const {cbase nil?} :Exception) ... ) PATTERN def_node_matcher :exception_new_with_message?, <<~PATTERN (send nil? ${:raise :fail} - (send (const _ :Exception) :new ... )) + (send (const {cbase nil?} :Exception) :new ... )) PATTERN def on_send(node) diff --git a/spec/rubocop/cop/lint/raise_exception_spec.rb b/spec/rubocop/cop/lint/raise_exception_spec.rb index 4433e55b9d6..5e3e9a310b4 100644 --- a/spec/rubocop/cop/lint/raise_exception_spec.rb +++ b/spec/rubocop/cop/lint/raise_exception_spec.rb @@ -80,4 +80,11 @@ it 'does not register an offense for `fail` without arguments' do expect_no_offenses('fail') end + + it 'does not register an offense when raising Exception with explicit ' \ + 'namespace' do + expect_no_offenses(<<~RUBY) + raise Foo::Exception + RUBY + end end