Skip to content

Commit 9aa4b8a

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

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-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 an error `RSpec/ChangeByZero` cop when without expect block. ([@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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def on_send(node)
102102
private
103103

104104
def register_offense(node, change_node)
105+
return unless node.parent.send_type?
106+
105107
if compound_expectations?(node)
106108
add_offense(node,
107109
message: message_compound(change_node)) do |corrector|
@@ -116,8 +118,7 @@ def register_offense(node, change_node)
116118
end
117119

118120
def compound_expectations?(node)
119-
node.parent.send_type? &&
120-
%i[and or & |].include?(node.parent.method_name)
121+
%i[and or & |].include?(node.parent.method_name)
121122
end
122123

123124
def message(change_node)

spec/rubocop/cop/rspec/change_by_zero_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,12 @@
366366
end
367367
RUBY
368368
end
369+
370+
it 'does not register an offense when without expect block' do
371+
expect_no_offenses(<<~RUBY)
372+
it do
373+
change(foo, :bar).by(0)
374+
end
375+
RUBY
376+
end
369377
end

0 commit comments

Comments
 (0)