Rubocop: Rails/RedundantAllowNil stumbles upon validation

Created on 4 Apr 2019  路  11Comments  路  Source: rubocop-hq/rubocop

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)
bug

Most helpful comment

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

All 11 comments

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'

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

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

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

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

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,

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

@koic you're amazing, thank you!

Thanks!

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

@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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

david942j picture david942j  路  3Comments

tedPen picture tedPen  路  3Comments

mikegee picture mikegee  路  3Comments

ecbrodie picture ecbrodie  路  3Comments

Ana06 picture Ana06  路  3Comments