From 40d9dc3fef76096449ffa26eb7ee55c0388b0c40 Mon Sep 17 00:00:00 2001 From: junglefish Date: Thu, 16 May 2024 22:16:35 +0900 Subject: [PATCH] [Fix rubocop#1278] Fix a false positivie for Rails/SkipsModelValidations --- changelog/fix_safe_navigator_for_skips_model_validation.md | 1 + lib/rubocop/cop/rails/skips_model_validations.rb | 2 +- spec/rubocop/cop/rails/skips_model_validations_spec.rb | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_safe_navigator_for_skips_model_validation.md 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..830e51588c --- /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` or `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