Skip to content

Commit 8e31f2f

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

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

clippy_lints/src/methods/obfuscated_if_else.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ pub(super) fn check<'tcx>(
1919
let recv_ty = cx.typeck_results().expr_ty(then_recv);
2020

2121
if recv_ty.is_bool() {
22-
let mut applicability = Applicability::MachineApplicable;
22+
let mut applicability = if then_arg.can_have_side_effects() || unwrap_arg.can_have_side_effects() {
23+
Applicability::MaybeIncorrect
24+
} else {
25+
Applicability::MachineApplicable
26+
};
27+
2328
let sugg = format!(
2429
"if {} {{ {} }} else {{ {} }}",
2530
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)]
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)]
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)