diff --git a/changelog/fix_safe_navigator_for_skips_model_validation.md b/changelog/fix_safe_navigator_for_skips_model_validation.md new file mode 100644 index 0000000000..c253c12619 --- /dev/null +++ b/changelog/fix_safe_navigator_for_skips_model_validation.md @@ -0,0 +1 @@ +* [#1278](https://github.com/rubocop/rubocop-rails/issues/1278): Fix a false positive for `Rails/SkipsModelValidations` when using `insert` with a safe navigator. ([@tldn0718][]) diff --git a/lib/rubocop/cop/rails/skips_model_validations.rb b/lib/rubocop/cop/rails/skips_model_validations.rb index de92ef30c5..c89cc13422 100644 --- a/lib/rubocop/cop/rails/skips_model_validations.rb +++ b/lib/rubocop/cop/rails/skips_model_validations.rb @@ -63,7 +63,7 @@ class SkipsModelValidations < Base PATTERN def_node_matcher :good_insert?, <<~PATTERN - (send _ {:insert :insert!} _ { + (call _ {:insert :insert!} _ { !(hash ...) (hash <(pair (sym !{:returning :unique_by}) _) ...>) } ...) diff --git a/spec/rubocop/cop/rails/skips_model_validations_spec.rb b/spec/rubocop/cop/rails/skips_model_validations_spec.rb index 4e430f23c4..3ab96881dd 100644 --- a/spec/rubocop/cop/rails/skips_model_validations_spec.rb +++ b/spec/rubocop/cop/rails/skips_model_validations_spec.rb @@ -57,12 +57,14 @@ it "does not register an offense for #{method} that looks like String#insert" do expect_no_offenses(<<~RUBY) string.#{method}(0, 'b') + string&.#{method}(0, 'b') RUBY end it "does not register an offense for #{method} that looks like Array#insert" do expect_no_offenses(<<~RUBY) array.#{method}(1, :a, :b) + array&.#{method}(1, :a, :b) RUBY end