Skip to content

Commit adec01a

Browse files
committed
[Fix #1016] Add RedundantActiveRecordAllMethod cop
1 parent dd17d2d commit adec01a

File tree

5 files changed

+475
-0
lines changed

5 files changed

+475
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1016](https://github.com/rubocop/rubocop-rails/issues/1016): Add `Rails/RedundantActiveRecordAllMethod` cop. ([@masato-bkn][])

config/default.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,13 @@ Rails/ReadWriteAttribute:
775775
Include:
776776
- app/models/**/*.rb
777777

778+
Rails/RedundantActiveRecordAllMethod:
779+
Description: Detect redundant `all` used as a receiver for Active Record query methods.
780+
StyleGuide: 'https://rails.rubystyle.guide/#redundant-all'
781+
Enabled: pending
782+
Safe: false
783+
VersionAdded: "<<next>>"
784+
778785
Rails/RedundantAllowNil:
779786
Description: >-
780787
Finds redundant use of `allow_nil` when `allow_blank` is set to
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module Rails
6+
# Detect redundant `all` used as a receiver for Active Record query methods.
7+
#
8+
# @safety
9+
# This cop is unsafe for autocorrection if the receiver for `all` is not an Active Record object.
10+
#
11+
# @example
12+
# # bad
13+
# User.all.find(id)
14+
# User.all.order(:created_at)
15+
# users.all.where(id: ids)
16+
# user.articles.all.order(:created_at)
17+
#
18+
# # good
19+
# User.find(id)
20+
# User.order(:created_at)
21+
# users.where(id: ids)
22+
# user.articles.order(:created_at)
23+
class RedundantActiveRecordAllMethod < Base
24+
include ActiveRecordHelper
25+
extend AutoCorrector
26+
27+
MSG = 'Redundant `all` detected.'
28+
29+
RESTRICT_ON_SEND = [:all].freeze
30+
31+
# Defined methods in `ActiveRecord::Querying::QUERYING_METHODS` on activerecord 7.0.5.
32+
QUERYING_METHODS = %i[
33+
and
34+
annotate
35+
any?
36+
average
37+
calculate
38+
count
39+
create_or_find_by
40+
create_or_find_by!
41+
create_with
42+
delete_all
43+
delete_by
44+
destroy_all
45+
destroy_by
46+
distinct
47+
eager_load
48+
except
49+
excluding
50+
exists?
51+
extending
52+
extract_associated
53+
fifth
54+
fifth!
55+
find
56+
find_by
57+
find_by!
58+
find_each
59+
find_in_batches
60+
find_or_create_by
61+
find_or_create_by!
62+
find_or_initialize_by
63+
find_sole_by
64+
first
65+
first!
66+
first_or_create
67+
first_or_create!
68+
first_or_initialize
69+
forty_two
70+
forty_two!
71+
fourth
72+
fourth!
73+
from
74+
group
75+
having
76+
ids
77+
in_batches
78+
in_order_of
79+
includes
80+
invert_where
81+
joins
82+
last
83+
last!
84+
left_joins
85+
left_outer_joins
86+
limit
87+
lock
88+
many?
89+
maximum
90+
merge
91+
minimum
92+
none
93+
none?
94+
offset
95+
one?
96+
only
97+
optimizer_hints
98+
or
99+
order
100+
pick
101+
pluck
102+
preload
103+
readonly
104+
references
105+
reorder
106+
reselect
107+
rewhere
108+
second
109+
second!
110+
second_to_last
111+
second_to_last!
112+
select
113+
sole
114+
strict_loading
115+
sum
116+
take
117+
take!
118+
third
119+
third!
120+
third_to_last
121+
third_to_last!
122+
touch_all
123+
unscope
124+
update_all
125+
where
126+
without
127+
].freeze
128+
129+
def on_send(node)
130+
query_node = node.parent
131+
132+
return unless query_node&.send_type?
133+
return unless QUERYING_METHODS.include?(query_node.method_name)
134+
return if node.receiver.nil? && !inherit_active_record_base?(node)
135+
136+
range_of_all_method = node.loc.selector
137+
add_offense(range_of_all_method) do |collector|
138+
collector.remove(range_of_all_method)
139+
collector.remove(query_node.loc.dot)
140+
end
141+
end
142+
end
143+
end
144+
end
145+
end

lib/rubocop/cop/rails_cops.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
require_relative 'rails/present'
8888
require_relative 'rails/rake_environment'
8989
require_relative 'rails/read_write_attribute'
90+
require_relative 'rails/redundant_active_record_all_method'
9091
require_relative 'rails/redundant_allow_nil'
9192
require_relative 'rails/redundant_foreign_key'
9293
require_relative 'rails/redundant_presence_validation_on_belongs_to'

0 commit comments

Comments
 (0)