rust/tests/ui/never_loop.rs

205 lines
2.9 KiB
Rust
Raw Normal View History

2018-12-09 23:26:16 +01:00
#![allow(
clippy::single_match,
unused_assignments,
unused_variables,
clippy::while_immutable_condition
)]
2017-06-01 06:22:15 +02:00
fn test1() {
let mut x = 0;
2018-12-09 23:26:16 +01:00
loop {
// clippy::never_loop
2017-06-01 06:22:15 +02:00
x += 1;
if x == 1 {
2018-12-09 23:26:16 +01:00
return;
2017-06-01 06:22:15 +02:00
}
break;
}
2017-05-31 03:44:01 +02:00
}
2017-06-01 06:22:15 +02:00
fn test2() {
let mut x = 0;
loop {
2017-06-01 06:22:15 +02:00
x += 1;
if x == 1 {
2018-12-09 23:26:16 +01:00
break;
}
}
2017-05-31 03:44:01 +02:00
}
2017-06-01 06:22:15 +02:00
fn test3() {
let mut x = 0;
2018-12-09 23:26:16 +01:00
loop {
// never loops
2017-06-01 06:22:15 +02:00
x += 1;
2018-12-09 23:26:16 +01:00
break;
}
2017-05-31 03:44:01 +02:00
}
2017-06-01 06:22:15 +02:00
fn test4() {
let mut x = 1;
loop {
2017-06-01 06:22:15 +02:00
x += 1;
match x {
5 => return,
_ => (),
}
}
}
2017-05-31 03:44:01 +02:00
2017-06-01 06:22:15 +02:00
fn test5() {
let i = 0;
2018-12-09 23:26:16 +01:00
loop {
// never loops
while i == 0 {
// never loops
break;
2017-06-01 06:22:15 +02:00
}
2018-12-09 23:26:16 +01:00
return;
}
2017-06-01 06:22:15 +02:00
}
fn test6() {
let mut x = 0;
2017-09-14 15:27:29 +02:00
'outer: loop {
2017-06-01 06:22:15 +02:00
x += 1;
2018-12-09 23:26:16 +01:00
loop {
// never loops
if x == 5 {
break;
}
continue 'outer;
}
return;
}
2017-06-01 06:22:15 +02:00
}
fn test7() {
let mut x = 0;
2017-05-31 03:44:01 +02:00
loop {
2017-06-01 06:22:15 +02:00
x += 1;
2017-05-31 03:44:01 +02:00
match x {
2017-06-01 06:22:15 +02:00
1 => continue,
2017-05-31 03:44:01 +02:00
_ => (),
}
2018-12-09 23:26:16 +01:00
return;
2017-06-01 06:22:15 +02:00
}
}
fn test8() {
let mut x = 0;
loop {
x += 1;
match x {
5 => return,
_ => continue,
}
}
}
fn test9() {
let x = Some(1);
2018-12-09 23:26:16 +01:00
while let Some(y) = x {
// never loops
return;
2017-06-01 06:22:15 +02:00
}
}
fn test10() {
2018-12-09 23:26:16 +01:00
for x in 0..10 {
// never loops
2017-06-01 06:22:15 +02:00
match x {
1 => break,
_ => return,
}
2017-05-31 03:44:01 +02:00
}
}
2017-07-05 03:56:05 +02:00
fn test11<F: FnMut() -> i32>(mut f: F) {
loop {
return match f() {
1 => continue,
_ => (),
2018-12-09 23:26:16 +01:00
};
2017-07-05 03:56:05 +02:00
}
}
2017-09-14 15:27:29 +02:00
pub fn test12(a: bool, b: bool) {
'label: loop {
loop {
if a {
continue 'label;
}
if b {
break;
}
}
break;
}
}
2017-10-06 06:46:08 +02:00
pub fn test13() {
let mut a = true;
2018-12-09 23:26:16 +01:00
loop {
// infinite loop
2017-10-06 06:46:08 +02:00
while a {
if true {
a = false;
continue;
}
return;
}
}
}
2017-10-06 06:46:08 +02:00
pub fn test14() {
let mut a = true;
2018-12-09 23:26:16 +01:00
'outer: while a {
// never loops
2017-10-06 06:46:08 +02:00
while a {
if a {
a = false;
2018-12-09 23:26:16 +01:00
continue;
2017-10-06 06:46:08 +02:00
}
}
break 'outer;
}
}
// Issue #1991: the outter loop should not warn.
pub fn test15() {
'label: loop {
while false {
break 'label;
}
}
}
// Issue #4058: `continue` in `break` expression
pub fn test16() {
let mut n = 1;
loop {
break if n != 5 {
n += 1;
continue;
};
}
}
2017-05-31 03:44:01 +02:00
fn main() {
2017-06-01 06:22:15 +02:00
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
test10();
2017-07-05 03:56:05 +02:00
test11(|| 0);
2017-09-14 15:27:29 +02:00
test12(true, false);
2017-10-06 06:46:08 +02:00
test13();
2017-10-06 06:46:08 +02:00
test14();
2017-05-31 03:44:01 +02:00
}