Skip to content

Commit b8e8405

Browse files
authored
Merge pull request #625 from koic/fix_a_false_positive_for_rails_redundant_presence_validation_on_belongs_to
[Fix #620] Fix a false positive for `Rails/RedundantPresenceValidationOnBelongsTo`
2 parents 9bf85ea + f10b921 commit b8e8405

File tree

3 files changed

+4
-11
lines changed

3 files changed

+4
-11
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#620](https://github.com/rubocop/rubocop-rails/issues/620): Fix a false positive for `Rails/RedundantPresenceValidationOnBelongsTo` using presence with a message. ([@koic][])

lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ class RedundantPresenceValidationOnBelongsTo < Base
5050
# @example source that matches - by association
5151
# validates :name, :user, presence: true
5252
#
53-
# @example source that matches - with presence options
54-
# validates :user, presence: { message: 'duplicate' }
55-
#
5653
# @example source that matches - by a foreign key
5754
# validates :user_id, presence: true
5855
#
@@ -66,7 +63,7 @@ class RedundantPresenceValidationOnBelongsTo < Base
6663
send nil? :validates
6764
(sym $_)+
6865
$[
69-
(hash <$(pair (sym :presence) {true hash}) ...>) # presence: true
66+
(hash <$(pair (sym :presence) true) ...>) # presence: true
7067
!(hash <$(pair (sym :strict) {true const}) ...>) # strict: true
7168
]
7269
)

spec/rubocop/cop/rails/redundant_presence_validation_on_belongs_to_spec.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,10 @@ class Profile
173173
RUBY
174174
end
175175

176-
it 'registers an offense for presence with a message' do
177-
expect_offense(<<~RUBY)
176+
it 'does not register an offense for presence with a message' do
177+
expect_no_offenses(<<~RUBY)
178178
belongs_to :user
179179
validates :user, presence: { message: 'Must be present' }
180-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove explicit presence validation for `user`.
181-
RUBY
182-
183-
expect_correction(<<~RUBY)
184-
belongs_to :user
185180
RUBY
186181
end
187182

0 commit comments

Comments
 (0)