Skip to content

Commit d5655f2

Browse files
authored
Merge pull request #1324 from BrianHawley/fix_change_by_zero_compound
Fix RSpec/ChangeByZero with compound operators
2 parents 858ba95 + 048b90f commit d5655f2

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Fix `RSpec/FilePath` cop missing mismatched expanded namespace. ([@sl4vr][])
66
* Add new `AllowConsecutiveOneLiners` (default true) option for `Rspec/EmptyLineAfterHook` cop. ([@ngouy][])
77
* Add autocorrect support for `RSpec/EmptyExampleGroup`. ([@r7kamura][])
8+
* Fix `RSpec/ChangeByZero` with compound expressions using `&` or `|` operators. ([@BrianHawley][])
89

910
## 2.12.1 (2022-07-03)
1011

@@ -718,3 +719,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
718719
[@edgibbs]: https://github.com/edgibbs
719720
[@Drowze]: https://github.com/Drowze
720721
[@akiomik]: https://github.com/akiomik
722+
[@BrianHawley]: https://github.com/BrianHawley

lib/rubocop/cop/rspec/change_by_zero.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def check_offense(node)
7979
end
8080

8181
def compound_expectations?(node)
82-
%i[and or].include?(node.parent.method_name)
82+
%i[and or & |].include?(node.parent.method_name)
8383
end
8484

8585
def autocorrect(corrector, node)

spec/rubocop/cop/rspec/change_by_zero_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@
3939
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
4040
.and change { Foo.baz }.by(0)
4141
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
42+
expect { foo }
43+
.to change(Foo, :bar).by(0) &
44+
^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
45+
change(Foo, :baz).by(0)
46+
^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
47+
expect { foo }
48+
.to change { Foo.bar }.by(0) &
49+
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
50+
change { Foo.baz }.by(0)
51+
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
4252
expect { foo }
4353
.to change(Foo, :bar).by(0)
4454
^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
@@ -49,8 +59,20 @@
4959
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
5060
.or change { Foo.baz }.by(0)
5161
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
62+
expect { foo }
63+
.to change(Foo, :bar).by(0) |
64+
^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
65+
change(Foo, :baz).by(0)
66+
^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
67+
expect { foo }
68+
.to change { Foo.bar }.by(0) |
69+
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
70+
change { Foo.baz }.by(0)
71+
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
5272
end
5373
RUBY
74+
75+
expect_no_corrections
5476
end
5577

5678
it 'does not register an offense when the argument to `by` is not zero' do

0 commit comments

Comments
 (0)