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

class << self is ignored in Metrics/ModuleLength #12231

Closed
d4rky-pl opened this issue Sep 29, 2023 · 4 comments · Fixed by #12240
Closed

class << self is ignored in Metrics/ModuleLength #12231

d4rky-pl opened this issue Sep 29, 2023 · 4 comments · Fixed by #12240
Labels

Comments

@d4rky-pl
Copy link
Contributor

Expected behavior

The code inside class << self should be counted towards module length

Actual behavior

The contents of the block are ignored and Metrics/ModuleLength passes

Steps to reproduce the problem

# .rubocop.yml
AllCops:
  DisabledByDefault: true

Metrics/ModuleLength:
  Max: 1
# This will fail with C: Metrics/ModuleLength: Module has too many lines. [2/1]
module TestModule
  def a
  end
end
# This will pass even if we put 1000+ more lines in class << self
module TestModule
  class << self
    def a
    end
  end
end

RuboCop version

➜  rubocop-repro rubocop -V
1.56.4 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.1.3) [arm64-darwin22]
@koic
Copy link
Member

koic commented Sep 29, 2023

This is detected by Metrics/ClassLength cop, not Metrics/ModuleLength cop.

@d4rky-pl
Copy link
Contributor Author

@koic I'm not sure if this is correct behaviour, if it is then feel free to close this ticket:

Given this configuration:

AllCops:
  DisabledByDefault: true

Metrics/ModuleLength:
  Max: 1

Metrics/ClassLength:
  Max: 1

For this case:

module TestModule
  class << self
    def a
    end
  end
end

we'll get:

test3.rb:3:3: C: Metrics/ClassLength: Class has too many lines. [2/1]
  class << self ...
  ^^^^^^^^^^^^^

but for this case:

module TestModule
  class << self
    def a
    end
  end
  def a; end
end

we'll get both:

test3.rb:2:1: C: Metrics/ModuleLength: Module has too many lines. [5/1]
module TestModule ...
^^^^^^^^^^^^^^^^^
test3.rb:3:3: C: Metrics/ClassLength: Class has too many lines. [2/1]
  class << self ...
  ^^^^^^^^^^^^^

@d4rky-pl
Copy link
Contributor Author

There was a change in behaviour between 1.49.0 and 1.50.0:

# testfile
module TestModule
  class << self
    def a
    end
  end
end

there is a difference in which cop reacts:

# rubocop 1.49.0

test3.rb:2:1: C: Metrics/ModuleLength: Module has too many lines. [4/1]
module TestModule ...
^^^^^^^^^^^^^^^^^
# rubocop 1.50.0

test3.rb:3:3: C: Metrics/ClassLength: Class has too many lines. [2/1]
  class << self ...
  ^^^^^^^^^^^^^

I assume it's because of either #11773 or #11776 so at this point I'm just not sure if both cops shouldn't react in this case or if the current behaviour is the correct and expected one

@koic
Copy link
Member

koic commented Oct 3, 2023

You're right. This is an unexpected change in behavior. I've opened #12240 to resolve this issue. Thank you!

koic added a commit to koic/rubocop that referenced this issue Oct 3, 2023
Fixes rubocop#12231.

This PR fixes a false negative for `Metrics/ModuleLength`
when defining a singleton class in a module.
koic added a commit that referenced this issue Oct 3, 2023
…module_length

[Fix #12231] Fix a false negative for `Metrics/ModuleLength`
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