diff --git a/CHANGELOG.md b/CHANGELOG.md index eded999e4e5..0f2048a691e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ * [#8507](https://github.com/rubocop-hq/rubocop/issues/8507): Fix `Style/RescueModifier` to handle parentheses around rescue modifiers. ([@dsavochkin][]) * [#8527](https://github.com/rubocop-hq/rubocop/pull/8527): Prevent an incorrect auto-correction for `Style/CaseEquality` cop when comparing with `===` against a regular expression receiver. ([@koic][]) * [#8524](https://github.com/rubocop-hq/rubocop/issues/8524): Fix `Layout/EmptyLinesAroundClassBody` and `Layout/EmptyLinesAroundModuleBody` to correctly handle an access modifier as a first child. ([@dsavochkin][]) +* [#8518](https://github.com/rubocop-hq/rubocop/issues/8518): Fix `Lint/ConstantResolution` cop reporting offense for `module` and `class` definitions. ([@tejasbubane][]) ### Changes diff --git a/lib/rubocop/cop/lint/constant_resolution.rb b/lib/rubocop/cop/lint/constant_resolution.rb index c7864faf963..df1de0f1627 100644 --- a/lib/rubocop/cop/lint/constant_resolution.rb +++ b/lib/rubocop/cop/lint/constant_resolution.rb @@ -63,7 +63,7 @@ class ConstantResolution < Base PATTERN def on_const(node) - return unless unqualified_const?(node) + return if !unqualified_const?(node) || node.parent&.defined_module add_offense(node) end diff --git a/spec/rubocop/cop/lint/constant_resolution_spec.rb b/spec/rubocop/cop/lint/constant_resolution_spec.rb index 19b170464c9..973267d0590 100644 --- a/spec/rubocop/cop/lint/constant_resolution_spec.rb +++ b/spec/rubocop/cop/lint/constant_resolution_spec.rb @@ -27,6 +27,15 @@ RUBY end + context 'module & class definitions' do + it 'does not register offense' do + expect_no_offenses(<<~RUBY) + module Foo; end + class Bar; end + RUBY + end + end + context 'with Only set' do let(:cop_config) { { 'Only' => ['MY_CONST'] } }