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/RedundantPresenceValidationOnBelongsTo incorrectly fixes cases where validation is conditional #1105

Closed
mheguy-stingray opened this issue Sep 4, 2023 · 0 comments · Fixed by #1140

Comments

@mheguy-stingray
Copy link

Rails/RedundantPresenceValidationOnBelongsTo changes the following:

class DataExport < ApplicationRecord
  belongs_to :survey
  validates :survey, presence: true, unless: -> { condition }
end

to:

class DataExport < ApplicationRecord
  belongs_to :survey
  validates :survey, unless: -> { condition  }
end

Dropping the condition on the presence check clearly has an effect on behaviour.

The doc indicates that the cop is marked unsafe because the error message changes, so this seems like a bug.

Specs:

1.56.2 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.2.2) [x86_64-linux]
  - rubocop-factory_bot 2.23.1
  - rubocop-rails 2.20.2
  - rubocop-rspec 2.23.2
koic added a commit to koic/rubocop-rails that referenced this issue Oct 5, 2023
…lidationOnBelongsTo`

Fixes rubocop#1105.

This PR fixes false positives for `Rails/RedundantPresenceValidationOnBelongsTo`
when using `validates` with `:if` or `:unless` options.
koic added a commit to koic/rubocop-rails that referenced this issue Oct 5, 2023
…lidationOnBelongsTo`

Fixes rubocop#1105.

This PR fixes false positives for `Rails/RedundantPresenceValidationOnBelongsTo`
when using `validates` with `:if` or `:unless` options.
@koic koic closed this as completed in #1140 Oct 7, 2023
koic added a commit that referenced this issue Oct 7, 2023
…presence_validation_on_belongs_to

[Fix #1105] Fix false positives for `Rails/RedundantPresenceValidationOnBelongsTo`
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