Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/new_class_descendants_cop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1544](https://github.com/rubocop/rubocop-rails/pull/1544): Add new `Rails/ClassDescendants` cop to discourage use of `Class#descendants` due to autoloading issues and non-deterministic behavior with Garbage Collection. ([@alexanderadam][])
8 changes: 8 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@ Rails/BulkChangeTable:
Include:
- db/**/*.rb

Rails/ClassDescendants:
Description: >-
Avoid using `Class#descendants` as it may not include classes that have yet
to be autoloaded and is non-deterministic with regards to Garbage Collection.
Enabled: pending
Severity: warning
VersionAdded: '<<next>>'

Rails/CompactBlank:
Description: 'Checks if collection can be blank-compacted with `compact_blank`.'
Enabled: pending
Expand Down
44 changes: 44 additions & 0 deletions lib/rubocop/cop/rails/class_descendants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Rails
# Checks for the use of `Class#descendants` which has several issues:
#
# 1. It doesn't know about classes that have yet to be autoloaded.
# 2. It's non-deterministic with regards to Garbage Collection of classes.
# If you use `Class.descendants` in tests where there is a pattern to
# dynamically define classes, GC is unpredictable for when those classes
# are cleaned up and removed.
#
#
# @example
# # bad
# User.descendants
# ApplicationRecord.descendants.map(&:name)
#
# # bad
# MyClass.descendants.each do |klass|
# klass.do_something
# end
#
class ClassDescendants < Base
MSG = 'Avoid using `%<method>s` as it may not include classes that have yet to be autoloaded ' \
'and is non-deterministic with regards to Garbage Collection.'

RESTRICT_ON_SEND = %i[descendants].freeze

# @!method descendants_call?(node)
def_node_matcher :descendants_call?, <<~PATTERN
(send _ :descendants ...)
PATTERN

def on_send(node)
return unless descendants_call?(node)

add_offense(node, message: format(MSG, method: node.method_name))
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
require_relative 'rails/belongs_to'
require_relative 'rails/blank'
require_relative 'rails/bulk_change_table'
require_relative 'rails/class_descendants'
require_relative 'rails/compact_blank'
require_relative 'rails/content_tag'
require_relative 'rails/create_table_with_timestamps'
Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/rails/class_descendants_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::ClassDescendants, :config do
it 'registers an offense when using `descendants`' do
expect_offense(<<~RUBY)
User.descendants
^^^^^^^^^^^^^^^^ Avoid using `descendants` as it may not include classes that have yet to be autoloaded and is non-deterministic with regards to Garbage Collection.
RUBY
end
end