Skip to content

Commit 5fc81d0

Browse files
committed
change the applicability of obfuscated_if_else depending on whether the original code can have side effects
1 parent 9135923 commit 5fc81d0

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
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 clippy_utils::sugg::Sugg;
56
use rustc_errors::Applicability;
@@ -18,7 +19,12 @@ pub(super) fn check<'tcx>(
1819
let recv_ty = cx.typeck_results().expr_ty(then_recv);
1920

2021
if recv_ty.is_bool() {
21-
let mut applicability = Applicability::MachineApplicable;
22+
let mut applicability = if switch_to_eager_eval(cx, then_arg) && switch_to_eager_eval(cx, unwrap_arg) {
23+
Applicability::MachineApplicable
24+
} else {
25+
Applicability::MaybeIncorrect
26+
};
27+
2228
let if_then = match then_method_name {
2329
"then" if let ExprKind::Closure(closure) = then_arg.kind => {
2430
let body = cx.tcx.hir().body(closure.body);

tests/ui/obfuscated_if_else.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::obfuscated_if_else)]
22
#![allow(clippy::unnecessary_lazy_evaluations)]
3+
#![allow(clippy::unit_arg, clippy::unused_unit)]
34

45
fn main() {
56
if true { "a" } else { "b" };
@@ -11,4 +12,8 @@ fn main() {
1112

1213
let partial = (a == 1).then_some("a");
1314
partial.unwrap_or("b"); // not lint
15+
16+
let mut a = 0;
17+
if true { a += 1 } else { () };
18+
if true { () } else { a += 2 };
1419
}

tests/ui/obfuscated_if_else.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::obfuscated_if_else)]
22
#![allow(clippy::unnecessary_lazy_evaluations)]
3+
#![allow(clippy::unit_arg, clippy::unused_unit)]
34

45
fn main() {
56
true.then_some("a").unwrap_or("b");
@@ -11,4 +12,8 @@ fn main() {
1112

1213
let partial = (a == 1).then_some("a");
1314
partial.unwrap_or("b"); // not lint
15+
16+
let mut a = 0;
17+
true.then_some(a += 1).unwrap_or(());
18+
true.then_some(()).unwrap_or(a += 2);
1419
}

tests/ui/obfuscated_if_else.stderr

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this method chain can be written more clearly with `if .. else ..`
2-
--> tests/ui/obfuscated_if_else.rs:5:5
2+
--> tests/ui/obfuscated_if_else.rs:6:5
33
|
44
LL | true.then_some("a").unwrap_or("b");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { "a" } else { "b" }`
@@ -8,22 +8,34 @@ LL | true.then_some("a").unwrap_or("b");
88
= help: to override `-D warnings` add `#[allow(clippy::obfuscated_if_else)]`
99

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

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

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

28-
error: aborting due to 4 previous errors
28+
error: this method chain can be written more clearly with `if .. else ..`
29+
--> tests/ui/obfuscated_if_else.rs:17:5
30+
|
31+
LL | true.then_some(a += 1).unwrap_or(());
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { a += 1 } else { () }`
33+
34+
error: this method chain can be written more clearly with `if .. else ..`
35+
--> tests/ui/obfuscated_if_else.rs:18:5
36+
|
37+
LL | true.then_some(()).unwrap_or(a += 2);
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { () } else { a += 2 }`
39+
40+
error: aborting due to 6 previous errors
2941

0 commit comments

Comments
 (0)