rust/tests/ui/collapsible_if.fixed
Jason Newcomb 58132cb3b0 Improve SpanlessEq
* Don't consider expansions of different macros to be the same, even if they expand to the same tokens
* Don't consider `cfg!` expansions to be equal if they check different configs.
2023-05-18 16:42:13 -04:00

157 lines
3.2 KiB
Rust

//@run-rustfix
#![allow(
clippy::assertions_on_constants,
clippy::equatable_if_let,
clippy::nonminimal_bool,
clippy::eq_op
)]
#[rustfmt::skip]
#[warn(clippy::collapsible_if)]
fn main() {
let x = "hello";
let y = "world";
if x == "hello" && y == "world" {
println!("Hello world!");
}
if (x == "hello" || x == "world") && (y == "world" || y == "hello") {
println!("Hello world!");
}
if x == "hello" && x == "world" && (y == "world" || y == "hello") {
println!("Hello world!");
}
if (x == "hello" || x == "world") && y == "world" && y == "hello" {
println!("Hello world!");
}
if x == "hello" && x == "world" && y == "world" && y == "hello" {
println!("Hello world!");
}
if 42 == 1337 && 'a' != 'A' {
println!("world!")
}
// 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`
}
// The following tests check for the fix of https://github.com/rust-lang/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" && 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!");
}
}
// Test behavior wrt. `let_chains`.
// None of the cases below should be collapsed.
fn truth() -> bool { true }
// Prefix:
if let 0 = 1 {
if truth() {}
}
// Suffix:
if truth() {
if let 0 = 1 {}
}
// Midfix:
if truth() {
if let 0 = 1 {
if truth() {}
}
}
// Fix #5962
if matches!(true, true) && matches!(true, true) {}
// Issue #9375
if matches!(true, true) && truth() && matches!(true, true) {}
if true {
#[cfg(not(teehee))]
if true {
println!("Hello world!");
}
}
}