From 1f75845a8f3318fba32aeb2f443f37aabdcc86f0 Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Sun, 21 Aug 2022 10:29:07 +0200 Subject: [PATCH] Reduce indentation and add comment about lint name --- clippy_lints/src/option_if_let_else.rs | 39 ++++++++++++++------------ tests/ui/option_if_let_else.stderr | 10 +++---- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/clippy_lints/src/option_if_let_else.rs b/clippy_lints/src/option_if_let_else.rs index 93388e78591..9602d0d1d2e 100644 --- a/clippy_lints/src/option_if_let_else.rs +++ b/clippy_lints/src/option_if_let_else.rs @@ -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, + ); } } } diff --git a/tests/ui/option_if_let_else.stderr b/tests/ui/option_if_let_else.stderr index 1bf513e0872..a5dbf6e1f22 100644 --- a/tests/ui/option_if_let_else.stderr +++ b/tests/ui/option_if_let_else.stderr @@ -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)`