rust/tests/ui/collapsible_if.rs

213 lines
4.3 KiB
Rust
Raw Normal View History

2018-10-06 18:18:06 +02:00
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2018-10-11 12:16:22 +02:00
2017-09-18 12:47:33 +02:00
2018-07-28 17:34:52 +02:00
#[warn(clippy::collapsible_if)]
fn main() {
let x = "hello";
let y = "world";
if x == "hello" {
if y == "world" {
println!("Hello world!");
}
}
if x == "hello" || x == "world" {
if y == "world" || y == "hello" {
println!("Hello world!");
}
}
2016-06-29 21:26:26 +02:00
if x == "hello" && x == "world" {
if y == "world" || y == "hello" {
println!("Hello world!");
}
}
if x == "hello" || x == "world" {
if y == "world" && y == "hello" {
println!("Hello world!");
}
}
if x == "hello" && x == "world" {
if y == "world" && y == "hello" {
println!("Hello world!");
}
}
if 42 == 1337 {
if 'a' != 'A' {
println!("world!")
}
}
2018-04-05 04:13:14 +02:00
// Collapse `else { if .. }` to `else if ..`
if x == "hello" {
print!("Hello ");
} else {
if y == "world" {
println!("world!")
}
}
if x == "hello" {
print!("Hello ");
} else {
if let Some(42) = Some(42) {
println!("world!")
}
}
if x == "hello" {
print!("Hello ");
} else {
if y == "world" {
println!("world")
}
else {
println!("!")
}
}
if x == "hello" {
print!("Hello ");
} else {
if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}
}
if let Some(42) = Some(42) {
print!("Hello ");
} else {
if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}
}
if let Some(42) = Some(42) {
print!("Hello ");
} else {
if x == "hello" {
println!("world")
}
else {
println!("!")
}
}
if let Some(42) = Some(42) {
print!("Hello ");
} else {
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`
}
2018-10-16 22:20:27 +02:00
// The following tests check for the fix of https://github.com/rust-lang-nursery/rust-clippy/issues/798
if x == "hello" {// Not collapsible
if y == "world" {
println!("Hello world!");
}
}
if x == "hello" { // Not collapsible
if y == "world" {
println!("Hello world!");
}
}
if x == "hello" {
// Not collapsible
if y == "world" {
println!("Hello world!");
}
}
if x == "hello" {
if y == "world" { // Collapsible
println!("Hello world!");
}
}
if x == "hello" {
print!("Hello ");
} else {
// Not collapsible
if y == "world" {
println!("world!")
}
}
if x == "hello" {
print!("Hello ");
} else {
// Not collapsible
if let Some(42) = Some(42) {
println!("world!")
}
}
if x == "hello" {
/* Not collapsible */
if y == "world" {
println!("Hello world!");
}
}
if x == "hello" { /* Not collapsible */
if y == "world" {
println!("Hello world!");
}
}
}