]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/if_then_some_else_none.rs
Improve `implicit_return`
[rust.git] / clippy_lints / src / if_then_some_else_none.rs
index aadadd0d934a573d25be4f2c6a79b533f76f5351..85c95f1151f84b53db10ffdacc68cc98cdd92783 100644 (file)
@@ -1,6 +1,8 @@
-use crate::utils;
+use clippy_utils::diagnostics::span_lint_and_help;
+use clippy_utils::source::snippet_with_macro_callsite;
+use clippy_utils::{is_else_clause, is_lang_ctor, meets_msrv};
 use if_chain::if_chain;
-use rustc_errors::Applicability;
+use rustc_hir::LangItem::{OptionNone, OptionSome};
 use rustc_hir::{Expr, ExprKind};
 use rustc_lint::{LateContext, LateLintPass, LintContext};
 use rustc_middle::lint::in_external_macro;
@@ -57,7 +59,7 @@ pub fn new(msrv: Option<RustcVersion>) -> Self {
 
 impl LateLintPass<'_> for IfThenSomeElseNone {
     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
-        if !utils::meets_msrv(self.msrv.as_ref(), &IF_THEN_SOME_ELSE_NONE_MSRV) {
+        if !meets_msrv(self.msrv.as_ref(), &IF_THEN_SOME_ELSE_NONE_MSRV) {
             return;
         }
 
@@ -66,39 +68,47 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
         }
 
         // We only care about the top-most `if` in the chain
-        if utils::parent_node_is_if_expr(expr, cx) {
+        if is_else_clause(cx.tcx, expr) {
             return;
         }
 
         if_chain! {
-            if let ExprKind::If(ref cond, ref then, Some(ref els)) = expr.kind;
-            if let ExprKind::Block(ref then_block, _) = then.kind;
-            if let Some(ref then_expr) = then_block.expr;
-            if let ExprKind::Call(ref then_call, [then_arg]) = then_expr.kind;
+            if let ExprKind::If(cond, then, Some(els)) = expr.kind;
+            if let ExprKind::Block(then_block, _) = then.kind;
+            if let Some(then_expr) = then_block.expr;
+            if let ExprKind::Call(then_call, [then_arg]) = then_expr.kind;
             if let ExprKind::Path(ref then_call_qpath) = then_call.kind;
-            if utils::match_qpath(then_call_qpath, &utils::paths::OPTION_SOME);
-            if let ExprKind::Block(ref els_block, _) = els.kind;
+            if is_lang_ctor(cx, then_call_qpath, OptionSome);
+            if let ExprKind::Block(els_block, _) = els.kind;
             if els_block.stmts.is_empty();
-            if let Some(ref els_expr) = els_block.expr;
-            if let ExprKind::Path(ref els_call_qpath) = els_expr.kind;
-            if utils::match_qpath(els_call_qpath, &utils::paths::OPTION_NONE);
+            if let Some(els_expr) = els_block.expr;
+            if let ExprKind::Path(ref qpath) = els_expr.kind;
+            if is_lang_ctor(cx, qpath, OptionNone);
             then {
-                let mut applicability = Applicability::MachineApplicable;
-                let cond_snip = utils::snippet_with_applicability(cx, cond.span, "[condition]", &mut applicability);
-                let arg_snip = utils::snippet_with_applicability(cx, then_arg.span, "", &mut applicability);
-                let sugg = format!(
-                    "{}.then(|| {{ /* snippet */ {} }})",
+                let cond_snip = snippet_with_macro_callsite(cx, cond.span, "[condition]");
+                let cond_snip = if matches!(cond.kind, ExprKind::Unary(_, _) | ExprKind::Binary(_, _, _)) {
+                    format!("({})", cond_snip)
+                } else {
+                    cond_snip.into_owned()
+                };
+                let arg_snip = snippet_with_macro_callsite(cx, then_arg.span, "");
+                let closure_body = if then_block.stmts.is_empty() {
+                    arg_snip.into_owned()
+                } else {
+                    format!("{{ /* snippet */ {} }}", arg_snip)
+                };
+                let help = format!(
+                    "consider using `bool::then` like: `{}.then(|| {})`",
                     cond_snip,
-                    arg_snip,
+                    closure_body,
                 );
-                utils::span_lint_and_sugg(
+                span_lint_and_help(
                     cx,
                     IF_THEN_SOME_ELSE_NONE,
                     expr.span,
                     "this could be simplified with `bool::then`",
-                    "try this",
-                    sugg,
-                    applicability,
+                    None,
+                    &help,
                 );
             }
         }