Skip to content

Conversation

@r7kamura
Copy link
Contributor

@r7kamura r7kamura commented Jul 8, 2025

I found a bug in the implementation of #in_where?.

In the previous implementation, it would return true not only when the node was included in the arguments of where, but also when the node was the receiver of the where call.

This bug causes false negatives in Rails/PluckId:

$ echo 'Post.pluck(:id).where(id: 1)' | bundle exec rubocop --stdin example.rb --only Rails/PluckId
Inspecting 1 file
.

1 file inspected, no offenses detected

and also causes false positives in Rails/PluckInWhere:

$ echo 'Post.pluck(:id).where(id: 1)' | bundle exec rubocop --stdin example.rb --only Rails/PluckInWhere
Inspecting 1 file
C

Offenses:

example.rb:1:6: C: [Correctable] Rails/PluckInWhere: Use select instead of pluck within where query method.
Post.pluck(:id).where(id: 1)
     ^^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable

As far as I could tell from searching with grep, this method is only used in these two cops.

$ git grep "in_where?"
lib/rubocop/cop/mixin/active_record_helper.rb:      def in_where?(node)
lib/rubocop/cop/rails/pluck_id.rb:          return if !pluck_id_call?(node) || in_where?(node)
lib/rubocop/cop/rails/pluck_in_where.rb:          return unless in_where?(node)

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 merged commit f57c3ba into rubocop:master Jul 9, 2025
16 checks passed
@koic
Copy link
Member

koic commented Jul 9, 2025

Thanks!

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.

2 participants