Skip to content

Use suggestion for needless_return #494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/returns.rs
Original file line number Diff line number Diff line change
@@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the snippet is None?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, we said "Consider using ..", which is nonsense. Now we say "unneeded return statement", without a suggestion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine, but we could do even better by defaulting to a suggestion to remove the return and semicolon.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, maybe later.

I note that currently we say "without trailing semicolon" even when there is no trailing semicolon. I don't think the current message is very good.

"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"
12 changes: 9 additions & 3 deletions tests/compile-fail/needless_return.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
}