Skip to content

Commit b900e88

Browse files
committed
Merge pull request #494 from sanxiyn/suggestion-2
Use suggestion for needless_return
2 parents d31f474 + 974ab43 commit b900e88

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

Diff for: src/returns.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use syntax::ast::*;
44
use syntax::codemap::{Span, Spanned};
55
use syntax::visit::FnKind;
66

7-
use utils::{span_lint, snippet, match_path_ast, in_external_macro};
7+
use utils::{span_lint, span_lint_and_then, snippet_opt, match_path_ast, in_external_macro};
88

99
/// **What it does:** This lint checks for return statements at the end of a block. It is `Warn` by default.
1010
///
@@ -37,7 +37,7 @@ impl ReturnPass {
3737
} else if let Some(stmt) = block.stmts.last() {
3838
if let StmtSemi(ref expr, _) = stmt.node {
3939
if let ExprRet(Some(ref inner)) = expr.node {
40-
self.emit_return_lint(cx, (expr.span, inner.span));
40+
self.emit_return_lint(cx, (stmt.span, inner.span));
4141
}
4242
}
4343
}
@@ -73,10 +73,15 @@ impl ReturnPass {
7373

7474
fn emit_return_lint(&mut self, cx: &EarlyContext, spans: (Span, Span)) {
7575
if in_external_macro(cx, spans.1) {return;}
76-
span_lint(cx, NEEDLESS_RETURN, spans.0, &format!(
77-
"unneeded return statement. Consider using `{}` \
78-
without the return and trailing semicolon",
79-
snippet(cx, spans.1, "..")))
76+
span_lint_and_then(cx, NEEDLESS_RETURN, spans.0,
77+
"unneeded return statement",
78+
|| {
79+
if let Some(snippet) = snippet_opt(cx, spans.1) {
80+
cx.sess().span_suggestion(spans.0,
81+
"remove `return` as shown:",
82+
snippet);
83+
}
84+
});
8085
}
8186

8287
// Check for "let x = EXPR; x"

Diff for: tests/compile-fail/needless_return.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ fn test_end_of_fn() -> bool {
88
// no error!
99
return true;
1010
}
11-
return true; //~ERROR unneeded return statement
11+
return true;
12+
//~^ ERROR unneeded return statement
13+
//~| HELP remove `return` as shown
14+
//~| SUGGESTION true
1215
}
1316

1417
fn test_no_semicolon() -> bool {
15-
return true //~ERROR unneeded return statement
18+
return true
19+
//~^ ERROR unneeded return statement
20+
//~| HELP remove `return` as shown
21+
//~| SUGGESTION true
1622
}
1723

1824
fn test_if_block() -> bool {
@@ -29,7 +35,7 @@ fn test_match(x: bool) -> bool {
2935
return false; //~ERROR unneeded return statement
3036
}
3137
false => {
32-
return true //~ERROR unneeded return statement
38+
return true; //~ERROR unneeded return statement
3339
}
3440
}
3541
}

0 commit comments

Comments
 (0)