diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f2ebe12ea3..54c389db526 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * [#8864](https://github.com/rubocop-hq/rubocop/issues/8864): Fix false positive for `Style/RedundantBegin` with a postfix `while` or `until`. ([@dvandersluis][]) * [#8869](https://github.com/rubocop-hq/rubocop/issues/8869): Fix a false positive for `Style/RedundantBegin` when using `begin` for or assignment and method call. ([@koic][]) * [#8862](https://github.com/rubocop-hq/rubocop/issues/8862): Fix an error for `Lint/AmbiguousRegexpLiteral` when using regexp without method calls in nested structure. ([@koic][]) +* [#8872](https://github.com/rubocop-hq/rubocop/issues/8872): Fix an error for `Metrics/ClassLength` when multiple assignments to constants. ([@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 22634b4f36a..2cf15ee35c4 100644 --- a/lib/rubocop/cop/metrics/class_length.rb +++ b/lib/rubocop/cop/metrics/class_length.rb @@ -39,8 +39,12 @@ def on_class(node) end def on_casgn(node) - if node.parent&.assignment? - block_node = node.parent.children[1] + parent = node.parent + + if parent&.assignment? + block_node = parent.children[1] + elsif parent&.parent&.masgn_type? + block_node = parent.parent.children[1] else _scope, _name, block_node = *node end diff --git a/spec/rubocop/cop/metrics/class_length_spec.rb b/spec/rubocop/cop/metrics/class_length_spec.rb index 9ae48f3f5e2..db27acad737 100644 --- a/spec/rubocop/cop/metrics/class_length_spec.rb +++ b/spec/rubocop/cop/metrics/class_length_spec.rb @@ -230,5 +230,20 @@ class Test end RUBY end + + it 'registers an offense when multiple assignments to constants' do + # `Bar` is always nil, but syntax is valid. + expect_offense(<<~RUBY) + Foo, Bar = Struct.new(:foo, :bar) do + ^^^^^^^^^^^^^^^^^^^^^^^^^ Class has too many lines. [6/5] + a = 1 + a = 2 + a = 3 + a = 4 + a = 5 + a = 6 + end + RUBY + end end end