From 8c4429e7426118a391f548e229492130f6609c29 Mon Sep 17 00:00:00 2001 From: "rito.ishihara" Date: Sat, 5 Apr 2025 00:32:11 +0900 Subject: [PATCH] Fix RSpec/ChangeByZero cop to handle invalid change matcher usage issue number: #2069 --- CHANGELOG.md | 2 ++ lib/rubocop/cop/rspec/change_by_zero.rb | 5 +++-- spec/rubocop/cop/rspec/change_by_zero_spec.rb | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a6335b37..597552321 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Fix issue when `Style/ContextWording` is configured with a Prefix being interpreted as a boolean, like `on`. ([@sakuro]) - Add new `RSpec/IncludeExamples` cop to enforce using `it_behaves_like` over `include_examples`. ([@dvandersluis]) - Change `RSpec/ScatteredSetup` to allow `around` hooks to be scattered. ([@ydah]) +- Fix an error `RSpec/ChangeByZero` cop when without expect block. ([@lee266]) ## 3.5.0 (2025-02-16) @@ -1003,6 +1004,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features. [@krororo]: https://github.com/krororo [@kuahyeow]: https://github.com/kuahyeow [@lazycoder9]: https://github.com/lazycoder9 +[@lee266]: https://github.com/lee266 [@leoarnold]: https://github.com/leoarnold [@liberatys]: https://github.com/Liberatys [@lokhi]: https://github.com/lokhi diff --git a/lib/rubocop/cop/rspec/change_by_zero.rb b/lib/rubocop/cop/rspec/change_by_zero.rb index 71407f74b..64c5df4b2 100644 --- a/lib/rubocop/cop/rspec/change_by_zero.rb +++ b/lib/rubocop/cop/rspec/change_by_zero.rb @@ -102,6 +102,8 @@ def on_send(node) private def register_offense(node, change_node) + return unless node.parent.send_type? + if compound_expectations?(node) add_offense(node, message: message_compound(change_node)) do |corrector| @@ -116,8 +118,7 @@ def register_offense(node, change_node) end def compound_expectations?(node) - node.parent.send_type? && - %i[and or & |].include?(node.parent.method_name) + %i[and or & |].include?(node.parent.method_name) end def message(change_node) diff --git a/spec/rubocop/cop/rspec/change_by_zero_spec.rb b/spec/rubocop/cop/rspec/change_by_zero_spec.rb index 4e9604768..675716fee 100644 --- a/spec/rubocop/cop/rspec/change_by_zero_spec.rb +++ b/spec/rubocop/cop/rspec/change_by_zero_spec.rb @@ -366,4 +366,12 @@ end RUBY end + + it 'does not register an offense when without expect block' do + expect_no_offenses(<<~RUBY) + it do + change(foo, :bar).by(0) + end + RUBY + end end