From 20929d3d69446e65968b63a3835f873f07541a95 Mon Sep 17 00:00:00 2001 From: ydah Date: Sat, 15 Nov 2025 17:02:26 +0900 Subject: [PATCH] Fix a false positive for `RspecRails/NegationBeValid` when use `to_not` Fixes: #70 --- CHANGELOG.md | 2 ++ lib/rubocop/cop/rspec_rails/negation_be_valid.rb | 2 +- spec/rubocop/cop/rspec_rails/negation_be_valid_spec.rb | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2d63657..ba14c680 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Master (Unreleased) +- Fix a false positive for `RspecRails/NegationBeValid` when use `to_not`. ([@ydah]) + ## 2.32.0 (2025-11-12) - Add `RSpecRails/HttpStatusNameConsistency` cop. ([@taketo1113]) diff --git a/lib/rubocop/cop/rspec_rails/negation_be_valid.rb b/lib/rubocop/cop/rspec_rails/negation_be_valid.rb index c57b228f..3f3f66f4 100644 --- a/lib/rubocop/cop/rspec_rails/negation_be_valid.rb +++ b/lib/rubocop/cop/rspec_rails/negation_be_valid.rb @@ -38,7 +38,7 @@ class NegationBeValid < ::RuboCop::Cop::Base # @!method not_to?(node) def_node_matcher :not_to?, <<~PATTERN - (send ... :not_to (send nil? :be_valid ...)) + (send ... {:not_to :to_not} (send nil? :be_valid ...)) PATTERN # @!method be_invalid?(node) diff --git a/spec/rubocop/cop/rspec_rails/negation_be_valid_spec.rb b/spec/rubocop/cop/rspec_rails/negation_be_valid_spec.rb index d35bf4a4..a920e48f 100644 --- a/spec/rubocop/cop/rspec_rails/negation_be_valid_spec.rb +++ b/spec/rubocop/cop/rspec_rails/negation_be_valid_spec.rb @@ -48,6 +48,14 @@ RUBY end + it 'registers an offense when using ' \ + '`expect(...).to_not be_valid`' do + expect_offense(<<~RUBY) + expect(foo).to_not be_valid + ^^^^^^^^^^^^^^^ Use `expect(...).to be_invalid`. + RUBY + end + it 'does not register an offense when using ' \ '`expect(...).to be_invalid`' do expect_no_offenses(<<~RUBY)