From 974ab43453514d85bc0bb3ffa4ee44f0b0e6204e Mon Sep 17 00:00:00 2001
From: Seo Sanghyeon <sanxiyn@gmail.com>
Date: Fri, 11 Dec 2015 16:28:05 +0900
Subject: [PATCH] Use suggestion for needless_return

---
 src/returns.rs                        | 17 +++++++++++------
 tests/compile-fail/needless_return.rs | 12 +++++++++---
 2 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/src/returns.rs b/src/returns.rs
index 3df4efd0889..7347558b3a7 100644
--- a/src/returns.rs
+++ b/src/returns.rs
@@ -4,7 +4,7 @@ use syntax::ast::*;
 use syntax::codemap::{Span, Spanned};
 use syntax::visit::FnKind;
 
-use utils::{span_lint, snippet, match_path_ast, in_external_macro};
+use utils::{span_lint, span_lint_and_then, snippet_opt, match_path_ast, in_external_macro};
 
 declare_lint!(pub NEEDLESS_RETURN, Warn,
               "using a return statement like `return expr;` where an expression would suffice");
@@ -23,7 +23,7 @@ impl ReturnPass {
         } else if let Some(stmt) = block.stmts.last() {
             if let StmtSemi(ref expr, _) = stmt.node {
                 if let ExprRet(Some(ref inner)) = expr.node {
-                    self.emit_return_lint(cx, (expr.span, inner.span));
+                    self.emit_return_lint(cx, (stmt.span, inner.span));
                 }
             }
         }
@@ -59,10 +59,15 @@ impl ReturnPass {
 
     fn emit_return_lint(&mut self, cx: &EarlyContext, spans: (Span, Span)) {
         if in_external_macro(cx, spans.1) {return;}
-        span_lint(cx, NEEDLESS_RETURN, spans.0, &format!(
-            "unneeded return statement. Consider using `{}` \
-             without the return and trailing semicolon",
-            snippet(cx, spans.1, "..")))
+        span_lint_and_then(cx, NEEDLESS_RETURN, spans.0,
+                           "unneeded return statement",
+                           || {
+            if let Some(snippet) = snippet_opt(cx, spans.1) {
+                cx.sess().span_suggestion(spans.0,
+                                          "remove `return` as shown:",
+                                          snippet);
+            }
+        });
     }
 
     // Check for "let x = EXPR; x"
diff --git a/tests/compile-fail/needless_return.rs b/tests/compile-fail/needless_return.rs
index e0012942906..fe29d991661 100644
--- a/tests/compile-fail/needless_return.rs
+++ b/tests/compile-fail/needless_return.rs
@@ -8,11 +8,17 @@ fn test_end_of_fn() -> bool {
         // no error!
         return true;
     }
-    return true;           //~ERROR unneeded return statement
+    return true;
+    //~^ ERROR unneeded return statement
+    //~| HELP remove `return` as shown
+    //~| SUGGESTION true
 }
 
 fn test_no_semicolon() -> bool {
-    return true            //~ERROR unneeded return statement
+    return true
+    //~^ ERROR unneeded return statement
+    //~| HELP remove `return` as shown
+    //~| SUGGESTION true
 }
 
 fn test_if_block() -> bool {
@@ -29,7 +35,7 @@ fn test_match(x: bool) -> bool {
             return false;  //~ERROR unneeded return statement
         }
         false => {
-            return true    //~ERROR unneeded return statement
+            return true;   //~ERROR unneeded return statement
         }
     }
 }