Skip to content

Commit a05b88a

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

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
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. ([@rito])
89

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

@@ -1041,6 +1042,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
10411042
[@rolfschmidt]: https://github.com/rolfschmidt
10421043
[@rrosenblum]: https://github.com/rrosenblum
10431044
[@rspeicher]: https://github.com/rspeicher
1045+
[@rito]: https://github.com/Lee266
10441046
[@rst-j]: https://github.com/RST-J
10451047
[@sakuro]: https://github.com/sakuro
10461048
[@samrjenkins]: https://github.com/samrjenkins

lib/rubocop/cop/rspec/change_by_zero.rb

Lines changed: 5 additions & 2 deletions
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

Lines changed: 7 additions & 0 deletions
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)