Skip to content

Fix an error RSpec/ChangeByZero cop when without expect block #2071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fix issue when `Style/ContextWording` is configured with a Prefix being interpreted as a boolean, like `on`. ([@sakuro])
- Add new `RSpec/IncludeExamples` cop to enforce using `it_behaves_like` over `include_examples`. ([@dvandersluis])
- Change `RSpec/ScatteredSetup` to allow `around` hooks to be scattered. ([@ydah])
- Fix an error `RSpec/ChangeByZero` cop when without expect block. ([@lee266])

## 3.5.0 (2025-02-16)

Expand Down Expand Up @@ -1003,6 +1004,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@krororo]: https://github.com/krororo
[@kuahyeow]: https://github.com/kuahyeow
[@lazycoder9]: https://github.com/lazycoder9
[@lee266]: https://github.com/lee266
[@leoarnold]: https://github.com/leoarnold
[@liberatys]: https://github.com/Liberatys
[@lokhi]: https://github.com/lokhi
Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/rspec/change_by_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def on_send(node)
private

def register_offense(node, change_node)
return unless node.parent.send_type?

if compound_expectations?(node)
add_offense(node,
message: message_compound(change_node)) do |corrector|
Expand All @@ -116,8 +118,7 @@ def register_offense(node, change_node)
end

def compound_expectations?(node)
node.parent.send_type? &&
%i[and or & |].include?(node.parent.method_name)
%i[and or & |].include?(node.parent.method_name)
end

def message(change_node)
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rspec/change_by_zero_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,12 @@
end
RUBY
end

it 'does not register an offense when without expect block' do
expect_no_offenses(<<~RUBY)
it do
change(foo, :bar).by(0)
end
RUBY
end
end