diff options
| author | lapla-cogito <me@lapla.dev> | 2025-01-22 18:57:20 +0900 |
|---|---|---|
| committer | lapla-cogito <me@lapla.dev> | 2025-01-26 10:03:20 +0900 |
| commit | 2640f657ec8bd64276f85741d268e7a87e35de6e (patch) | |
| tree | a8a7242579a438794385dd2bc1b64ed423087abd | |
| parent | 913592373d40ec6c67774fcf32143bc548b4b6ac (diff) | |
| download | rust-2640f657ec8bd64276f85741d268e7a87e35de6e.tar.gz rust-2640f657ec8bd64276f85741d268e7a87e35de6e.zip | |
change the applicability of `obfuscated_if_else` depending on whether the original code can have side effects
| -rw-r--r-- | clippy_lints/src/methods/obfuscated_if_else.rs | 8 | ||||
| -rw-r--r-- | tests/ui/obfuscated_if_else.fixed | 6 | ||||
| -rw-r--r-- | tests/ui/obfuscated_if_else.rs | 6 | ||||
| -rw-r--r-- | tests/ui/obfuscated_if_else.stderr | 14 |
4 files changed, 30 insertions, 4 deletions
diff --git a/clippy_lints/src/methods/obfuscated_if_else.rs b/clippy_lints/src/methods/obfuscated_if_else.rs index 3e2cf113cb2..b71f79f8482 100644 --- a/clippy_lints/src/methods/obfuscated_if_else.rs +++ b/clippy_lints/src/methods/obfuscated_if_else.rs @@ -1,5 +1,6 @@ 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::source::snippet_with_applicability; use clippy_utils::sugg::Sugg; use rustc_errors::Applicability; @@ -18,7 +19,12 @@ pub(super) fn check<'tcx>( let recv_ty = cx.typeck_results().expr_ty(then_recv); if recv_ty.is_bool() { - let mut applicability = Applicability::MachineApplicable; + let mut applicability = if switch_to_eager_eval(cx, then_arg) && switch_to_eager_eval(cx, unwrap_arg) { + Applicability::MachineApplicable + } else { + Applicability::MaybeIncorrect + }; + let if_then = match then_method_name { "then" if let ExprKind::Closure(closure) = then_arg.kind => { let body = cx.tcx.hir().body(closure.body); diff --git a/tests/ui/obfuscated_if_else.fixed b/tests/ui/obfuscated_if_else.fixed index 11883a8989f..bfe1c5e10cf 100644 --- a/tests/ui/obfuscated_if_else.fixed +++ b/tests/ui/obfuscated_if_else.fixed @@ -1,5 +1,5 @@ #![warn(clippy::obfuscated_if_else)] -#![allow(clippy::unnecessary_lazy_evaluations)] +#![allow(clippy::unnecessary_lazy_evaluations, clippy::unit_arg, clippy::unused_unit)] fn main() { if true { "a" } else { "b" }; @@ -11,4 +11,8 @@ fn main() { let partial = (a == 1).then_some("a"); partial.unwrap_or("b"); // not lint + + let mut a = 0; + if true { a += 1 } else { () }; + if true { () } else { a += 2 }; } diff --git a/tests/ui/obfuscated_if_else.rs b/tests/ui/obfuscated_if_else.rs index 1f7896e0ffa..0ded2a2ceed 100644 --- a/tests/ui/obfuscated_if_else.rs +++ b/tests/ui/obfuscated_if_else.rs @@ -1,5 +1,5 @@ #![warn(clippy::obfuscated_if_else)] -#![allow(clippy::unnecessary_lazy_evaluations)] +#![allow(clippy::unnecessary_lazy_evaluations, clippy::unit_arg, clippy::unused_unit)] fn main() { true.then_some("a").unwrap_or("b"); @@ -11,4 +11,8 @@ fn main() { let partial = (a == 1).then_some("a"); partial.unwrap_or("b"); // not lint + + let mut a = 0; + true.then_some(a += 1).unwrap_or(()); + true.then_some(()).unwrap_or(a += 2); } diff --git a/tests/ui/obfuscated_if_else.stderr b/tests/ui/obfuscated_if_else.stderr index 33985d1111b..9ce1f475c48 100644 --- a/tests/ui/obfuscated_if_else.stderr +++ b/tests/ui/obfuscated_if_else.stderr @@ -25,5 +25,17 @@ error: this method chain can be written more clearly with `if .. else ..` LL | (a == 1).then(|| "a").unwrap_or("b"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if a == 1 { "a" } else { "b" }` -error: aborting due to 4 previous errors +error: this method chain can be written more clearly with `if .. else ..` + --> tests/ui/obfuscated_if_else.rs:16: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 + | +LL | true.then_some(()).unwrap_or(a += 2); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { () } else { a += 2 }` + +error: aborting due to 6 previous errors |
