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

Rails/RedundantAllowNil stumbles upon validation #6882

Closed
Medvezo opened this issue Apr 4, 2019 · 11 comments · Fixed by #6886
Closed

Rails/RedundantAllowNil stumbles upon validation #6882

Medvezo opened this issue Apr 4, 2019 · 11 comments · Fixed by #6886
Labels

Comments

@Medvezo
Copy link

Medvezo commented Apr 4, 2019

Actual behavior

Newly added Rails/RedundantAllowNil errors out after encountering validates keyword

5 errors occurred:
An error occurred while Rails/RedundantAllowNil cop was inspecting /home/app/models/user.rb:9:2.
An error occurred while Rails/RedundantAllowNil cop was inspecting /home/app/models/user.rb:10:2.
An error occurred while Rails/RedundantAllowNil cop was inspecting /home/app/models/user.rb:11:2.
An error occurred while Rails/RedundantAllowNil cop was inspecting /home/app/models/user.rb:12:2.
An error occurred while Rails/RedundantAllowNil cop was inspecting /home/app/models/user.rb:13:2.

Steps to reproduce the problem

The file in question app/models/user.rb:

class User < ApplicationRecord
  belongs_to :account, optional: true

  before_save { self.email = email.downcase }

  has_secure_password
  has_secure_token :confirmation_token

  validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }
  validates :email, length: { maximum: 256 }
  validates :email, presence: true
  validates :email, uniqueness: { case_sensitive: false }
  validates :password, length: { minimum: 8 }
end

RuboCop version

$ [bundle exec] rubocop -V
0.67.0 (using Parser 2.6.2.0, running on ruby 2.6.2 x86_64-linux)
@Medvezo
Copy link
Author

Medvezo commented Apr 4, 2019

Identified the culprit after running rubocop -d:

An error occurred while Rails/RedundantAllowNil cop was inspecting /home/app/models/user.rb:10:2.
undefined method `children' for nil:NilClass
.../rubocop-0.67.0/lib/rubocop/cop/rails/redundant_allow_nil.rb:42:in `on_send'

@koic koic added the bug label Apr 4, 2019
koic added a commit to koic/rubocop that referenced this issue Apr 4, 2019
Fixes rubocop#6882.

This PR fixes the following error for `Rails/RedundantAllowNil`
when not using both `allow_nil` and `allow_blank`.

```ruby
% cat app/models/user.rb
class User < ApplicationRecord
  validates :email, presence: true
end
```

```console
% rubocop app/models/user.rb --only Rails/RedundantAllowNil -d
For /private/tmp/6882: configuration from
/Users/koic/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.67.1/config/default.yml
Inspecting 1 file
Scanning /private/tmp/6882/app/models/user.rb
An error occurred while Rails/RedundantAllowNil cop was inspecting
/private/tmp/6882/app/models/user.rb:2:2.
undefined method `children' for nil:NilClass
/Users/koic/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.67.1/lib/rubocop/cop/rails/redundant_allow_nil.rb:42:in `on_send'

(snip)

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Rails/RedundantAllowNil cop was inspecting
/private/tmp/6882/app/models/user.rb:2:2.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues

Mention the following information in the issue report:
0.67.1 (using Parser 2.6.2.0, running on ruby 2.6.1 x86_64-darwin17)
```
@koic
Copy link
Member

koic commented Apr 4, 2019

Thanks for the feedback. I opened a PR #6886.

bbatsov pushed a commit that referenced this issue Apr 4, 2019
Fixes #6882.

This PR fixes the following error for `Rails/RedundantAllowNil`
when not using both `allow_nil` and `allow_blank`.

```ruby
% cat app/models/user.rb
class User < ApplicationRecord
  validates :email, presence: true
end
```

```console
% rubocop app/models/user.rb --only Rails/RedundantAllowNil -d
For /private/tmp/6882: configuration from
/Users/koic/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.67.1/config/default.yml
Inspecting 1 file
Scanning /private/tmp/6882/app/models/user.rb
An error occurred while Rails/RedundantAllowNil cop was inspecting
/private/tmp/6882/app/models/user.rb:2:2.
undefined method `children' for nil:NilClass
/Users/koic/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.67.1/lib/rubocop/cop/rails/redundant_allow_nil.rb:42:in `on_send'

(snip)

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Rails/RedundantAllowNil cop was inspecting
/private/tmp/6882/app/models/user.rb:2:2.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues

Mention the following information in the issue report:
0.67.1 (using Parser 2.6.2.0, running on ruby 2.6.1 x86_64-darwin17)
```
@connorshea
Copy link
Contributor

I just got the same error in my Rails project :D

@rafasoares
Copy link

Same, still getting it on 0.67.1.

1 error occurred:
An error occurred while Rails/RedundantAllowNil cop was inspecting /path/to/rails/app/models/user.rb:9:2.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues

Mention the following information in the issue report:
0.67.1 (using Parser 2.6.2.0, running on ruby 2.6.2 x86_64-darwin18)
Finished in 5.799085999955423 seconds

@rossjones
Copy link

Sorry, just noticed the changelog mentions the fix isn't released yet but the PR is on master.

@rafasoares
Copy link

You're right. I completely misread the release page. I should probably go to bed. lol

But thanks for pointing it out, I've disabled the cop for now to get rid of the error,

@koic
Copy link
Member

koic commented Apr 5, 2019

Hi there. We RuboCop core team released RuboCop 0.67.2.
https://github.com/rubocop-hq/rubocop/blob/v0.67.2/CHANGELOG.md#0672-2019-04-05

@rossjones
Copy link

@koic you're amazing, thank you!

@connorshea
Copy link
Contributor

Thanks!

@koerneml
Copy link

koerneml commented Apr 8, 2019

1 error occurred:
An error occurred while Rails/RedundantAllowNil cop was inspecting /path/to/rails/app/models/model.rb:12:8.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues

Mention the following information in the issue report:
0.67.2 (using Parser 2.6.2.0, running on ruby 2.6.1 x86_64-linux)

It seems that some of the issue still persists in 0.67.2

in this case, it was a klass.validates-call in a self.inherited(klass) callback if that information is of any help

@koic
Copy link
Member

koic commented Apr 9, 2019

@koerneml Could you open a new issue based on the issue template?
https://github.com/rubocop-hq/rubocop/blob/v0.67.2/.github/ISSUE_TEMPLATE/bug_report.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants