rust-clippy/tests/ui/collapsible_if.rs

184 lines
2.7 KiB
Rust
Raw Normal View History

#![feature(plugin)]
#![plugin(clippy)]
#[deny(collapsible_if)]
fn main() {
let x = "hello";
let y = "world";
if x == "hello" {
2017-02-08 13:58:07 +00:00
if y == "world" {
println!("Hello world!");
}
}
if x == "hello" || x == "world" {
2017-02-08 13:58:07 +00:00
if y == "world" || y == "hello" {
println!("Hello world!");
}
}
2016-06-29 19:26:26 +00:00
if x == "hello" && x == "world" {
2017-02-08 13:58:07 +00:00
2016-06-29 19:26:26 +00:00
if y == "world" || y == "hello" {
println!("Hello world!");
}
}
if x == "hello" || x == "world" {
2017-02-08 13:58:07 +00:00
2016-06-29 19:26:26 +00:00
if y == "world" && y == "hello" {
println!("Hello world!");
}
}
if x == "hello" && x == "world" {
2017-02-08 13:58:07 +00:00
2016-06-29 19:26:26 +00:00
if y == "world" && y == "hello" {
println!("Hello world!");
}
}
if 42 == 1337 {
2017-02-08 13:58:07 +00:00
2016-06-29 19:26:26 +00:00
if 'a' != 'A' {
println!("world!")
}
}
// Collaspe `else { if .. }` to `else if ..`
if x == "hello" {
print!("Hello ");
} else {
2017-02-08 13:58:07 +00:00
if y == "world" {
println!("world!")
}
}
if x == "hello" {
print!("Hello ");
} else {
2017-02-08 13:58:07 +00:00
if let Some(42) = Some(42) {
println!("world!")
}
}
if x == "hello" {
print!("Hello ");
} else {
2017-02-08 13:58:07 +00:00
if y == "world" {
println!("world")
}
else {
println!("!")
}
}
if x == "hello" {
print!("Hello ");
} else {
2017-02-08 13:58:07 +00:00
if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}
}
if let Some(42) = Some(42) {
print!("Hello ");
} else {
2017-02-08 13:58:07 +00:00
if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}
}
if let Some(42) = Some(42) {
print!("Hello ");
} else {
2017-02-08 13:58:07 +00:00
if x == "hello" {
println!("world")
}
else {
println!("!")
}
}
if let Some(42) = Some(42) {
print!("Hello ");
} else {
2017-02-08 13:58:07 +00:00
if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}
}
// Works because any if with an else statement cannot be collapsed.
if x == "hello" {
if y == "world" {
println!("Hello world!");
}
} else {
println!("Not Hello world");
}
if x == "hello" {
if y == "world" {
println!("Hello world!");
} else {
println!("Hello something else");
}
}
if x == "hello" {
print!("Hello ");
if y == "world" {
println!("world!")
}
}
if true {
} else {
assert!(true); // assert! is just an `if`
}
}