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

[Fix #6882] Fix an error for Rails/RedundantAllowNil #6886

Merged

Conversation

koic
Copy link
Member

@koic koic commented Apr 4, 2019

Fixes #6882.

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

% cat app/models/user.rb
class User < ApplicationRecord
  validates :email, presence: true
end
% 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)

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

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)
```
elsif allow_nil_val.false_type? && allow_blank_val.true_type?
add_offense(allow_nil, message: MSG_ALLOW_NIL_FALSE)
end
offense(allow_nil_val, allow_blank_val, allow_nil)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 I extracted to the method because it was warned by Metrics cops.

@bbatsov bbatsov merged commit a09f62b into rubocop:master Apr 4, 2019
@bbatsov
Copy link
Collaborator

bbatsov commented Apr 4, 2019

Thanks!

@koic koic deleted the fix_an_error_for_rails_redundant_allow_nil branch April 5, 2019 01:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rails/RedundantAllowNil stumbles upon validation
2 participants