rust/tests/ui/diverging_sub_expression.rs

76 lines
2.2 KiB
Rust
Raw Normal View History

2018-07-28 10:34:52 -05:00
#![warn(clippy::diverging_sub_expression)]
#![allow(clippy::match_same_arms, clippy::overly_complex_bool_expr)]
#![allow(clippy::nonminimal_bool)]
2018-07-28 10:34:52 -05:00
#[allow(clippy::empty_loop)]
2018-12-09 16:26:16 -06:00
fn diverge() -> ! {
loop {}
}
2016-09-13 05:41:37 -05:00
struct A;
impl A {
2018-12-09 16:26:16 -06:00
fn foo(&self) -> ! {
diverge()
}
2016-09-13 05:41:37 -05:00
}
2018-07-28 10:34:52 -05:00
#[allow(unused_variables, clippy::unnecessary_operation, clippy::short_circuit_statement)]
fn main() {
let b = true;
2017-02-08 07:58:07 -06:00
b || diverge();
//~^ ERROR: sub-expression diverges
//~| NOTE: `-D clippy::diverging-sub-expression` implied by `-D warnings`
2017-02-08 07:58:07 -06:00
b || A.foo();
//~^ ERROR: sub-expression diverges
}
#[allow(dead_code, unused_variables)]
#[rustfmt::skip]
fn foobar() {
loop {
let x = match 5 {
4 => return,
5 => continue,
2017-02-08 07:58:07 -06:00
6 => true || return,
//~^ ERROR: sub-expression diverges
2017-02-08 07:58:07 -06:00
7 => true || continue,
//~^ ERROR: sub-expression diverges
8 => break,
9 => diverge(),
2017-02-08 07:58:07 -06:00
3 => true || diverge(),
//~^ ERROR: sub-expression diverges
10 => match 42 {
99 => return,
_ => true || panic!("boo"),
//~^ ERROR: sub-expression diverges
},
// lint blocks as well
15 => true || { return; },
//~^ ERROR: sub-expression diverges
16 => false || { return; },
//~^ ERROR: sub-expression diverges
// ... and when it's a single expression
17 => true || { return },
//~^ ERROR: sub-expression diverges
18 => false || { return },
//~^ ERROR: sub-expression diverges
// ... but not when there's both an expression and a statement
19 => true || { _ = 1; return },
20 => false || { _ = 1; return },
// ... or multiple statements
21 => true || { _ = 1; return; },
22 => false || { _ = 1; return; },
23 => true || { return; true },
24 => true || { return; true },
2017-02-08 07:58:07 -06:00
_ => true || break,
//~^ ERROR: sub-expression diverges
};
}
}
#[allow(unused)]
fn ignore_todo() {
let x: u32 = todo!();
println!("{x}");
}