Skip to content

Commit 764b0ae

Browse files
committed
Emit redundant if when duplicated in needless_continue
1 parent e582fcd commit 764b0ae

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

clippy_lints/src/needless_continue.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,10 @@ fn check_and_warn(cx: &EarlyContext<'_>, expr: &ast::Expr) {
422422
);
423423
}
424424
};
425-
check_last_stmt_in_block(loop_block, &p);
426425

427-
for (i, stmt) in loop_block.stmts.iter().enumerate() {
426+
let stmts = &loop_block.stmts;
427+
for (i, stmt) in stmts.iter().enumerate() {
428+
let mut maybe_emitted_in_if = false;
428429
with_if_expr(stmt, |if_expr, cond, then_block, else_expr| {
429430
let data = &LintData {
430431
if_expr,
@@ -434,6 +435,8 @@ fn check_and_warn(cx: &EarlyContext<'_>, expr: &ast::Expr) {
434435
stmt_idx: i,
435436
loop_block,
436437
};
438+
439+
maybe_emitted_in_if = true;
437440
if needless_continue_in_else(else_expr, label) {
438441
emit_warning(
439442
cx,
@@ -443,8 +446,14 @@ fn check_and_warn(cx: &EarlyContext<'_>, expr: &ast::Expr) {
443446
);
444447
} else if is_first_block_stmt_continue(then_block, label) {
445448
emit_warning(cx, data, DROP_ELSE_BLOCK_MSG, LintType::ContinueInsideThenBlock);
449+
} else {
450+
maybe_emitted_in_if = false;
446451
}
447452
});
453+
454+
if i == stmts.len() - 1 && !maybe_emitted_in_if {
455+
check_last_stmt_in_block(loop_block, &p);
456+
}
448457
}
449458
});
450459
}

tests/ui/needless_continue.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,32 @@ mod issue_4077 {
194194
}
195195
}
196196
}
197+
198+
for _ in 0..10 {
199+
match "foo".parse::<i32>() {
200+
Ok(_) => do_something(),
201+
Err(_) => {
202+
println!("bar-10");
203+
continue;
204+
},
205+
}
206+
}
207+
208+
loop {
209+
if true {
210+
} else {
211+
// redundant `else`
212+
continue; // redundant `continue`
213+
}
214+
}
215+
216+
loop {
217+
if some_expr() {
218+
continue;
219+
} else {
220+
do_something();
221+
}
222+
}
197223
}
198224

199225
// The contents of these functions are irrelevant, the purpose of this file is

tests/ui/needless_continue.stderr

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,47 @@ LL | continue 'inner;
168168
|
169169
= help: consider dropping the `continue` expression
170170

171-
error: aborting due to 12 previous errors
171+
error: this `continue` expression is redundant
172+
--> tests/ui/needless_continue.rs:203:21
173+
|
174+
LL | continue;
175+
| ^^^^^^^^
176+
|
177+
= help: consider dropping the `continue` expression
178+
179+
error: this `else` block is redundant
180+
--> tests/ui/needless_continue.rs:210:20
181+
|
182+
LL | } else {
183+
| ____________________^
184+
LL | | // redundant `else`
185+
LL | | continue; // redundant `continue`
186+
LL | | }
187+
| |_____________^
188+
|
189+
= help: consider dropping the `else` clause and merging the code that follows (in the loop) with the `if` block
190+
if true {
191+
// merged code follows:
192+
193+
}
194+
195+
error: there is no need for an explicit `else` block for this `if` expression
196+
--> tests/ui/needless_continue.rs:217:13
197+
|
198+
LL | / if some_expr() {
199+
LL | | continue;
200+
LL | | } else {
201+
LL | | do_something();
202+
LL | | }
203+
| |_____________^
204+
|
205+
= help: consider dropping the `else` clause
206+
if some_expr() {
207+
continue;
208+
}
209+
{
210+
do_something();
211+
}
212+
213+
error: aborting due to 15 previous errors
172214

0 commit comments

Comments
 (0)