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

UniqueValidationWithoutIndex raises NoMethodError on empty validates #1130

Closed
jamiemccarthy opened this issue Sep 24, 2023 · 0 comments · Fixed by #1131
Closed

UniqueValidationWithoutIndex raises NoMethodError on empty validates #1130

jamiemccarthy opened this issue Sep 24, 2023 · 0 comments · Fixed by #1131

Comments

@jamiemccarthy
Copy link
Contributor

The Rails/UniqueValidationWithoutIndex cop assumes any validates node has at least one argument. It starts its work by scanning the arguments to see if there is a uniqueness part to the directive. But if there are no arguments, it crashes.

(To be clear, validates requires arguments and its presence without arguments is invalid.)

Expected behavior

On a class with a validates directive that has no arguments, no rubocop-rails cop should crash.

(Bonus points if some rubocop-rails cop flags it as incorrect. Currently none does.)

Actual behavior

Rails/UniqueValidationWithoutIndex crashes with a NoMethodError:

# rubocop app/models/foo.rb
Inspecting 1 file
An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting /workspaces/cq/app/models/foo.rb:13:2.
To see the complete backtrace run rubocop -d.
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting /workspaces/cq/app/models/foo.rb:13:2.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.56.3 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.2.2) [aarch64-linux]
# rubocop app/models/foo.rb -d
...
Scanning /workspaces/cq/app/models/foo.rb
An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting /workspaces/cq/app/models/foo.rb:13:2.
undefined method `hash_type?' for nil:NilClass
/usr/local/bundle/gems/rubocop-rails-2.21.1/lib/rubocop/cop/rails/unique_validation_without_index.rb:128:in `uniqueness_part'
/usr/local/bundle/gems/rubocop-rails-2.21.1/lib/rubocop/cop/rails/unique_validation_without_index.rb:35:in `on_send'
/usr/local/bundle/gems/rubocop-1.56.3/lib/rubocop/cop/commissioner.rb:143:in `public_send'
/usr/local/bundle/gems/rubocop-1.56.3/lib/rubocop/cop/commissioner.rb:143:in `block (2 levels) in trigger_restricted_cops'
...

Steps to reproduce the problem

Add the single line

  validates

to any ApplicationRecord class.

RuboCop version

# rubocop -V
1.56.3 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.2.2) [aarch64-linux]
  - rubocop-capybara 2.19.0
  - rubocop-minitest 0.31.1
  - rubocop-performance 1.19.1
  - rubocop-rails 2.21.1
koic added a commit that referenced this issue Sep 25, 2023
[Fix #1130] Fix crash for Rails/UniqueValidationWithoutIndex with bare validate
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 a pull request may close this issue.

1 participant