Skip to content

Commit c25abf4

Browse files
authored
Merge pull request #2071 from lee266/bugfix-change-by-zero
Fix an error `RSpec/ChangeByZero` cop when without expect block
2 parents 14f3dd2 + 8c4429e commit c25abf4

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
@@ -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

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)