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

Fix a false positive for Rails/RedundantActiveRecordAllMethod #1126

Conversation

koic
Copy link
Member

@koic koic commented Sep 19, 2023

Resolves #1114 (comment)

This PR fixes a false positive for RedundantActiveRecordAllMethod when using find with block argument.

e.g. Enumerable#find and ActiveRecord::Base#find have different arguments, So false positives can be prevented.


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.

@koic koic force-pushed the fix_a_false_positive_for_rails_redundant_active_record_all_method branch 2 times, most recently from 60ccbef to 343f8fc Compare September 19, 2023 03:52
@koic koic changed the title Fix a false positive for RedundantActiveRecordAllMethod Fix a false positive for Rails/RedundantActiveRecordAllMethod Sep 19, 2023
@koic koic force-pushed the fix_a_false_positive_for_rails_redundant_active_record_all_method branch from 343f8fc to c09deca Compare September 19, 2023 04:10
@masato-bkn
Copy link
Contributor

masato-bkn commented Sep 19, 2023

May I ask a question? It seems that ActiveRecord::Base#find can also take a block argument. In the context of this cop, has it been determined that it's better to ignore find that takes a block argument?

@koic
Copy link
Member Author

koic commented Sep 19, 2023

Are there any specific use cases where false negatives are an issue?

@masato-bkn
Copy link
Contributor

I don't currently have a specific use case in mind, and I just wanted to inquire if this change aligns with my current understanding 😅

@koic koic force-pushed the fix_a_false_positive_for_rails_redundant_active_record_all_method branch 2 times, most recently from 0db2236 to 93b2574 Compare September 20, 2023 05:19
@@ -127,12 +127,15 @@ class RedundantActiveRecordAllMethod < Base
without
].to_set.freeze

POSSIBLE_ENUMERABLE_BLOCK_METHODS = %i[any? find select].freeze
Copy link
Contributor

@masato-bkn masato-bkn Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the following methods could also be added to POSSIBLE_ENUMERABLE_BLOCK_METHODS for the same reason. What do you think?

  • none?
  • one?
  • sum
  • count

@koic koic force-pushed the fix_a_false_positive_for_rails_redundant_active_record_all_method branch from 93b2574 to 049a40e Compare September 25, 2023 03:05
Resolves rubocop#1114 (comment)

This PR fixes a false positive for `Rails/RedundantActiveRecordAllMethod`
when using some `Enumerable`'s methods with block argument.

e.g. `Enumerable#find` and `ActiveRecord::Base#find` have different arguments,
So false positives can be prevented.
@koic koic force-pushed the fix_a_false_positive_for_rails_redundant_active_record_all_method branch from 049a40e to e586f7f Compare September 28, 2023 16:44
@koic koic merged commit e1b1fff into rubocop:master Sep 28, 2023
10 checks passed
@koic koic deleted the fix_a_false_positive_for_rails_redundant_active_record_all_method branch September 28, 2023 16:45
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.

None yet

2 participants