Skip to content

Commit e19f3f1

Browse files
tejasbubanekoic
authored andcommitted
[Fix #1463] Mark Rails/IndexWith as unsafe autocorrect
Fixes #1463 Closes #1470
1 parent 64626d2 commit e19f3f1

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1463](https://github.com/rubocop/rubocop-rails/issues/1463): Mark `Rails/IndexWith` as unsafe autocorrect. ([@tejasbubane][])

config/default.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,9 @@ Rails/IndexBy:
653653
Rails/IndexWith:
654654
Description: 'Prefer `index_with` over `each_with_object`, `to_h`, or `map`.'
655655
Enabled: true
656+
SafeAutoCorrect: false
656657
VersionAdded: '2.5'
657-
VersionChanged: '2.8'
658+
VersionChanged: '<<next>>'
658659

659660
Rails/Inquiry:
660661
Description: "Prefer Ruby's comparison operators over Active Support's `Array#inquiry` and `String#inquiry`."

lib/rubocop/cop/rails/index_with.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ module Rails
88
# an enumerable into a hash where the keys are the original elements.
99
# Rails provides the `index_with` method for this purpose.
1010
#
11+
# @safety
12+
# This cop is marked as unsafe autocorrection, because `nil.to_h` returns {}
13+
# but `nil.with_index` throws `NoMethodError`. Therefore, autocorrection is not
14+
# compatible if the receiver is nil.
15+
#
1116
# @example
1217
# # bad
1318
# [1, 2, 3].each_with_object({}) { |el, h| h[el] = foo(el) }

0 commit comments

Comments
 (0)