Skip to content

Commit bd4f92b

Browse files
committed
[Fix #1130] Fix crash for Rails/UniqueValidationWithoutIndex with bare validate
1 parent d3117ff commit bd4f92b

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1130](https://github.com/rubocop/rubocop-rails/issues/1130): Fix crash for `Rails/UniqueValidationWithoutIndex` with bare validate. ([@jamiemccarthy][])

lib/rubocop/cop/rails/unique_validation_without_index.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def class_node(node)
125125

126126
def uniqueness_part(node)
127127
pairs = node.arguments.last
128-
return unless pairs.hash_type?
128+
return unless pairs&.hash_type?
129129

130130
pairs.each_pair.find do |pair|
131131
next unless pair.key.sym_type? && pair.key.value == :uniqueness

spec/rubocop/cop/rails/unique_validation_without_index_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ class User
6060
end
6161
RUBY
6262
end
63+
64+
it 'ignores a bare validates directive by itself' do
65+
expect_no_offenses(<<~RUBY)
66+
class User
67+
validates
68+
end
69+
RUBY
70+
end
71+
72+
it 'ignores a bare validates directive among others' do
73+
expect_no_offenses(<<~RUBY)
74+
class User
75+
validates
76+
after_commit :foo
77+
def foo; true; end
78+
end
79+
RUBY
80+
end
6381
end
6482

6583
context 'when the table has an index but it is not unique' do

0 commit comments

Comments
 (0)