Reduce indentation and add comment about lint name

This commit is contained in:
Philipp Krones 2022-08-21 10:29:07 +02:00
parent ffe7125163
commit 1f75845a8f
No known key found for this signature in database
GPG Key ID: 1CA0DF2AF59D68A5
2 changed files with 26 additions and 23 deletions

View File

@ -64,6 +64,8 @@ declare_clippy_lint! {
/// y*y
/// }, |foo| foo);
/// ```
// FIXME: Before moving this lint out of nursery, the lint name needs to be updated. It now also
// covers matches and `Result`.
#[clippy::version = "1.47.0"]
pub OPTION_IF_LET_ELSE,
nursery,
@ -235,24 +237,25 @@ fn is_none_or_err_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool {
impl<'tcx> LateLintPass<'tcx> for OptionIfLetElse {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
// Don't lint macros, because it behaves weirdly
// and don't lint constant as well
if !expr.span.from_expansion() && !in_constant(cx, expr.hir_id) {
let detection = detect_option_if_let_else(cx, expr).or_else(|| detect_option_match(cx, expr));
if let Some(det) = detection {
span_lint_and_sugg(
cx,
OPTION_IF_LET_ELSE,
expr.span,
format!("use Option::{} instead of an if let/else", det.method_sugg).as_str(),
"try",
format!(
"{}.{}({}, {})",
det.option, det.method_sugg, det.none_expr, det.some_expr
),
Applicability::MaybeIncorrect,
);
}
// Don't lint macros and constants
if expr.span.from_expansion() || in_constant(cx, expr.hir_id) {
return;
}
let detection = detect_option_if_let_else(cx, expr).or_else(|| detect_option_match(cx, expr));
if let Some(det) = detection {
span_lint_and_sugg(
cx,
OPTION_IF_LET_ELSE,
expr.span,
format!("use Option::{} instead of an if let/else", det.method_sugg).as_str(),
"try",
format!(
"{}.{}({}, {})",
det.option, det.method_sugg, det.none_expr, det.some_expr
),
Applicability::MaybeIncorrect,
);
}
}
}

View File

@ -207,7 +207,7 @@ LL ~ });
|
error: use Option::map_or instead of an if let/else
--> $DIR/option_if_let_else.rs:212:13
--> $DIR/option_if_let_else.rs:213:13
|
LL | let _ = match s {
| _____________^
@ -217,7 +217,7 @@ LL | | };
| |_____^ help: try: `s.map_or(1, |string| string.len())`
error: use Option::map_or instead of an if let/else
--> $DIR/option_if_let_else.rs:216:13
--> $DIR/option_if_let_else.rs:217:13
|
LL | let _ = match Some(10) {
| _____________^
@ -227,7 +227,7 @@ LL | | };
| |_____^ help: try: `Some(10).map_or(5, |a| a + 1)`
error: use Option::map_or instead of an if let/else
--> $DIR/option_if_let_else.rs:222:13
--> $DIR/option_if_let_else.rs:223:13
|
LL | let _ = match res {
| _____________^
@ -237,7 +237,7 @@ LL | | };
| |_____^ help: try: `res.map_or(1, |a| a + 1)`
error: use Option::map_or instead of an if let/else
--> $DIR/option_if_let_else.rs:226:13
--> $DIR/option_if_let_else.rs:227:13
|
LL | let _ = match res {
| _____________^
@ -247,7 +247,7 @@ LL | | };
| |_____^ help: try: `res.map_or(1, |a| a + 1)`
error: use Option::map_or instead of an if let/else
--> $DIR/option_if_let_else.rs:230:13
--> $DIR/option_if_let_else.rs:231:13
|
LL | let _ = if let Ok(a) = res { a + 1 } else { 5 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `res.map_or(5, |a| a + 1)`