Skip to content

Commit add9f0a

Browse files
committed
change the applicability of obfuscated_if_else depending on whether the original code can have side effects
1 parent 396de57 commit add9f0a

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

clippy_lints/src/methods/obfuscated_if_else.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::OBFUSCATED_IF_ELSE;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
34
use clippy_utils::source::snippet_with_applicability;
45
use rustc_errors::Applicability;
56
use rustc_hir as hir;
@@ -19,7 +20,12 @@ pub(super) fn check<'tcx>(
1920
let recv_ty = cx.typeck_results().expr_ty(then_recv);
2021

2122
if recv_ty.is_bool() {
22-
let mut applicability = Applicability::MachineApplicable;
23+
let mut applicability = if switch_to_eager_eval(cx, then_arg) && switch_to_eager_eval(cx, unwrap_arg) {
24+
Applicability::MachineApplicable
25+
} else {
26+
Applicability::MaybeIncorrect
27+
};
28+
2329
let sugg = format!(
2430
"if {} {{ {} }} else {{ {} }}",
2531
snippet_with_applicability(cx, then_recv.span, "..", &mut applicability),

tests/ui/obfuscated_if_else.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#![warn(clippy::obfuscated_if_else)]
2+
#![allow(clippy::unit_arg, clippy::unused_unit)]
23

34
fn main() {
45
if true { "a" } else { "b" };
6+
7+
let mut a = 0;
8+
if true { a += 1 } else { () };
9+
if true { () } else { a += 2 };
510
}

tests/ui/obfuscated_if_else.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#![warn(clippy::obfuscated_if_else)]
2+
#![allow(clippy::unit_arg, clippy::unused_unit)]
23

34
fn main() {
45
true.then_some("a").unwrap_or("b");
6+
7+
let mut a = 0;
8+
true.then_some(a += 1).unwrap_or(());
9+
true.then_some(()).unwrap_or(a += 2);
510
}

tests/ui/obfuscated_if_else.stderr

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
error: use of `.then_some(..).unwrap_or(..)` can be written more clearly with `if .. else ..`
2-
--> tests/ui/obfuscated_if_else.rs:4:5
2+
--> tests/ui/obfuscated_if_else.rs:5:5
33
|
44
LL | true.then_some("a").unwrap_or("b");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { "a" } else { "b" }`
66
|
77
= note: `-D clippy::obfuscated-if-else` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::obfuscated_if_else)]`
99

10-
error: aborting due to 1 previous error
10+
error: use of `.then_some(..).unwrap_or(..)` can be written more clearly with `if .. else ..`
11+
--> tests/ui/obfuscated_if_else.rs:8:5
12+
|
13+
LL | true.then_some(a += 1).unwrap_or(());
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { a += 1 } else { () }`
15+
16+
error: use of `.then_some(..).unwrap_or(..)` can be written more clearly with `if .. else ..`
17+
--> tests/ui/obfuscated_if_else.rs:9:5
18+
|
19+
LL | true.then_some(()).unwrap_or(a += 2);
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { () } else { a += 2 }`
21+
22+
error: aborting due to 3 previous errors
1123

0 commit comments

Comments
 (0)