]> git.lizzy.rs Git - rust.git/commitdiff
Fix suggestion spans for `NEEDLESS_RETURN`
authormcarton <cartonmartin+git@gmail.com>
Thu, 14 Jul 2016 16:32:09 +0000 (18:32 +0200)
committermcarton <cartonmartin+git@gmail.com>
Thu, 14 Jul 2016 16:32:48 +0000 (18:32 +0200)
clippy_lints/src/returns.rs
tests/compile-fail/needless_return.rs

index fda151cd6d7e1f186765647b1c450282782908fe..deea50f03039649a6fd1a2aabf6bd33fba375552 100644 (file)
@@ -39,7 +39,7 @@ fn check_block_return(&mut self, cx: &EarlyContext, block: &Block) {
         if let Some(stmt) = block.stmts.last() {
             match stmt.node {
                 StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => {
-                    self.check_final_expr(cx, expr);
+                    self.check_final_expr(cx, expr, Some(stmt.span));
                 }
                 _ => (),
             }
@@ -47,11 +47,11 @@ fn check_block_return(&mut self, cx: &EarlyContext, block: &Block) {
     }
 
     // Check a the final expression in a block if it's a return.
-    fn check_final_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
+    fn check_final_expr(&mut self, cx: &EarlyContext, expr: &Expr, span: Option<Span>) {
         match expr.node {
             // simple return is always "bad"
             ExprKind::Ret(Some(ref inner)) => {
-                self.emit_return_lint(cx, (expr.span, inner.span));
+                self.emit_return_lint(cx, span.expect("`else return` is not possible"), inner.span);
             }
             // a whole block? check it!
             ExprKind::Block(ref block) => {
@@ -62,25 +62,25 @@ fn check_final_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
             // (except for unit type functions) so we don't match it
             ExprKind::If(_, ref ifblock, Some(ref elsexpr)) => {
                 self.check_block_return(cx, ifblock);
-                self.check_final_expr(cx, elsexpr);
+                self.check_final_expr(cx, elsexpr, None);
             }
             // a match expr, check all arms
             ExprKind::Match(_, ref arms) => {
                 for arm in arms {
-                    self.check_final_expr(cx, &arm.body);
+                    self.check_final_expr(cx, &arm.body, Some(arm.body.span));
                 }
             }
             _ => (),
         }
     }
 
-    fn emit_return_lint(&mut self, cx: &EarlyContext, spans: (Span, Span)) {
-        if in_external_macro(cx, spans.1) {
+    fn emit_return_lint(&mut self, cx: &EarlyContext, ret_span: Span, inner_span: Span) {
+        if in_external_macro(cx, inner_span) {
             return;
         }
-        span_lint_and_then(cx, NEEDLESS_RETURN, spans.0, "unneeded return statement", |db| {
-            if let Some(snippet) = snippet_opt(cx, spans.1) {
-                db.span_suggestion(spans.0, "remove `return` as shown:", snippet);
+        span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded return statement", |db| {
+            if let Some(snippet) = snippet_opt(cx, inner_span) {
+                db.span_suggestion(ret_span, "remove `return` as shown:", snippet);
             }
         });
     }
index 80fed2818ef54505846ece77a7c50aee51d72772..5a391a358b1891a15492dad6165df6119ea2ab38 100644 (file)
@@ -37,12 +37,11 @@ fn test_if_block() -> bool {
 
 fn test_match(x: bool) -> bool {
     match x {
-        true => {
-            return false;
-            //~^ ERROR unneeded return statement
-            //~| HELP remove `return` as shown
-            //~| SUGGESTION false
-        }
+        true => return false,
+        //~^ ERROR unneeded return statement
+        //~| HELP remove `return` as shown
+        //~| SUGGESTION false
+
         false => {
             return true;
             //~^ ERROR unneeded return statement