-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
trigger
obfuscated_if_else
for .then(..).unwrap_or(..)
- Loading branch information
1 parent
e359e88
commit f42093c
Showing
5 changed files
with
59 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
#![warn(clippy::obfuscated_if_else)] | ||
#![allow(clippy::unnecessary_lazy_evaluations)] | ||
|
||
fn main() { | ||
if true { "a" } else { "b" }; | ||
if true { "a" } else { "b" }; | ||
|
||
let a = 1; | ||
if a == 1 { "a" } else { "b" }; | ||
if a == 1 { "a" } else { "b" }; | ||
|
||
let partial = (a == 1).then_some("a"); | ||
partial.unwrap_or("b"); // not lint | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
#![warn(clippy::obfuscated_if_else)] | ||
#![allow(clippy::unnecessary_lazy_evaluations)] | ||
|
||
fn main() { | ||
true.then_some("a").unwrap_or("b"); | ||
true.then(|| "a").unwrap_or("b"); | ||
|
||
let a = 1; | ||
(a == 1).then_some("a").unwrap_or("b"); | ||
(a == 1).then(|| "a").unwrap_or("b"); | ||
|
||
let partial = (a == 1).then_some("a"); | ||
partial.unwrap_or("b"); // not lint | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,29 @@ | ||
error: use of `.then_some(..).unwrap_or(..)` can be written more clearly with `if .. else ..` | ||
--> tests/ui/obfuscated_if_else.rs:4:5 | ||
error: use of method chains can be written more clearly with `if .. else ..` | ||
--> tests/ui/obfuscated_if_else.rs:5:5 | ||
| | ||
LL | true.then_some("a").unwrap_or("b"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { "a" } else { "b" }` | ||
| | ||
= note: `-D clippy::obfuscated-if-else` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::obfuscated_if_else)]` | ||
|
||
error: aborting due to 1 previous error | ||
error: use of method chains can be written more clearly with `if .. else ..` | ||
--> tests/ui/obfuscated_if_else.rs:6:5 | ||
| | ||
LL | true.then(|| "a").unwrap_or("b"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if true { "a" } else { "b" }` | ||
|
||
error: use of method chains can be written more clearly with `if .. else ..` | ||
--> tests/ui/obfuscated_if_else.rs:9:5 | ||
| | ||
LL | (a == 1).then_some("a").unwrap_or("b"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if a == 1 { "a" } else { "b" }` | ||
|
||
error: use of method chains can be written more clearly with `if .. else ..` | ||
--> tests/ui/obfuscated_if_else.rs:10:5 | ||
| | ||
LL | (a == 1).then(|| "a").unwrap_or("b"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if a == 1 { "a" } else { "b" }` | ||
|
||
error: aborting due to 4 previous errors | ||
|