Skip to content
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

Incorrect auto-correction: Time.new -> Time.zone.new #86

Closed
doloopwhile opened this issue Jul 8, 2019 · 1 comment · Fixed by #87
Closed

Incorrect auto-correction: Time.new -> Time.zone.new #86

doloopwhile opened this issue Jul 8, 2019 · 1 comment · Fixed by #87
Assignees
Labels
bug Something isn't working

Comments

@doloopwhile
Copy link

Rails/TimeZone will substitute Time.new to Time.zone.new with --auto-correct option.

However, Time.zone.new is not a constructor of TimeWithZone.
It should be Time.zone.local.


Expected behavior

Substitutes Time.new with Time.zone.local.

Actual behavior

Substitutes Time.new with Time.zone.new.

Steps to reproduce the problem

$ gem install rubocop -v 0.72.0
$ gem install rubocop-rails -v 2.2.0
$ echo 'require: rubocop-rails' > .rubocop.yml
$ echo 'Time.new(2016, 7, 8, 12, 34, 56)' > a.rb
$ rubocop --only Rails/TimeZone --auto-correct a.rb
Inspecting 1 file
C

Offenses:

a.rb:1:6: C: [Corrected] Rails/TimeZone: Do not use Time.new without zone. Use one of Time.zone.local, Time.current, Time.new.in_time_zone, Time.new.utc, Time.new.getlocal, Time.new.xmlschema, Time.new.iso8601, Time.new.jisx0301, Time.new.rfc3339, Time.new.httpdate, Time.new.to_i, Time.new.to_f instead. (https://github.com/rubocop-hq/rails-style-guide#time, http://danilenko.org/2012/7/6/rails_timezones)
Time.new(2016, 7, 8, 12, 34, 56)
     ^^^

1 file inspected, 1 offense detected, 1 offense corrected
$ cat a.rb
Time.zone.new(2016, 7, 8, 12, 34, 56)

RuboCop version

$ rubocop -V
0.72.0 (using Parser 2.6.3.0, running on ruby 2.6.2 x86_64-darwin17)
@koic koic added the bug Something isn't working label Jul 9, 2019
@koic koic self-assigned this Jul 9, 2019
koic added a commit to koic/rubocop-rails that referenced this issue Jul 12, 2019
Fixes rubocop#86.

This PR fixes an incorrect auto-correct for `Rails/TimeZone` when using `Time.new`.

### Before

```diff
- Time.new
+ Time.zone.new  #=> NoMethodError: undefined method `new'

- Time.new(2019, 7, 7, 10, 15, 25)
+ Time.zone.new(2019, 7, 7, 10, 15, 25) #=> NoMethodError: undefined method `new'
```

### After

```diff
- Time.new
+ Time.zone.now

- Time.new(2019, 7, 7, 10, 15, 25)
+ Time.zone.local(2019, 7, 7, 10, 15, 25)
```
koic added a commit to koic/rubocop-rails that referenced this issue Jul 12, 2019
Fixes rubocop#86.

This PR fixes an incorrect auto-correct for `Rails/TimeZone` when using `Time.new`.

### Before

```diff
- Time.new
+ Time.zone.new  #=> NoMethodError: undefined method `new'

- Time.new(2019, 7, 7, 10, 15, 25)
+ Time.zone.new(2019, 7, 7, 10, 15, 25) #=> NoMethodError: undefined method `new'
```

### After

```diff
- Time.new
+ Time.zone.now

- Time.new(2019, 7, 7, 10, 15, 25)
+ Time.zone.local(2019, 7, 7, 10, 15, 25)
```
@koic koic closed this as completed in #87 Jul 13, 2019
koic added a commit that referenced this issue Jul 13, 2019
…me_zone

[#86] Fix an incorrect auto-correct for `Rails/TimeZone`
@koic
Copy link
Member

koic commented Jul 13, 2019

RuboCop Rails 2.2.1 has been released with a fix for this issue.
https://rubygems.org/gems/rubocop-rails/versions/2.2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants