rust/tests/ui/obfuscated_if_else.fixed
Samuel Tardieu ac0a11a8bc Fix obfuscated_if_else suggestion on left side of a binary expr
An `if … { … } else { … }` used as the left operand of a binary
expression requires parentheses to be parsed as an expression.
2025-02-09 02:11:37 +01:00

50 lines
2.2 KiB
Rust

#![warn(clippy::obfuscated_if_else)]
#![allow(clippy::unnecessary_lazy_evaluations, clippy::unit_arg, clippy::unused_unit)]
fn main() {
if true { "a" } else { "b" };
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
if true { "a" } else { "b" };
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
let a = 1;
if a == 1 { "a" } else { "b" };
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
if a == 1 { "a" } else { "b" };
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
let partial = (a == 1).then_some("a");
partial.unwrap_or("b"); // not lint
let mut a = 0;
if true { a += 1 } else { () };
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
if true { () } else { a += 2 };
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
}
fn issue11141() {
// Parentheses are required around the left side of a binary expression
let _ = (if true { 40 } else { 17 }) | 2;
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
// Parentheses are required only for the leftmost expression
let _ = (if true { 30 } else { 17 }) | if true { 2 } else { 3 } | if true { 10 } else { 1 };
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
// Parentheses are not required around the right side of a binary expression
let _ = 2 | if true { 40 } else { 17 };
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
// Parentheses are not required for a cast
let _ = if true { 42 } else { 17 } as u8;
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
// Parentheses are not required for a deref
let _ = *if true { &42 } else { &17 };
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
// Parentheses are not required for a deref followed by a cast
let _ = *if true { &42 } else { &17 } as u8;
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
}