Skip to content

Commit

Permalink
[Fix #1108] Fix an incorrect autocorrect for Rails/TimeZone
Browse files Browse the repository at this point in the history
Fixes #1108.

This PR fixes an incorrect autocorrect for `Rails/TimeZone`
when using `String#to_time`.
  • Loading branch information
koic committed Sep 11, 2023
1 parent fc9a15a commit ce6056d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1108](https://github.com/rubocop/rubocop-rails/issues/1108): Fix an incorrect autocorrect for `Rails/TimeZone` when using `String#to_time`. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/time_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def on_send(node)
return if !node.receiver&.str_type? || !node.method?(:to_time)

add_offense(node.loc.selector, message: MSG_STRING_TO_TIME) do |corrector|
autocorrect(corrector, node)
corrector.replace(node, "Time.zone.parse(#{node.receiver.source})")
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/rubocop/cop/rails/time_zone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@
"2012-03-02 16:05:37".to_time
^^^^^^^ Do not use `String#to_time` without zone. Use `Time.zone.parse` instead.
RUBY

expect_correction(<<~RUBY)
Time.zone.parse("2012-03-02 16:05:37")
RUBY
end

it 'does not register an offense for `to_time` without receiver' do
Expand Down

0 comments on commit ce6056d

Please sign in to comment.