Skip to content

Commit c53e103

Browse files
committed
Cut 2.33.0
1 parent b9da6a9 commit c53e103

File tree

7 files changed

+136
-9
lines changed

7 files changed

+136
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
## master (unreleased)
1111

12+
## 2.33.0 (2025-08-10)
13+
1214
### New features
1315

1416
* [#1324](https://github.com/rubocop/rubocop-rails/pull/1324): Add `Rails/FindByOrAssignmentMemoization` cop. ([@r7kamura][])

config/default.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ Rails/FindByOrAssignmentMemoization:
544544
StyleGuide: 'https://rails.rubystyle.guide/#find-by-memoization'
545545
Enabled: pending
546546
Safe: false
547-
VersionAdded: '<<next>>'
547+
VersionAdded: '2.33'
548548

549549
Rails/FindEach:
550550
Description: 'Prefer all.find_each over all.each.'
@@ -663,7 +663,7 @@ Rails/IndexWith:
663663
Enabled: true
664664
SafeAutoCorrect: false
665665
VersionAdded: '2.5'
666-
VersionChanged: '<<next>>'
666+
VersionChanged: '2.33'
667667

668668
Rails/Inquiry:
669669
Description: "Prefer Ruby's comparison operators over Active Support's `Array#inquiry` and `String#inquiry`."
@@ -761,7 +761,7 @@ Rails/OrderArguments:
761761
Description: 'Prefer symbol arguments over strings in `order` method.'
762762
StyleGuide: 'https://rails.rubystyle.guide/#order-arguments'
763763
Enabled: pending
764-
VersionAdded: '<<next>>'
764+
VersionAdded: '2.33'
765765
Safe: false
766766

767767
Rails/OrderById:

docs/antora.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ name: rubocop-rails
22
title: RuboCop Rails
33
# We always provide version without patch here (e.g. 1.1),
44
# as patch versions should not appear in the docs.
5-
version: ~
5+
version: '2.33'
66
nav:
77
- modules/ROOT/nav.adoc

docs/modules/ROOT/pages/cops.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ based on the https://rails.rubystyle.guide/[Rails Style Guide].
6363
* xref:cops_rails.adoc#railsfilepath[Rails/FilePath]
6464
* xref:cops_rails.adoc#railsfindby[Rails/FindBy]
6565
* xref:cops_rails.adoc#railsfindbyid[Rails/FindById]
66+
* xref:cops_rails.adoc#railsfindbyorassignmentmemoization[Rails/FindByOrAssignmentMemoization]
6667
* xref:cops_rails.adoc#railsfindeach[Rails/FindEach]
6768
* xref:cops_rails.adoc#railsfreezetime[Rails/FreezeTime]
6869
* xref:cops_rails.adoc#railshasandbelongstomany[Rails/HasAndBelongsToMany]
@@ -87,6 +88,7 @@ based on the https://rails.rubystyle.guide/[Rails Style Guide].
8788
* xref:cops_rails.adoc#railsmultipleroutepaths[Rails/MultipleRoutePaths]
8889
* xref:cops_rails.adoc#railsnegateinclude[Rails/NegateInclude]
8990
* xref:cops_rails.adoc#railsnotnullcolumn[Rails/NotNullColumn]
91+
* xref:cops_rails.adoc#railsorderarguments[Rails/OrderArguments]
9092
* xref:cops_rails.adoc#railsorderbyid[Rails/OrderById]
9193
* xref:cops_rails.adoc#railsoutput[Rails/Output]
9294
* xref:cops_rails.adoc#railsoutputsafety[Rails/OutputSafety]

docs/modules/ROOT/pages/cops_rails.adoc

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,7 @@ enum :status, { active: 0, archived: 1 }, prefix: true
21242124
| String
21252125
21262126
| Include
2127-
| `+**/app/models/**/*.rb+`
2127+
| `+**/app/models/**/*.rb+`, `+**/lib/**/*.rb+`
21282128
| Array
21292129
|===
21302130
@@ -2608,6 +2608,55 @@ User.find(id)
26082608
26092609
* https://rails.rubystyle.guide/#find
26102610
2611+
[#railsfindbyorassignmentmemoization]
2612+
== Rails/FindByOrAssignmentMemoization
2613+
2614+
|===
2615+
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
2616+
2617+
| Pending
2618+
| No
2619+
| Always (Unsafe)
2620+
| 2.33
2621+
| -
2622+
|===
2623+
2624+
Avoid memoizing `find_by` results with `||=`.
2625+
2626+
It is common to see code that attempts to memoize `find_by` result by `||=`,
2627+
but `find_by` may return `nil`, in which case it is not memoized as intended.
2628+
2629+
[#safety-railsfindbyorassignmentmemoization]
2630+
=== Safety
2631+
2632+
This cop is unsafe because detected `find_by` may not be Active Record's method,
2633+
or the code may have a different purpose than memoization.
2634+
2635+
[#examples-railsfindbyorassignmentmemoization]
2636+
=== Examples
2637+
2638+
[source,ruby]
2639+
----
2640+
# bad
2641+
def current_user
2642+
@current_user ||= User.find_by(id: session[:user_id])
2643+
end
2644+
2645+
# good
2646+
def current_user
2647+
if instance_variable_defined?(:@current_user)
2648+
@current_user
2649+
else
2650+
@current_user = User.find_by(id: session[:user_id])
2651+
end
2652+
end
2653+
----
2654+
2655+
[#references-railsfindbyorassignmentmemoization]
2656+
=== References
2657+
2658+
* https://rails.rubystyle.guide/#find-by-memoization
2659+
26112660
[#railsfindeach]
26122661
== Rails/FindEach
26132662
@@ -3409,16 +3458,23 @@ NOTE: Required Rails version: 6.0
34093458
34103459
| Enabled
34113460
| Yes
3412-
| Always
3461+
| Always (Unsafe)
34133462
| 2.5
3414-
| 2.8
3463+
| 2.33
34153464
|===
34163465
34173466
Looks for uses of `each_with_object({}) { ... }`,
34183467
`map { ... }.to_h`, and `Hash[map { ... }]` that are transforming
34193468
an enumerable into a hash where the keys are the original elements.
34203469
Rails provides the `index_with` method for this purpose.
34213470
3471+
[#safety-railsindexwith]
3472+
=== Safety
3473+
3474+
This cop is marked as unsafe autocorrection, because `nil.to_h` returns {}
3475+
but `nil.with_index` throws `NoMethodError`. Therefore, autocorrection is not
3476+
compatible if the receiver is nil.
3477+
34223478
[#examples-railsindexwith]
34233479
=== Examples
34243480
@@ -4139,6 +4195,45 @@ change_column_null :products, :category_id, false
41394195
| Array
41404196
|===
41414197
4198+
[#railsorderarguments]
4199+
== Rails/OrderArguments
4200+
4201+
|===
4202+
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
4203+
4204+
| Pending
4205+
| No
4206+
| Always (Unsafe)
4207+
| 2.33
4208+
| -
4209+
|===
4210+
4211+
Prefer symbol arguments over strings in `order` method.
4212+
4213+
[#safety-railsorderarguments]
4214+
=== Safety
4215+
4216+
Cop is unsafe because the receiver might not be an Active Record query.
4217+
4218+
[#examples-railsorderarguments]
4219+
=== Examples
4220+
4221+
[source,ruby]
4222+
----
4223+
# bad
4224+
User.order('name')
4225+
User.order('name DESC')
4226+
4227+
# good
4228+
User.order(:name)
4229+
User.order(name: :desc)
4230+
----
4231+
4232+
[#references-railsorderarguments]
4233+
=== References
4234+
4235+
* https://rails.rubystyle.guide/#order-arguments
4236+
41424237
[#railsorderbyid]
41434238
== Rails/OrderById
41444239
@@ -4395,6 +4490,9 @@ users = User.all
43954490
end
43964491
----
43974492
4493+
If a method call has no receiver, like `do_something { users.map { |user| user[:foo] }`,
4494+
it is not considered part of an iteration and will be detected.
4495+
43984496
[#safety-railspluck]
43994497
=== Safety
44004498
@@ -6902,7 +7000,7 @@ Time.at(timestamp).in_time_zone
69027000
=== References
69037001
69047002
* https://rails.rubystyle.guide#time
6905-
* http://danilenko.org/2012/7/6/rails_timezones
7003+
* https://danilenko.org/2012/7/6/rails_timezones
69067004
69077005
[#railstimezoneassignment]
69087006
== Rails/TimeZoneAssignment

lib/rubocop/rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module RuboCop
44
module Rails
55
# This module holds the RuboCop Rails version information.
66
module Version
7-
STRING = '2.32.0'
7+
STRING = '2.33.0'
88

99
def self.document_version
1010
STRING.match('\d+\.\d+').to_s

relnotes/v2.33.0.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
### New features
2+
3+
* [#1324](https://github.com/rubocop/rubocop-rails/pull/1324): Add `Rails/FindByOrAssignmentMemoization` cop. ([@r7kamura][])
4+
* [#1501](https://github.com/rubocop/rubocop-rails/pull/1501): Add new cop `Rails/OrderArguments`. ([@lovro-bikic][])
5+
6+
### Bug fixes
7+
8+
* [#1497](https://github.com/rubocop/rubocop-rails/pull/1497): Fix bugs in `Rails/PluckId` and `Rails/PluckInWhere`. ([@r7kamura][])
9+
* [#1478](https://github.com/rubocop/rubocop-rails/issues/1478): Fix a false negative for `Rails/EnvLocal` when having preceding conditions. ([@fatkodima][])
10+
* [#1505](https://github.com/rubocop/rubocop-rails/issues/1505): Fix false negatives for `Rails/Pluck` when `map` method call is used in a block without a receiver. ([@koic][])
11+
* [#1486](https://github.com/rubocop/rubocop-rails/issues/1486): Fix false positives for `Rails/Output` when `p` method is a DSL. ([@koic][])
12+
* [#1495](https://github.com/rubocop/rubocop-rails/issues/1495): Fix false positives for `Rails/TransactionExitStatement` when `break` is used in loop in transactions. ([@koic][])
13+
* [#1483](https://github.com/rubocop/rubocop-rails/pull/1483): Fix autocorrection error when `Rails/IndexWith` has nested offenses. ([@lovro-bikic][])
14+
15+
### Changes
16+
17+
* [#1500](https://github.com/rubocop/rubocop-rails/pull/1500): Exclude controllers and mailers from `Lint/UselessMethodDefinition`. ([@r7kamura][])
18+
* [#1474](https://github.com/rubocop/rubocop-rails/issues/1474): Make `Rails/EnumSyntax` include the lib directory by default. ([@koic][])
19+
* [#1463](https://github.com/rubocop/rubocop-rails/issues/1463): Mark `Rails/IndexWith` as unsafe autocorrect. ([@tejasbubane][])
20+
21+
[@r7kamura]: https://github.com/r7kamura
22+
[@lovro-bikic]: https://github.com/lovro-bikic
23+
[@fatkodima]: https://github.com/fatkodima
24+
[@koic]: https://github.com/koic
25+
[@tejasbubane]: https://github.com/tejasbubane

0 commit comments

Comments
 (0)