Skip to content

Commit d1eb4af

Browse files
author
rito.ishihara
committed
Fix RSpec/ChangeByZero cop to handle invalid change matcher usage
issue number: #2069
1 parent e85969d commit d1eb4af

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Fix false positive in `RSpec/Pending`, where it would mark the default block `it` as an offense. ([@bquorning])
66
- Fix issue when `Style/ContextWording` is configured with a Prefix being interpreted as a boolean, like `on`. ([@sakuro])
77
- Add new `RSpec/IncludeExamples` cop to enforce using `it_behaves_like` over `include_examples`. ([@dvandersluis])
8+
- Fix `RSpec/ChangeByZero` cop error when analyzing invalid change matcher syntax. ([@lee266])
89

910
## 3.5.0 (2025-02-16)
1011

@@ -1002,6 +1003,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
10021003
[@krororo]: https://github.com/krororo
10031004
[@kuahyeow]: https://github.com/kuahyeow
10041005
[@lazycoder9]: https://github.com/lazycoder9
1006+
[@lee266]: https://github.com/lee266
10051007
[@leoarnold]: https://github.com/leoarnold
10061008
[@liberatys]: https://github.com/Liberatys
10071009
[@lokhi]: https://github.com/lokhi

lib/rubocop/cop/rspec/change_by_zero.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ def on_send(node)
102102
private
103103

104104
def register_offense(node, change_node)
105+
if !node.parent.respond_to?(:send_type?) || !node.parent.send_type?
106+
return
107+
end
108+
105109
if compound_expectations?(node)
106110
add_offense(node,
107111
message: message_compound(change_node)) do |corrector|
@@ -116,8 +120,7 @@ def register_offense(node, change_node)
116120
end
117121

118122
def compound_expectations?(node)
119-
node.parent.send_type? &&
120-
%i[and or & |].include?(node.parent.method_name)
123+
%i[and or & |].include?(node.parent.method_name)
121124
end
122125

123126
def message(change_node)

spec/rubocop/cop/rspec/change_by_zero_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -365,5 +365,12 @@
365365
expect { foo }.to change { Foo.bar }.by(1)
366366
end
367367
RUBY
368+
369+
expect_no_offenses(<<~RUBY)
370+
it do
371+
subject; change(Foo, :bar).by(0)
372+
change(foo, :bar).by(0)
373+
end
374+
RUBY
368375
end
369376
end

0 commit comments

Comments
 (0)