-
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.
Auto merge of #9148 - arieluy:then_some_unwrap_or, r=Jarcho
Add new lint `obfuscated_if_else` part of #9100, additional commits could make it work with `then` and `unwrap_or_else` as well changelog: Add new lint `obfuscated_if_else`
- Loading branch information
Showing
9 changed files
with
104 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// run-rustfix | ||
|
||
use super::OBFUSCATED_IF_ELSE; | ||
use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet_with_applicability}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx hir::Expr<'_>, | ||
then_recv: &'tcx hir::Expr<'_>, | ||
then_arg: &'tcx hir::Expr<'_>, | ||
unwrap_arg: &'tcx hir::Expr<'_>, | ||
) { | ||
// something.then_some(blah).unwrap_or(blah) | ||
// ^^^^^^^^^-then_recv ^^^^-then_arg ^^^^- unwrap_arg | ||
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- expr | ||
|
||
let recv_ty = cx.typeck_results().expr_ty(then_recv); | ||
|
||
if recv_ty.is_bool() { | ||
let mut applicability = Applicability::MachineApplicable; | ||
let sugg = format!( | ||
"if {} {{ {} }} else {{ {} }}", | ||
snippet_with_applicability(cx, then_recv.span, "..", &mut applicability), | ||
snippet_with_applicability(cx, then_arg.span, "..", &mut applicability), | ||
snippet_with_applicability(cx, unwrap_arg.span, "..", &mut applicability) | ||
); | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
OBFUSCATED_IF_ELSE, | ||
expr.span, | ||
"use of `.then_some(..).unwrap_or(..)` can be written \ | ||
more clearly with `if .. else ..`", | ||
"try", | ||
sugg, | ||
applicability, | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::obfuscated_if_else)] | ||
|
||
fn main() { | ||
if true { "a" } else { "b" }; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::obfuscated_if_else)] | ||
|
||
fn main() { | ||
true.then_some("a").unwrap_or("b"); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error: use of `.then_some(..).unwrap_or(..)` can be written more clearly with `if .. else ..` | ||
--> $DIR/obfuscated_if_else.rs:6: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` | ||
|
||
error: aborting due to previous error | ||
|