]> git.lizzy.rs Git - rust.git/commitdiff
Fix false-positive in `LET_AND_RETURN`
authormcarton <cartonmartin+git@gmail.com>
Wed, 8 Jun 2016 22:24:44 +0000 (00:24 +0200)
committermcarton <cartonmartin+git@gmail.com>
Thu, 9 Jun 2016 21:34:16 +0000 (23:34 +0200)
If the declaration has a type, it might be required for coercion to
happen.

clippy_lints/src/returns.rs

index d78938212634ba6e39366f1689a500390880ce0f..7bb468166b8634f25918c9ff84477b444676eed6 100644 (file)
@@ -3,7 +3,7 @@
 use syntax::codemap::{Span, Spanned};
 use syntax::visit::FnKind;
 
-use utils::{span_lint, span_lint_and_then, snippet_opt, match_path_ast, in_external_macro};
+use utils::{span_note_and_lint, span_lint_and_then, snippet_opt, match_path_ast, in_external_macro};
 
 /// **What it does:** This lint checks for return statements at the end of a block.
 ///
@@ -95,29 +95,23 @@ fn check_let_return(&mut self, cx: &EarlyContext, block: &Block) {
                 let Some(ref retexpr) = block.expr,
                 let StmtKind::Decl(ref decl, _) = stmt.node,
                 let DeclKind::Local(ref local) = decl.node,
+                local.ty.is_none(),
                 let Some(ref initexpr) = local.init,
                 let PatKind::Ident(_, Spanned { node: id, .. }, _) = local.pat.node,
                 let ExprKind::Path(_, ref path) = retexpr.node,
-                match_path_ast(path, &[&id.name.as_str()])
+                match_path_ast(path, &[&id.name.as_str()]),
+                !in_external_macro(cx, initexpr.span),
             ], {
-                self.emit_let_lint(cx, retexpr.span, initexpr.span);
+                span_note_and_lint(cx,
+                                   LET_AND_RETURN,
+                                   retexpr.span,
+                                   "returning the result of a let binding from a block. \
+                                   Consider returning the expression directly.",
+                                   initexpr.span,
+                                   "this expression can be directly returned");
             }
         }
     }
-
-    fn emit_let_lint(&mut self, cx: &EarlyContext, lint_span: Span, note_span: Span) {
-        if in_external_macro(cx, note_span) {
-            return;
-        }
-        let mut db = span_lint(cx,
-                               LET_AND_RETURN,
-                               lint_span,
-                               "returning the result of a let binding from a block. Consider returning the \
-                                expression directly.");
-        if cx.current_level(LET_AND_RETURN) != Level::Allow {
-            db.span_note(note_span, "this expression can be directly returned");
-        }
-    }
 }
 
 impl LintPass for ReturnPass {