-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd17d2d
commit adec01a
Showing
5 changed files
with
475 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#1016](https://github.com/rubocop/rubocop-rails/issues/1016): Add `Rails/RedundantActiveRecordAllMethod` cop. ([@masato-bkn][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
lib/rubocop/cop/rails/redundant_active_record_all_method.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Detect redundant `all` used as a receiver for Active Record query methods. | ||
# | ||
# @safety | ||
# This cop is unsafe for autocorrection if the receiver for `all` is not an Active Record object. | ||
# | ||
# @example | ||
# # bad | ||
# User.all.find(id) | ||
# User.all.order(:created_at) | ||
# users.all.where(id: ids) | ||
# user.articles.all.order(:created_at) | ||
# | ||
# # good | ||
# User.find(id) | ||
# User.order(:created_at) | ||
# users.where(id: ids) | ||
# user.articles.order(:created_at) | ||
class RedundantActiveRecordAllMethod < Base | ||
include ActiveRecordHelper | ||
extend AutoCorrector | ||
|
||
MSG = 'Redundant `all` detected.' | ||
|
||
RESTRICT_ON_SEND = [:all].freeze | ||
|
||
# Defined methods in `ActiveRecord::Querying::QUERYING_METHODS` on activerecord 7.0.5. | ||
QUERYING_METHODS = %i[ | ||
and | ||
annotate | ||
any? | ||
average | ||
calculate | ||
count | ||
create_or_find_by | ||
create_or_find_by! | ||
create_with | ||
delete_all | ||
delete_by | ||
destroy_all | ||
destroy_by | ||
distinct | ||
eager_load | ||
except | ||
excluding | ||
exists? | ||
extending | ||
extract_associated | ||
fifth | ||
fifth! | ||
find | ||
find_by | ||
find_by! | ||
find_each | ||
find_in_batches | ||
find_or_create_by | ||
find_or_create_by! | ||
find_or_initialize_by | ||
find_sole_by | ||
first | ||
first! | ||
first_or_create | ||
first_or_create! | ||
first_or_initialize | ||
forty_two | ||
forty_two! | ||
fourth | ||
fourth! | ||
from | ||
group | ||
having | ||
ids | ||
in_batches | ||
in_order_of | ||
includes | ||
invert_where | ||
joins | ||
last | ||
last! | ||
left_joins | ||
left_outer_joins | ||
limit | ||
lock | ||
many? | ||
maximum | ||
merge | ||
minimum | ||
none | ||
none? | ||
offset | ||
one? | ||
only | ||
optimizer_hints | ||
or | ||
order | ||
pick | ||
pluck | ||
preload | ||
readonly | ||
references | ||
reorder | ||
reselect | ||
rewhere | ||
second | ||
second! | ||
second_to_last | ||
second_to_last! | ||
select | ||
sole | ||
strict_loading | ||
sum | ||
take | ||
take! | ||
third | ||
third! | ||
third_to_last | ||
third_to_last! | ||
touch_all | ||
unscope | ||
update_all | ||
where | ||
without | ||
].freeze | ||
|
||
def on_send(node) | ||
query_node = node.parent | ||
|
||
return unless query_node&.send_type? | ||
return unless QUERYING_METHODS.include?(query_node.method_name) | ||
return if node.receiver.nil? && !inherit_active_record_base?(node) | ||
|
||
range_of_all_method = node.loc.selector | ||
add_offense(range_of_all_method) do |collector| | ||
collector.remove(range_of_all_method) | ||
collector.remove(query_node.loc.dot) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.