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

Rails/HasManyOrHasOneDependent reports false positive when has_many or has_one is called on an explicit receiver #1158

Closed
samrjenkins opened this issue Oct 17, 2023 · 0 comments · Fixed by #1159

Comments

@samrjenkins
Copy link
Contributor

Rails/HasManyOrHasOneDependent is reporting false positives when has_many or has_one is called on an explicit receiver. The pattern used to match a with_options block does not match blocks which define a parameter:

with_options do |foo|

it will only match blocks without parameters:

with_options do

Expected behavior

No offense:

class Foo < ActiveRecord::Base
  with_options dependent: :destroy do |model|
    model.has_many :bars
    model.has_one :baz
  end
end

Actual behavior

Two offenses:

class Foo < ActiveRecord::Base
  with_options dependent: :destroy do |model|
    model.has_many :bars
          ^^^^^^^^ Specify a `:dependent` option.
    model.has_one :baz
          ^^^^^^^ Specify a `:dependent` option.
  end
end

Steps to reproduce the problem

See "Actual behaviour" example above

RuboCop version

1.56.4 (using Parser 3.2.2.4, rubocop-ast 1.29.0, running on ruby 3.0.6) [arm64-darwin22]
  - rubocop-capybara 2.19.0
  - rubocop-factory_bot 2.24.0
  - rubocop-florence 0.8.0
  - rubocop-performance 1.19.1
  - rubocop-rails 2.21.2
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.24.1
  - rubocop-thread_safety 0.5.1

samrjenkins added a commit to samrjenkins/rubocop-rails that referenced this issue Oct 17, 2023
…e when has_many or has_one called on explicit receiver
samrjenkins added a commit to samrjenkins/rubocop-rails that referenced this issue Oct 17, 2023
…ive when has_many or has_one called on explicit receiver
samrjenkins added a commit to samrjenkins/rubocop-rails that referenced this issue Oct 17, 2023
…ive when has_many or has_one called on explicit receiver
samrjenkins added a commit to samrjenkins/rubocop-rails that referenced this issue Oct 17, 2023
…ive when has_many or has_one called on explicit receiver
koic added a commit that referenced this issue Oct 18, 2023
…endent-false-positive

[Fix #1158] Rails/HasManyOrHasOneDependent reports false positive when has_many or has_one called on explicit receiver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant