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 Cop gives false positives under some specific scenarios. #376

Closed
gPrado opened this issue Oct 19, 2020 · 0 comments · Fixed by #394
Closed

Rails/RedundantAllowNil Cop gives false positives under some specific scenarios. #376

gPrado opened this issue Oct 19, 2020 · 0 comments · Fixed by #394
Labels
bug Something isn't working

Comments

@gPrado
Copy link
Contributor

gPrado commented Oct 19, 2020

The Rails/RedundantAllowNil gives false positives when allow_nil/allow_blank configuration are used in nested validations.

In the example below the requirements are:

  • email field must accept nil values;
  • email field must not accept empty string values. The failure message must be "can't be blank" and that must be the only failure message;
  • email field must be validated against a regular expression. The failure message must be "is invalid" and that must be the only failure message;

The allow_blank is therefore not redundant in the nested scope, because any other configuration would break at least one of the requirements above.


Steps to reproduce the problem

Given the following code:

class User
  include ActiveModel::Validations

  attr_accessor :email

  validates :email,
    format: {
      with: /some email address regexp/,
      allow_blank: true
    },
    presence: {
      allow_nil: true
    }
end

or

class User
  include ActiveModel::Validations

  attr_accessor :email

  validates :email,
    format: {
      with: /some email address regexp/,
      allow_blank: true
    },
    presence: true,
    allow_nil: true
end

Expected behavior

Rubocop should not raise errors.

Actual behavior

Rubocop fails with the following message: "Rails/RedundantAllowNil: allow_nil is redundant when allow_blank has the same value."

RuboCop version

0.93.1 (using Parser 2.7.2.0, rubocop-ast 0.8.0, running on ruby 2.5.8 x86_64-linux)

@Drenmi Drenmi added the bug Something isn't working label Nov 20, 2020
ngouy added a commit to ngouy/rubocop-rails that referenced this issue Nov 28, 2020
@koic koic closed this as completed in #394 Nov 30, 2020
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