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 #551] Fix a false positive for Rails/FindEach #552

Merged
merged 1 commit into from
Sep 25, 2021

Conversation

koic
Copy link
Member

@koic koic commented Sep 17, 2021

Fixes #551.

This PR fixes a false positive for Rails/FindEach when using model.errors.where in Rails 6.1.


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 (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.
  • 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.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

Fixes rubocop#551.

This PR fixes a false positive for `Rails/FindEach`
when using `model.errors.where` in Rails 6.1.
@koic koic merged commit 7c5ecd5 into rubocop:master Sep 25, 2021
@koic koic deleted the fix_a_false_positive_for_rails_find_each branch September 25, 2021 05:06
@agrobbin
Copy link
Contributor

agrobbin commented Oct 6, 2021

This has resulted in a send_type? exception for us when trying to upgrade.

undefined method `send_type?' for nil:NilClass
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-rails-2.12.3/lib/rubocop/cop/rails/find_each.rb:58:in `active_model_error?'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-rails-2.12.3/lib/rubocop/cop/rails/find_each.rb:54:in `active_model_error_where?'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-rails-2.12.3/lib/rubocop/cop/rails/find_each.rb:46:in `ignored?'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-rails-2.12.3/lib/rubocop/cop/rails/find_each.rb:35:in `on_send'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:136:in `public_send'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:136:in `block (2 levels) in trigger_restricted_cops'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:160:in `with_cop_error_handling'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:135:in `block in trigger_restricted_cops'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:134:in `each'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:134:in `trigger_restricted_cops'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:70:in `on_send'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:158:in `on_block'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:137:in `each'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:154:in `on_class'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:71:in `on_class'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:154:in `on_class'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:71:in `on_class'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:138:in `on_while'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:71:in `on_module'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-ast-1.12.0/lib/rubocop/ast/traversal.rb:20:in `walk'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/commissioner.rb:86:in `investigate'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/team.rb:155:in `investigate_partial'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cop/team.rb:83:in `investigate'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/runner.rb:309:in `inspect_file'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/runner.rb:253:in `block in do_inspection_loop'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/runner.rb:287:in `block in iterate_until_no_changes'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/runner.rb:280:in `loop'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/runner.rb:280:in `iterate_until_no_changes'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/runner.rb:249:in `do_inspection_loop'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/runner.rb:130:in `block in file_offenses'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/runner.rb:155:in `file_offense_cache'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/runner.rb:129:in `file_offenses'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/runner.rb:67:in `block in warm_cache'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/parallel-1.21.0/lib/parallel.rb:515:in `call_with_index'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/parallel-1.21.0/lib/parallel.rb:485:in `process_incoming_jobs'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/parallel-1.21.0/lib/parallel.rb:465:in `block in worker'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/parallel-1.21.0/lib/parallel.rb:456:in `fork'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/parallel-1.21.0/lib/parallel.rb:456:in `worker'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/parallel-1.21.0/lib/parallel.rb:447:in `block in create_workers'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/parallel-1.21.0/lib/parallel.rb:446:in `each'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/parallel-1.21.0/lib/parallel.rb:446:in `each_with_index'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/parallel-1.21.0/lib/parallel.rb:446:in `create_workers'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/parallel-1.21.0/lib/parallel.rb:386:in `work_in_processes'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/parallel-1.21.0/lib/parallel.rb:289:in `map'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/parallel-1.21.0/lib/parallel.rb:238:in `each'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/runner.rb:67:in `warm_cache'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/runner.rb:46:in `run'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cli/command.rb:11:in `run'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cli/environment.rb:18:in `run'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cli.rb:71:in `run_command'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cli.rb:78:in `execute_runners'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/lib/rubocop/cli.rb:47:in `run'
/usr/local/var/rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rubocop-1.22.1/exe/rubocop:12:in `block in <top (required)>'

The code in question looks like this:

where(record: [record1, record2]).each(&:touch)

Previously, we had been explicitly disabling Rails/FindEach on this line, but with this change, it also complains about an unnecessary disabling of the cop, which surprises us.

koic added a commit to koic/rubocop-rails that referenced this pull request Oct 7, 2021
Follow up to rubocop#552 (comment).

This PR fixes an error for `Rails/FindEach` when using `where` with no receiver.
koic added a commit to koic/rubocop-rails that referenced this pull request Oct 7, 2021
Follow up to rubocop#552 (comment).

This PR fixes an error for `Rails/FindEach` when using `where` with no receiver.
@koic koic mentioned this pull request Oct 7, 2021
9 tasks
koic added a commit to koic/rubocop-rails that referenced this pull request Oct 7, 2021
Follow up to rubocop#552 (comment).

This PR fixes an error for `Rails/FindEach` when using `where` with no receiver.
@koic
Copy link
Member Author

koic commented Oct 7, 2021

@agrobbin Thanks for your feedback. I can reproduce the error and I fixed it by #573.

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/FindEach: false positive on ActiveModel::Errors on Rails 6.1
2 participants