Skip to content

Commit

Permalink
Merge pull request rubocop#1293 from fatkodima/where-range-unsafe
Browse files Browse the repository at this point in the history
[Fix rubocop#1283] Mark `WhereRange` as unsafe autocorrect
  • Loading branch information
koic authored Jun 10, 2024
2 parents eb89bdd + f262f71 commit 202d8c1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/change_where_range_unsafe_autocorrect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1283](https://github.com/rubocop/rubocop-rails/issues/1283): Mark `WhereRange` as unsafe autocorrect. ([@fatkodima][])
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,7 @@ Rails/WhereRange:
Description: 'Use ranges in `where` instead of manually constructing SQL.'
StyleGuide: 'https://rails.rubystyle.guide/#where-ranges'
Enabled: pending
SafeAutoCorrect: false
VersionAdded: '2.25'

# Accept `redirect_to(...) and return` and similar cases.
Expand Down
8 changes: 8 additions & 0 deletions lib/rubocop/cop/rails/where_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ module Rails
# Identifies places where manually constructed SQL
# in `where` can be replaced with ranges.
#
# @safety
# This cop's autocorrection is unsafe because it can change the query
# by explicitly attaching the column to the wrong table.
# For example, `Booking.joins(:events).where('end_at < ?', Time.current)` will correctly
# implicitly attach the `end_at` column to the `events` table. But when autocorrected to
# `Booking.joins(:events).where(end_at: ...Time.current)`, it will now be incorrectly
# explicitly attached to the `bookings` table.
#
# @example
# # bad
# User.where('age >= ?', 18)
Expand Down

0 comments on commit 202d8c1

Please sign in to comment.