Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metrics/ClassLength is double reporting on class with singleton class #12291

Closed
pbstriker38 opened this issue Oct 17, 2023 · 2 comments · Fixed by #12292
Closed

Metrics/ClassLength is double reporting on class with singleton class #12291

pbstriker38 opened this issue Oct 17, 2023 · 2 comments · Fixed by #12292
Labels

Comments

@pbstriker38
Copy link

When a class uses class << self to define class methods, rubocop reports an offense on both the class << self and the parent class.


Expected behavior

let(:cop_config) { { 'Max' => 5, 'CountComments' => false } }

it 'rejects a class with more than 5 lines including its singleton class' do
  expect_offense(<<~RUBY)
    class Test
    ^^^^^^^^^^ Class has too many lines. [8/5]
      class << self
        a = 1
        a = 2
        a = 3
        a = 4
        a = 5
        a = 6
      end
    end
  RUBY
end

Actual behavior

let(:cop_config) { { 'Max' => 5, 'CountComments' => false } }

it 'rejects a class with more than 5 lines including its singleton class' do
  expect_offense(<<~RUBY)
    class Test
    ^^^^^^^^^^ Class has too many lines. [8/5]
      class << self
      ^^^^^^^^^^^^^ Class has too many lines. [6/5]
        a = 1
        a = 2
        a = 3
        a = 4
        a = 5
        a = 6
      end
    end
  RUBY
end

RuboCop version

$ bundle exec rubocop -V
1.57.1 (using Parser 3.2.2.4, rubocop-ast 1.29.0, running on ruby 3.2.1) [arm64-darwin21]
  - rubocop-performance 1.19.1
  - rubocop-rspec 2.24.1
@pbstriker38
Copy link
Author

This is where the behavior changed

45174dc

@pbstriker38
Copy link
Author

rubocop v1.56 behaves like this

let(:cop_config) { { 'Max' => 5, 'CountComments' => false } }

it 'rejects a class with more than 5 lines including its singleton class' do
  expect_offense(<<~RUBY)
    class Test
      class << self
      ^^^^^^^^^^^^^ Class has too many lines. [6/5]
        a = 1
        a = 2
        a = 3
        a = 4
        a = 5
        a = 6
      end
    end
  RUBY
end

@koic koic added the bug label Oct 18, 2023
koic added a commit to koic/rubocop that referenced this issue Oct 18, 2023
Fixes rubocop#12291.

This PR fixes a false positive for `Metrics/ClassLength`
when a class with a singleton class definition.
koic added a commit that referenced this issue Oct 19, 2023
…class_length

[Fix #12291] Fix a false positive for `Metrics/ClassLength`
bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this issue Nov 1, 2023
https://build.opensuse.org/request/show/1121572
by user dancermak + dimstar_suse
- updated to version 1.57.2
  ## 1.57.2 (2023-10-26)
  ### Bug fixes
  * [#12274](rubocop/rubocop#12274): Fix a false positive for `Lint/Void` when `each`'s receiver is an object of `Enumerator` to which `filter` has been applied. ([@koic][])
  * [#12291](rubocop/rubocop#12291): Fix a false positive for `Metrics/ClassLength` when a class with a singleton class definition. ([@koic][])
  * [#12293](rubocop/rubocop#12293): Fix a false positive for `Style/RedundantDoubleSplatHashBraces` when using double splat hash braces with `merge` and method chain. ([@koic][])
  * [#12298](rubocop/rubocop#12298): Fix a false positive for `Style/RedundantParentheses` when using a parenthesized hash literal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants