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 an error for Rails/UniqueValidationWithoutIndex #1021

Conversation

ydah
Copy link
Member

@ydah ydah commented Jun 18, 2023

This PR is fix an error for Rails/UniqueValidationWithoutIndex.

Code to reproduce in models/**/*.rb:

validates :name, presence: true

Occurred Error:

An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting /Users/ydah/test/app/models/test.rb:18:2.
undefined method `falsey_literal?' for nil:NilClass
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/unique_validation_without_index.rb:34:in `on_send'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:143:in `public_send'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:143:in `block (2 levels) in trigger_restricted_cops'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:171:in `with_cop_error_handling'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:142:in `block in trigger_restricted_cops'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:141:in `each'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:141:in `trigger_restricted_cops'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:70:in `on_send'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `each'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:154:in `on_class'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:71:in `on_class'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.29.0/lib/rubocop/ast/traversal.rb:20:in `walk'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cop/commissioner.rb:87:in `investigate'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cop/team.rb:156:in `investigate_partial'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cop/team.rb:98:in `investigate'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:345:in `block in inspect_file'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:344:in `each'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:344:in `flat_map'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:344:in `inspect_file'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:287:in `block in do_inspection_loop'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:321:in `block in iterate_until_no_changes'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:314:in `loop'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:314:in `iterate_until_no_changes'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:283:in `do_inspection_loop'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:164:in `block in file_offenses'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:189:in `file_offense_cache'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:163:in `file_offenses'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:154:in `process_file'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:135:in `block in each_inspected_file'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:134:in `each'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:134:in `reduce'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:134:in `each_inspected_file'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:120:in `inspect_files'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/runner.rb:73:in `run'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cli/command.rb:11:in `run'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cli/environment.rb:18:in `run'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cli.rb:118:in `run_command'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cli.rb:125:in `execute_runners'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cli.rb:51:in `block in run'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cli.rb:77:in `profile_if_needed'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/lib/rubocop/cli.rb:43:in `run'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/exe/rubocop:19:in `block in <top (required)>'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/3.0.0/benchmark.rb:308:in `realtime'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.52.1/exe/rubocop:19:in `<top (required)>'
/Users/ydah/.rbenv/versions/3.0.4/bin/rubocop:25:in `load'
/Users/ydah/.rbenv/versions/3.0.4/bin/rubocop:25:in `<top (required)>'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.3/lib/bundler/cli/exec.rb:58:in `load'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.3/lib/bundler/cli/exec.rb:58:in `kernel_load'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.3/lib/bundler/cli/exec.rb:23:in `run'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.3/lib/bundler/cli.rb:491:in `exec'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.3/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.3/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.3/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.3/lib/bundler/cli.rb:34:in `dispatch'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.3/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.3/lib/bundler/cli.rb:28:in `start'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.3/exe/bundle:45:in `block in <top (required)>'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.3/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
/Users/ydah/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/bundler-2.4.3/exe/bundle:33:in `<top (required)>'
/Users/ydah/.rbenv/versions/3.0.4/bin/bundle:25:in `load'
/Users/ydah/.rbenv/versions/3.0.4/bin/bundle:25:in `<main>'

bundle exec rubocop -V

1.52.1 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.0.4) [x86_64-darwin21]
  - rubocop-capybara 2.18.0
  - rubocop-factory_bot 2.23.1
  - rubocop-performance 1.18.0
  - rubocop-rails 2.20.0
  - rubocop-rspec 2.22.0

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • 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.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@ydah ydah force-pushed the fix_an_error_for_rails_unique_validation_without_index branch from 8fef179 to 2b2adc5 Compare June 18, 2023 10:26
@@ -6,6 +6,7 @@
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
validates :account, uniqueness: true
validates :name, presence: true
Copy link
Member

Choose a reason for hiding this comment

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

For test independence, Can you make this test case a separate it from this it in the context?

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 made it an independent test. What do you think?

This PR is fix an error for `Rails/UniqueValidationWithoutIndex`.
@koic
Copy link
Member

koic commented Jun 19, 2023

Anyway, this solves the problem, so let's merge it. Thank you!

@koic koic merged commit 05ad15b into rubocop:master Jun 19, 2023
10 checks passed
@ydah ydah deleted the fix_an_error_for_rails_unique_validation_without_index branch June 19, 2023 02:24
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.

None yet

5 participants