Skip to content

Commit 8c4429e

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

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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])
88
- Change `RSpec/ScatteredSetup` to allow `around` hooks to be scattered. ([@ydah])
9+
- Fix an error `RSpec/ChangeByZero` cop when without expect block. ([@lee266])
910

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

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

lib/rubocop/cop/rspec/change_by_zero.rb

+3-2
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

+8
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)