mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-26 22:50:56 +00:00
Use suggestion for needless_return
This commit is contained in:
parent
3e7677ff31
commit
974ab43453
2 changed files with 20 additions and 9 deletions
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue