Skip to content

Commit 3dd584c

Browse files
committed
Remove unused conditions
1 parent fff745e commit 3dd584c

File tree

2 files changed

+23
-40
lines changed

2 files changed

+23
-40
lines changed

lib/rubocop/cop/rspec/discarded_matcher.rb

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,50 +55,21 @@ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
5555
def check_discarded_matcher(send_node, node)
5656
return unless matcher_call?(send_node)
5757
return unless inside_example?(node)
58-
return unless example_with_expectation?(node)
5958

6059
target = find_outermost_chain(node)
6160
return unless void_value?(target)
6261

6362
add_offense(target, message: format(MSG, method: node.method_name))
6463
end
6564

66-
def example_with_expectation?(node)
67-
example_node =
68-
node.each_ancestor(:block).find { |ancestor| example?(ancestor) }
69-
70-
example_node && includes_expectation?(example_node)
71-
end
72-
7365
def void_value?(node)
7466
case node.parent.type
7567
when :begin
7668
true
7769
when :block
7870
example?(node.parent)
79-
when :if
80-
void_value_in_branch?(node, node.parent)
81-
when :and, :or
82-
void_in_logical_operator?(node, node.parent)
8371
when :when, :case
84-
void_in_case_branch?(node, node.parent)
85-
end
86-
end
87-
88-
def void_value_in_branch?(node, parent)
89-
(parent.if_branch == node || parent.else_branch == node) &&
90-
void_value?(parent)
91-
end
92-
93-
def void_in_logical_operator?(node, parent)
94-
parent.rhs == node && void_value?(parent)
95-
end
96-
97-
def void_in_case_branch?(node, parent)
98-
if parent.when_type?
99-
parent.body == node && void_value?(parent.parent)
100-
else
101-
parent.else_branch == node && void_value?(parent)
72+
void_value?(node.parent)
10273
end
10374
end
10475

spec/rubocop/cop/rspec/discarded_matcher_spec.rb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,17 @@
158158
RUBY
159159
end
160160

161-
it 'registers an offense for `change` in both `if` and `else` branches' do
162-
expect_offense(<<~RUBY)
161+
it 'does not register for `change` in `if` and `else` branches' do
162+
expect_no_offenses(<<~RUBY)
163163
specify do
164-
expect { result }.to change { obj.foo }.from(1).to(2)
165-
if condition
166-
change { obj.bar }.from(3).to(4)
167-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The result of `change` is not used. Did you mean to chain it with `.and`?
168-
else
169-
change { obj.baz }.from(5).to(6)
170-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The result of `change` is not used. Did you mean to chain it with `.and`?
171-
end
164+
change_temp =
165+
if condition
166+
change { obj.bar }.from(3).to(4)
167+
else
168+
change { obj.baz }.from(5).to(6)
169+
end
170+
171+
expect { result }.to change_temp
172172
end
173173
RUBY
174174
end
@@ -183,6 +183,18 @@
183183
RUBY
184184
end
185185

186+
it 'does not register an offense for `change` in non-void `if` and `else` branches' do
187+
expect_no_offenses(<<~RUBY)
188+
specify do
189+
result = if condition
190+
expect { result }.to change { obj.foo }.from(1).to(2)
191+
else
192+
expect { result }.to change { obj.foo }.from(3).to(4)
193+
end
194+
end
195+
RUBY
196+
end
197+
186198
it 'registers an offense for `change` in a `case` else branch' do
187199
expect_offense(<<~RUBY)
188200
specify do

0 commit comments

Comments
 (0)