Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix obfuscated_if_else suggestion on left side of a binary expr #14124

Merged
merged 2 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions clippy_lints/src/methods/obfuscated_if_else.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::OBFUSCATED_IF_ELSE;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
use clippy_utils::get_parent_expr;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -41,6 +42,17 @@ pub(super) fn check<'tcx>(
snippet_with_applicability(cx, unwrap_arg.span, "..", &mut applicability)
);

// To be parsed as an expression, the `if { … } else { … }` as the left operand of a binary operator
// requires parentheses.
let sugg = if let Some(parent_expr) = get_parent_expr(cx, expr)
&& let ExprKind::Binary(_, left, _) = parent_expr.kind
&& left.hir_id == expr.hir_id
{
format!("({sugg})")
} else {
sugg
};

span_lint_and_sugg(
cx,
OBFUSCATED_IF_ELSE,
Expand Down
32 changes: 32 additions & 0 deletions tests/ui/obfuscated_if_else.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,48 @@

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 ..`
}
32 changes: 32 additions & 0 deletions tests/ui/obfuscated_if_else.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the //~ ui_test annotations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be ok now

Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,48 @@

fn main() {
true.then_some("a").unwrap_or("b");
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
true.then(|| "a").unwrap_or("b");
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`

let a = 1;
(a == 1).then_some("a").unwrap_or("b");
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
(a == 1).then(|| "a").unwrap_or("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;
true.then_some(a += 1).unwrap_or(());
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
true.then_some(()).unwrap_or(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 _ = true.then_some(40).unwrap_or(17) | 2;
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`

// Parentheses are required only for the leftmost expression
let _ = true.then_some(30).unwrap_or(17) | true.then_some(2).unwrap_or(3) | true.then_some(10).unwrap_or(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 | true.then_some(40).unwrap_or(17);
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`

// Parentheses are not required for a cast
let _ = true.then_some(42).unwrap_or(17) as u8;
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`

// Parentheses are not required for a deref
let _ = *true.then_some(&42).unwrap_or(&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 _ = *true.then_some(&42).unwrap_or(&17) as u8;
//~^ ERROR: this method chain can be written more clearly with `if .. else ..`
}
60 changes: 54 additions & 6 deletions tests/ui/obfuscated_if_else.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,82 @@ LL | true.then_some("a").unwrap_or("b");
= help: to override `-D warnings` add `#[allow(clippy::obfuscated_if_else)]`

error: this method chain can be written more clearly with `if .. else ..`
--> tests/ui/obfuscated_if_else.rs:6:5
--> tests/ui/obfuscated_if_else.rs:7:5
|
LL | true.then(|| "a").unwrap_or("b");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { "a" } else { "b" }`

error: this method chain can be written more clearly with `if .. else ..`
--> tests/ui/obfuscated_if_else.rs:9:5
--> tests/ui/obfuscated_if_else.rs:11:5
|
LL | (a == 1).then_some("a").unwrap_or("b");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if a == 1 { "a" } else { "b" }`

error: this method chain can be written more clearly with `if .. else ..`
--> tests/ui/obfuscated_if_else.rs:10:5
--> tests/ui/obfuscated_if_else.rs:13:5
|
LL | (a == 1).then(|| "a").unwrap_or("b");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if a == 1 { "a" } else { "b" }`

error: this method chain can be written more clearly with `if .. else ..`
--> tests/ui/obfuscated_if_else.rs:16:5
--> tests/ui/obfuscated_if_else.rs:20:5
|
LL | true.then_some(a += 1).unwrap_or(());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { a += 1 } else { () }`

error: this method chain can be written more clearly with `if .. else ..`
--> tests/ui/obfuscated_if_else.rs:17:5
--> tests/ui/obfuscated_if_else.rs:22:5
|
LL | true.then_some(()).unwrap_or(a += 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { () } else { a += 2 }`

error: aborting due to 6 previous errors
error: this method chain can be written more clearly with `if .. else ..`
--> tests/ui/obfuscated_if_else.rs:28:13
|
LL | let _ = true.then_some(40).unwrap_or(17) | 2;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(if true { 40 } else { 17 })`

error: this method chain can be written more clearly with `if .. else ..`
--> tests/ui/obfuscated_if_else.rs:32:13
|
LL | let _ = true.then_some(30).unwrap_or(17) | true.then_some(2).unwrap_or(3) | true.then_some(10).unwrap_or(1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(if true { 30 } else { 17 })`

error: this method chain can be written more clearly with `if .. else ..`
--> tests/ui/obfuscated_if_else.rs:32:48
|
LL | let _ = true.then_some(30).unwrap_or(17) | true.then_some(2).unwrap_or(3) | true.then_some(10).unwrap_or(1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { 2 } else { 3 }`

error: this method chain can be written more clearly with `if .. else ..`
--> tests/ui/obfuscated_if_else.rs:32:81
|
LL | let _ = true.then_some(30).unwrap_or(17) | true.then_some(2).unwrap_or(3) | true.then_some(10).unwrap_or(1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { 10 } else { 1 }`

error: this method chain can be written more clearly with `if .. else ..`
--> tests/ui/obfuscated_if_else.rs:36:17
|
LL | let _ = 2 | true.then_some(40).unwrap_or(17);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { 40 } else { 17 }`

error: this method chain can be written more clearly with `if .. else ..`
--> tests/ui/obfuscated_if_else.rs:40:13
|
LL | let _ = true.then_some(42).unwrap_or(17) as u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { 42 } else { 17 }`

error: this method chain can be written more clearly with `if .. else ..`
--> tests/ui/obfuscated_if_else.rs:44:14
|
LL | let _ = *true.then_some(&42).unwrap_or(&17);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { &42 } else { &17 }`

error: this method chain can be written more clearly with `if .. else ..`
--> tests/ui/obfuscated_if_else.rs:48:14
|
LL | let _ = *true.then_some(&42).unwrap_or(&17) as u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { &42 } else { &17 }`

error: aborting due to 14 previous errors