Skip to content

Commit

Permalink
Merge pull request #1115 from koic/fix_an_incorrect_autocorrect_for_r…
Browse files Browse the repository at this point in the history
…ails_time_zone

[Fix #1108] Fix an incorrect autocorrect for `Rails/TimeZone`
  • Loading branch information
koic authored Sep 13, 2023
2 parents a56fb0e + ce6056d commit 8457ca2
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 8457ca2

Please sign in to comment.