diff --git a/CHANGELOG.md b/CHANGELOG.md index ad557c04537..12d8505b111 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * [#8872](https://github.com/rubocop-hq/rubocop/issues/8872): Fix an error for `Metrics/ClassLength` when multiple assignments to constants. ([@koic][]) * [#8871](https://github.com/rubocop-hq/rubocop/issues/8871): Fix a false positive for `Style/RedundantBegin` when using `begin` for method argument or part of conditions. ([@koic][]) * [#8875](https://github.com/rubocop-hq/rubocop/issues/8875): Fix an incorrect auto-correct for `Style/ClassEqualityComparison` when comparing class name. ([@koic][]) +* [#8880](https://github.com/rubocop-hq/rubocop/issues/8880): Fix an error for `Style/ClassLength` when overlapping constant assignments. ([@koic][]) ## 0.93.0 (2020-10-08) diff --git a/lib/rubocop/cop/metrics/class_length.rb b/lib/rubocop/cop/metrics/class_length.rb index 2cf15ee35c4..038b9c0d618 100644 --- a/lib/rubocop/cop/metrics/class_length.rb +++ b/lib/rubocop/cop/metrics/class_length.rb @@ -49,7 +49,9 @@ def on_casgn(node) _scope, _name, block_node = *node end - check_code_length(block_node) if block_node.class_definition? + return unless block_node.respond_to?(:class_definition?) && block_node.class_definition? + + check_code_length(block_node) end private diff --git a/spec/rubocop/cop/metrics/class_length_spec.rb b/spec/rubocop/cop/metrics/class_length_spec.rb index db27acad737..e4351fd7d34 100644 --- a/spec/rubocop/cop/metrics/class_length_spec.rb +++ b/spec/rubocop/cop/metrics/class_length_spec.rb @@ -202,6 +202,14 @@ class Test end end + context 'when overlapping constant assignments' do + it 'registers an offense' do + expect_no_offenses(<<~RUBY) + X = Y = Z = do_something + RUBY + end + end + context 'when inspecting a class defined with Struct.new' do it 'registers an offense' do expect_offense(<<~RUBY)