2023-07-02 12:35:19 +00:00
|
|
|
#![feature(inline_const)]
|
2018-12-09 22:26:16 +00:00
|
|
|
#![allow(
|
2023-07-02 12:35:19 +00:00
|
|
|
clippy::eq_op,
|
2018-12-09 22:26:16 +00:00
|
|
|
clippy::single_match,
|
|
|
|
unused_assignments,
|
|
|
|
unused_variables,
|
|
|
|
clippy::while_immutable_condition
|
|
|
|
)]
|
2023-08-24 19:32:12 +00:00
|
|
|
//@no-rustfix
|
2017-06-01 04:22:15 +00:00
|
|
|
fn test1() {
|
|
|
|
let mut x = 0;
|
2018-12-09 22:26:16 +00:00
|
|
|
loop {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this loop never actually loops
|
|
|
|
//~| NOTE: `#[deny(clippy::never_loop)]` on by default
|
2018-12-09 22:26:16 +00:00
|
|
|
// clippy::never_loop
|
2017-06-01 04:22:15 +00:00
|
|
|
x += 1;
|
|
|
|
if x == 1 {
|
2018-12-09 22:26:16 +00:00
|
|
|
return;
|
2017-06-01 04:22:15 +00:00
|
|
|
}
|
2017-02-17 03:53:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-05-31 01:44:01 +00:00
|
|
|
}
|
2017-02-17 03:53:14 +00:00
|
|
|
|
2017-06-01 04:22:15 +00:00
|
|
|
fn test2() {
|
|
|
|
let mut x = 0;
|
2017-02-17 03:53:14 +00:00
|
|
|
loop {
|
2017-06-01 04:22:15 +00:00
|
|
|
x += 1;
|
2017-02-17 03:53:14 +00:00
|
|
|
if x == 1 {
|
2018-12-09 22:26:16 +00:00
|
|
|
break;
|
2017-02-17 03:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-31 01:44:01 +00:00
|
|
|
}
|
2017-02-17 03:53:14 +00:00
|
|
|
|
2017-06-01 04:22:15 +00:00
|
|
|
fn test3() {
|
|
|
|
let mut x = 0;
|
2018-12-09 22:26:16 +00:00
|
|
|
loop {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this loop never actually loops
|
2018-12-09 22:26:16 +00:00
|
|
|
// never loops
|
2017-06-01 04:22:15 +00:00
|
|
|
x += 1;
|
2018-12-09 22:26:16 +00:00
|
|
|
break;
|
2017-02-17 03:53:14 +00:00
|
|
|
}
|
2017-05-31 01:44:01 +00:00
|
|
|
}
|
2017-02-17 03:53:14 +00:00
|
|
|
|
2017-06-01 04:22:15 +00:00
|
|
|
fn test4() {
|
|
|
|
let mut x = 1;
|
2017-02-17 03:53:14 +00:00
|
|
|
loop {
|
2017-06-01 04:22:15 +00:00
|
|
|
x += 1;
|
|
|
|
match x {
|
|
|
|
5 => return,
|
|
|
|
_ => (),
|
2017-02-17 03:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-31 01:44:01 +00:00
|
|
|
|
2017-06-01 04:22:15 +00:00
|
|
|
fn test5() {
|
|
|
|
let i = 0;
|
2018-12-09 22:26:16 +00:00
|
|
|
loop {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this loop never actually loops
|
2018-12-09 22:26:16 +00:00
|
|
|
// never loops
|
|
|
|
while i == 0 {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this loop never actually loops
|
2018-12-09 22:26:16 +00:00
|
|
|
// never loops
|
|
|
|
break;
|
2017-06-01 04:22:15 +00:00
|
|
|
}
|
2018-12-09 22:26:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-06-01 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test6() {
|
|
|
|
let mut x = 0;
|
2017-09-14 13:27:29 +00:00
|
|
|
'outer: loop {
|
2017-06-01 04:22:15 +00:00
|
|
|
x += 1;
|
2018-12-09 22:26:16 +00:00
|
|
|
loop {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this loop never actually loops
|
2018-12-09 22:26:16 +00:00
|
|
|
// never loops
|
|
|
|
if x == 5 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue 'outer;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-06-01 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test7() {
|
|
|
|
let mut x = 0;
|
2017-05-31 01:44:01 +00:00
|
|
|
loop {
|
2017-06-01 04:22:15 +00:00
|
|
|
x += 1;
|
2017-05-31 01:44:01 +00:00
|
|
|
match x {
|
2017-06-01 04:22:15 +00:00
|
|
|
1 => continue,
|
2017-05-31 01:44:01 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
2018-12-09 22:26:16 +00:00
|
|
|
return;
|
2017-06-01 04:22:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test8() {
|
|
|
|
let mut x = 0;
|
|
|
|
loop {
|
|
|
|
x += 1;
|
|
|
|
match x {
|
|
|
|
5 => return,
|
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test9() {
|
|
|
|
let x = Some(1);
|
2018-12-09 22:26:16 +00:00
|
|
|
while let Some(y) = x {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this loop never actually loops
|
2018-12-09 22:26:16 +00:00
|
|
|
// never loops
|
|
|
|
return;
|
2017-06-01 04:22:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test10() {
|
2018-12-09 22:26:16 +00:00
|
|
|
for x in 0..10 {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this loop never actually loops
|
2018-12-09 22:26:16 +00:00
|
|
|
// never loops
|
2017-06-01 04:22:15 +00:00
|
|
|
match x {
|
|
|
|
1 => break,
|
|
|
|
_ => return,
|
|
|
|
}
|
2017-05-31 01:44:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 01:56:05 +00:00
|
|
|
fn test11<F: FnMut() -> i32>(mut f: F) {
|
|
|
|
loop {
|
|
|
|
return match f() {
|
|
|
|
1 => continue,
|
|
|
|
_ => (),
|
2018-12-09 22:26:16 +00:00
|
|
|
};
|
2017-07-05 01:56:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:27:29 +00:00
|
|
|
pub fn test12(a: bool, b: bool) {
|
|
|
|
'label: loop {
|
|
|
|
loop {
|
|
|
|
if a {
|
|
|
|
continue 'label;
|
|
|
|
}
|
|
|
|
if b {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 04:46:08 +00:00
|
|
|
pub fn test13() {
|
|
|
|
let mut a = true;
|
2018-12-09 22:26:16 +00:00
|
|
|
loop {
|
|
|
|
// infinite loop
|
2017-10-06 04:46:08 +00:00
|
|
|
while a {
|
|
|
|
if true {
|
|
|
|
a = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 04:46:08 +00:00
|
|
|
pub fn test14() {
|
|
|
|
let mut a = true;
|
2018-12-09 22:26:16 +00:00
|
|
|
'outer: while a {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this loop never actually loops
|
2018-12-09 22:26:16 +00:00
|
|
|
// never loops
|
2017-10-06 04:46:08 +00:00
|
|
|
while a {
|
|
|
|
if a {
|
|
|
|
a = false;
|
2018-12-09 22:26:16 +00:00
|
|
|
continue;
|
2017-10-06 04:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break 'outer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-26 19:07:07 +00:00
|
|
|
// Issue #1991: the outer loop should not warn.
|
2017-11-05 14:43:28 +00:00
|
|
|
pub fn test15() {
|
|
|
|
'label: loop {
|
|
|
|
while false {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this loop never actually loops
|
2017-11-05 14:43:28 +00:00
|
|
|
break 'label;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 05:16:32 +00:00
|
|
|
// Issue #4058: `continue` in `break` expression
|
|
|
|
pub fn test16() {
|
|
|
|
let mut n = 1;
|
|
|
|
loop {
|
|
|
|
break if n != 5 {
|
|
|
|
n += 1;
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 15:39:06 +00:00
|
|
|
// Issue #9001: `continue` in struct expression fields
|
|
|
|
pub fn test17() {
|
|
|
|
struct Foo {
|
|
|
|
f: (),
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut n = 0;
|
|
|
|
let _ = loop {
|
|
|
|
break Foo {
|
|
|
|
f: if n < 5 {
|
|
|
|
n += 1;
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-06 07:44:38 +00:00
|
|
|
// Issue #9356: `continue` in else branch of let..else
|
|
|
|
pub fn test18() {
|
|
|
|
let x = Some(0);
|
|
|
|
let y = 0;
|
|
|
|
// might loop
|
|
|
|
let _ = loop {
|
|
|
|
let Some(x) = x else {
|
|
|
|
if y > 0 {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
break x;
|
|
|
|
};
|
|
|
|
// never loops
|
|
|
|
let _ = loop {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this loop never actually loops
|
2022-10-06 07:44:38 +00:00
|
|
|
let Some(x) = x else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
break x;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-21 19:34:47 +00:00
|
|
|
// Issue #9831: unconditional break to internal labeled block
|
|
|
|
pub fn test19() {
|
|
|
|
fn thing(iter: impl Iterator) {
|
|
|
|
for _ in iter {
|
|
|
|
'b: {
|
|
|
|
break 'b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test20() {
|
|
|
|
'a: loop {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this loop never actually loops
|
2022-11-21 19:34:47 +00:00
|
|
|
'b: {
|
|
|
|
break 'b 'c: {
|
|
|
|
break 'a;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: sub-expression diverges
|
|
|
|
//~| NOTE: `-D clippy::diverging-sub-expression` implied by `-D warnings`
|
2022-11-21 19:34:47 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-26 00:08:29 +00:00
|
|
|
pub fn test21() {
|
|
|
|
loop {
|
|
|
|
'a: {
|
|
|
|
{}
|
|
|
|
break 'a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue 10304: code after break from block was not considered
|
|
|
|
// unreachable code and was considered for further analysis of
|
|
|
|
// whether the loop would ever be executed or not.
|
|
|
|
pub fn test22() {
|
|
|
|
for _ in 0..10 {
|
|
|
|
'block: {
|
|
|
|
break 'block;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
println!("looped");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test23() {
|
|
|
|
for _ in 0..10 {
|
|
|
|
'block: {
|
|
|
|
for _ in 0..20 {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this loop never actually loops
|
2023-02-26 00:08:29 +00:00
|
|
|
break 'block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("looped");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test24() {
|
|
|
|
'a: for _ in 0..10 {
|
|
|
|
'b: {
|
|
|
|
let x = Some(1);
|
|
|
|
match x {
|
|
|
|
None => break 'a,
|
|
|
|
Some(_) => break 'b,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-02 12:35:19 +00:00
|
|
|
// Do not lint, we can evaluate `true` to always succeed thus can short-circuit before the `return`
|
|
|
|
pub fn test25() {
|
|
|
|
loop {
|
|
|
|
'label: {
|
|
|
|
if const { true } {
|
|
|
|
break 'label;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test26() {
|
|
|
|
loop {
|
|
|
|
'label: {
|
|
|
|
if 1 == 1 {
|
|
|
|
break 'label;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test27() {
|
|
|
|
loop {
|
|
|
|
'label: {
|
|
|
|
let x = true;
|
|
|
|
if x {
|
|
|
|
break 'label;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 16:13:53 +00:00
|
|
|
// issue 11004
|
|
|
|
pub fn test29() {
|
|
|
|
loop {
|
|
|
|
'label: {
|
|
|
|
if true {
|
|
|
|
break 'label;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test30() {
|
|
|
|
'a: loop {
|
|
|
|
'b: {
|
|
|
|
for j in 0..2 {
|
|
|
|
if j == 1 {
|
|
|
|
break 'b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break 'a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test31(b: bool) {
|
|
|
|
'a: loop {
|
|
|
|
'b: {
|
|
|
|
'c: loop {
|
|
|
|
//~^ ERROR: this loop never actually loops
|
|
|
|
if b { break 'c } else { break 'b }
|
|
|
|
}
|
|
|
|
continue 'a;
|
|
|
|
}
|
|
|
|
break 'a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test32() {
|
|
|
|
loop {
|
|
|
|
//~^ ERROR: this loop never actually loops
|
|
|
|
panic!("oh no");
|
|
|
|
}
|
|
|
|
loop {
|
|
|
|
//~^ ERROR: this loop never actually loops
|
|
|
|
unimplemented!("not yet");
|
|
|
|
}
|
|
|
|
loop {
|
|
|
|
// no error
|
|
|
|
todo!("maybe later");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 01:44:01 +00:00
|
|
|
fn main() {
|
2017-06-01 04:22:15 +00:00
|
|
|
test1();
|
|
|
|
test2();
|
|
|
|
test3();
|
|
|
|
test4();
|
|
|
|
test5();
|
|
|
|
test6();
|
|
|
|
test7();
|
|
|
|
test8();
|
|
|
|
test9();
|
|
|
|
test10();
|
2017-07-05 01:56:05 +00:00
|
|
|
test11(|| 0);
|
2017-09-14 13:27:29 +00:00
|
|
|
test12(true, false);
|
2017-10-06 04:46:08 +00:00
|
|
|
test13();
|
2017-10-06 04:46:08 +00:00
|
|
|
test14();
|
2017-05-31 01:44:01 +00:00
|
|
|
}
|