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

False positive when the receiver of all is not an ActiveRecord object #1124

Closed
masato-bkn opened this issue Sep 18, 2023 · 7 comments · Fixed by #1127
Closed

False positive when the receiver of all is not an ActiveRecord object #1124

masato-bkn opened this issue Sep 18, 2023 · 7 comments · Fixed by #1127

Comments

@masato-bkn
Copy link
Contributor

As mentioned in this comment, Rails/RedundantActiveRecordAllMethod produces a false positive when the receiver of all is not an ActiveRecord object.

Expected behavior

The cop should ignore cases where the receiver of all is not an ActiveRecord object.

Actual behavior

ActiveSupport::TimeZone.all.first
                        ^^^
ActionMailer::Preview.all.first
                      ^^^

Steps to reproduce the problem

Run rubocop on the following code:

ActiveSupport::TimeZone.all.first
ActionMailer::Preview.all.first
$ bundle exec rubocop --only Rails/RedundantActiveRecordAllMethod ./app/false-positive.rb
Inspecting 1 file
C

Offenses:

app/false-positive.rb:2:25: C: [Correctable] Rails/RedundantActiveRecordAllMethod: Redundant all detected.
ActiveSupport::TimeZone.all.first
                        ^^^
app/false-positive.rb:3:23: C: [Correctable] Rails/RedundantActiveRecordAllMethod: Redundant all detected.
ActionMailer::Preview.all.first
                      ^^^

RuboCop version

$ bundle ex rubocop -V
1.56.0 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.0.0) [x86_64-darwin18]
  - rubocop-rails 2.21.1

@masato-bkn masato-bkn changed the title False positive when the receiver of all is not an ActiveRecord object. False positive when the receiver of all is not an ActiveRecord object Sep 18, 2023
@fatkodima
Copy link
Contributor

Rubocop performs static analysis of the code (does not run the code), so there is no way to know if the receiver is a ActiveRecord model or not. And better the rubocop:disable used in this case.

@jpcody
Copy link

jpcody commented Sep 18, 2023

Isn't this code, which was introduced here determining whether it's an AR model?

koic added a commit to koic/rubocop-rails that referenced this issue Sep 19, 2023
…rdAllMethod`

Fixes rubocop#1124.

This PR fixes false positives for `Rails/RedundantActiveRecordAllMethod`
when receiver is not an Active Record model.
@jorg-vr
Copy link

jorg-vr commented Sep 19, 2023

We encountered the same issue using Docker::Container.all from https://github.com/upserve/docker-api
If it is not possible to limit the rule to active record models, it should be removed. As there are too many potential false positives. (.all is a quite generic function name)

@koic
Copy link
Member

koic commented Sep 19, 2023

@jorg-vr Since this cop is already marked as unsafe, there will be limited ways to prevent false positives for static analysis. Can you provide a specific code example where false positives occurs with Docker::Container.all?

@koic
Copy link
Member

koic commented Sep 19, 2023

Ah, I saw the backlink:
dodona-edu/dodona#4987

That false positive could be resolved.

@jpcody
Copy link

jpcody commented Sep 19, 2023

On my end, I have two instances: one where I'm iterating over sidekiq queues and another where I'm calling an activehash-backed model. Neither of these inherit from ApplicationRecord or ActiveRecord::Base, so I was surprised to find them called out as positive based on the above linked code and PR.

def queue_too_latent?(queue_name, threshold_seconds)
  queue = Sidekiq::Queue.all.find { |q| q.name == queue_name }
  latency = queue&.latency || 0

  latency > threshold_seconds
end
Hazmat::SystemClassification.all.each do |sc|
  
end

@koic
Copy link
Member

koic commented Sep 20, 2023

I haven't reviewed all cases for ActiveRecord::Querying::QUERYING_METHODS, but such scenarios will likely be resolved in the following PR:
#1126

koic added a commit that referenced this issue Sep 25, 2023
…dant_active_record_dll_method

[Fix #1124] Fix false positives for `Rails/RedundantActiveRecordAllMethod`
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 a pull request may close this issue.

5 participants