Skip to content

[Fix #1463] Mark Rails/IndexWith as unsafe autocorrect #1470

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/change_mark_rails_index_with_as_unsafe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1463](https://github.com/rubocop/rubocop-rails/pull/1463): Mark `Rails/IndexWith` as unsafe autocorrect. ([@tejasbubane][])
3 changes: 2 additions & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,9 @@ Rails/IndexBy:
Rails/IndexWith:
Description: 'Prefer `index_with` over `each_with_object`, `to_h`, or `map`.'
Enabled: true
SafeAutoCorrect: false
VersionAdded: '2.5'
VersionChanged: '2.8'
VersionChanged: '<<next>>'

Rails/Inquiry:
Description: "Prefer Ruby's comparison operators over Active Support's `Array#inquiry` and `String#inquiry`."
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/rails/index_with.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module Rails
# an enumerable into a hash where the keys are the original elements.
# Rails provides the `index_with` method for this purpose.
#
# This cop is marked as unsafe autocorrection, because `nil.to_h` returns {}
# but `nil.with_index` throws `NoMethodError`. Therefore, autocorrection is not
# compatible if the receiver is nil.
#
# @example
# # bad
# [1, 2, 3].each_with_object({}) { |el, h| h[el] = foo(el) }
Expand Down
Loading