Skip to content

Fix never_loop forget to remove break in nested loop #15356

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions clippy_lints/src/loops/never_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn contains_any_break_or_continue(block: &Block<'_>) -> bool {
/// The first two bits of information are in this enum, and the last part is in the
/// `local_labels` variable, which contains a list of `(block_id, reachable)` pairs ordered by
/// scope.
#[derive(Clone)]
#[derive(Clone, Debug)]
enum NeverLoopResult {
/// A continue may occur for the main loop.
MayContinueMainLoop,
Expand Down Expand Up @@ -207,6 +207,8 @@ fn all_spans_after_expr(cx: &LateContext<'_>, expr: &Expr<'_>) -> Vec<Span> {
}

return vec![stmt.span];
} else if let Node::Block(_) = cx.tcx.parent_hir_node(expr.hir_id) {
return vec![expr.span];
}

vec![]
Expand Down Expand Up @@ -356,7 +358,9 @@ fn never_loop_expr<'tcx>(
};
let result = combine_seq(result, || {
if cx.typeck_results().expr_ty(expr).is_never() {
NeverLoopResult::Diverging { break_spans: vec![] }
NeverLoopResult::Diverging {
break_spans: all_spans_after_expr(cx, expr),
}
} else {
NeverLoopResult::Normal
}
Expand Down
9 changes: 0 additions & 9 deletions tests/ui/never_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,15 +431,6 @@ fn stmt_after_return() {
}

fn loop_label() {
'outer: for v in 0..10 {
//~^ never_loop
loop {
//~^ never_loop
break 'outer;
}
return;
}

for v in 0..10 {
//~^ never_loop
'inner: loop {
Expand Down
36 changes: 5 additions & 31 deletions tests/ui/never_loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -185,32 +185,6 @@ LL ~
error: this loop never actually loops
--> tests/ui/never_loop.rs:434:5
|
LL | / 'outer: for v in 0..10 {
LL | |
LL | | loop {
... |
LL | | return;
LL | | }
| |_____^
|
help: if you need the first element of the iterator, try writing
|
LL - 'outer: for v in 0..10 {
LL + if let Some(v) = (0..10).next() {
|

error: this loop never actually loops
--> tests/ui/never_loop.rs:436:9
|
LL | / loop {
LL | |
LL | | break 'outer;
LL | | }
| |_________^

error: this loop never actually loops
--> tests/ui/never_loop.rs:443:5
|
LL | / for v in 0..10 {
LL | |
LL | | 'inner: loop {
Expand All @@ -226,7 +200,7 @@ LL + if let Some(v) = (0..10).next() {
|

error: this loop never actually loops
--> tests/ui/never_loop.rs:445:9
--> tests/ui/never_loop.rs:436:9
|
LL | / 'inner: loop {
LL | |
Expand All @@ -235,7 +209,7 @@ LL | | }
| |_________^

error: this loop never actually loops
--> tests/ui/never_loop.rs:471:5
--> tests/ui/never_loop.rs:462:5
|
LL | / 'a: for _ in 0..1 {
LL | |
Expand All @@ -251,7 +225,7 @@ LL ~
|

error: this loop never actually loops
--> tests/ui/never_loop.rs:477:5
--> tests/ui/never_loop.rs:468:5
|
LL | / 'a: for i in 0..1 {
LL | |
Expand All @@ -275,7 +249,7 @@ LL ~
|

error: this loop never actually loops
--> tests/ui/never_loop.rs:492:5
--> tests/ui/never_loop.rs:483:5
|
LL | / for v in 0..10 {
LL | |
Expand All @@ -297,5 +271,5 @@ LL ~
LL ~
|

error: aborting due to 24 previous errors
error: aborting due to 22 previous errors

17 changes: 16 additions & 1 deletion tests/ui/never_loop_fixable.fixed
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(clippy::iter_next_slice, clippy::needless_return)]
#![allow(clippy::iter_next_slice, clippy::needless_return, clippy::redundant_pattern_matching)]

fn no_break_or_continue_loop() {
if let Some(i) = [1, 2, 3].iter().next() {
Expand All @@ -18,3 +18,18 @@ fn no_break_or_continue_loop_outer() {
}
}
}

fn loop_label() {
if let Some(v) = (0..10).next() {
//~^ never_loop


}
}

fn issue15350() {
if let Some(_) = (0..100).next() {
//~^ never_loop

}
}
23 changes: 22 additions & 1 deletion tests/ui/never_loop_fixable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(clippy::iter_next_slice, clippy::needless_return)]
#![allow(clippy::iter_next_slice, clippy::needless_return, clippy::redundant_pattern_matching)]

fn no_break_or_continue_loop() {
for i in [1, 2, 3].iter() {
Expand All @@ -18,3 +18,24 @@ fn no_break_or_continue_loop_outer() {
}
}
}

fn loop_label() {
'outer: for v in 0..10 {
//~^ never_loop
loop {
//~^ never_loop
break 'outer;
}
return;
}
}

fn issue15350() {
'bar: for _ in 0..100 {
//~^ never_loop
loop {
//~^ never_loop
break 'bar;
}
}
}
56 changes: 55 additions & 1 deletion tests/ui/never_loop_fixable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,59 @@ LL - for i in [1, 2, 3].iter() {
LL + if let Some(i) = [1, 2, 3].iter().next() {
|

error: aborting due to 2 previous errors
error: this loop never actually loops
--> tests/ui/never_loop_fixable.rs:23:5
|
LL | / 'outer: for v in 0..10 {
LL | |
LL | | loop {
... |
LL | | return;
LL | | }
| |_____^
|
help: if you need the first element of the iterator, try writing
|
LL ~ if let Some(v) = (0..10).next() {
LL |
LL ~
LL ~
|

error: this loop never actually loops
--> tests/ui/never_loop_fixable.rs:25:9
|
LL | / loop {
LL | |
LL | | break 'outer;
LL | | }
| |_________^

error: this loop never actually loops
--> tests/ui/never_loop_fixable.rs:34:5
|
LL | / 'bar: for _ in 0..100 {
LL | |
LL | | loop {
... |
LL | | }
| |_____^
|
help: if you need the first element of the iterator, try writing
|
LL ~ if let Some(_) = (0..100).next() {
LL |
LL ~
|

error: this loop never actually loops
--> tests/ui/never_loop_fixable.rs:36:9
|
LL | / loop {
LL | |
LL | | break 'bar;
LL | | }
| |_________^

error: aborting due to 6 previous errors