Skip to content

UniqueValidationWithoutIndex raises NoMethodError on empty validates #1130

Closed
@jamiemccarthy

Description

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions