Skip to content

Conversation

@alexanderadam
Copy link

@alexanderadam alexanderadam commented Oct 21, 2025

A wise man, let's call him Ben, once said:

There are two reasons why this is an unreliable solution:

  • it doesn't know about things that have yet to be autoloaded

  • it's non-deterministic with regards to Garbage Collection of classes. If you use Class.descendants in Test, where there is a pattern to dynamically define classes, GC is unpredictable for when those classes are cleaned up and removed by the GC.

And I totally trust him on that because he's making a… GoodJob 🥁


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.
    ➡️ Add .descendants and .subclasses advice rails-style-guide#373

fixes #1544

@alexanderadam alexanderadam force-pushed the feat/add_Rails/ClassDescendants branch from 1c0e671 to 2e1353e Compare October 21, 2025 17:20
I trust Ben Sheldon regarding that 😉

github/rubocop-github#110
@rafaelfranca
Copy link
Contributor

It is not up to me to say if this should be accepted or not, but Rails provides many sharp knives, this is one of them. Not only #dencendents or #subclasses or really any think that would get inheritance in a Ruby program can see objects that aren't loaded, so I'm not sure this is a valid warning to show users.

Same with Garbage collection. Objects that are GC'ed aren't visible for the Ruby VM anymore.

This is more about knowledge of the Ruby VM than a warning about some specific feature of Ruby or Rails.

@koic
Copy link
Member

koic commented Oct 23, 2025

I agree with @rafaelfranca's opinion. The risks of using #subclasses are already documented as behavior that should be taken into account:
https://docs.ruby-lang.org/en/3.4/Class.html#method-i-subclasses

It might make sense to suggest adding similar information to the #descendants documentation, but that would be up to the Rails core team to decide. Only #descendants (and #subclasses) should not be treated as special cases.

Of course, this represents the stance of RuboCop Rails and does not preclude implementation in a third-party RuboCop gem. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New cop Rails/ClassDescendants that warns developers when they use Class.descendants

3 participants