4 use syntax::codemap::{Span, Spanned};
5 use syntax::visit::FnKind;
7 use utils::{span_lint, span_lint_and_then, snippet_opt, match_path_ast, in_external_macro};
9 /// **What it does:** This lint checks for return statements at the end of a block. It is `Warn` by default.
11 /// **Why is this bad?** Removing the `return` and semicolon will make the code more rusty.
13 /// **Known problems:** None
15 /// **Example:** `fn foo(x: usize) { return x; }`
16 declare_lint!(pub NEEDLESS_RETURN, Warn,
17 "using a return statement like `return expr;` where an expression would suffice");
18 /// **What it does:** This lint checks for `let`-bindings, which are subsequently returned. It is `Warn` by default.
20 /// **Why is this bad?** It is just extraneous code. Remove it to make your code more rusty.
22 /// **Known problems:** None
24 /// **Example:** `{ let x = ..; x }`
25 declare_lint!(pub LET_AND_RETURN, Warn,
26 "creating a let-binding and then immediately returning it like `let x = expr; x` at \
29 #[derive(Copy, Clone)]
30 pub struct ReturnPass;
33 // Check the final stmt or expr in a block for unnecessary return.
34 fn check_block_return(&mut self, cx: &EarlyContext, block: &Block) {
35 if let Some(ref expr) = block.expr {
36 self.check_final_expr(cx, expr);
37 } else if let Some(stmt) = block.stmts.last() {
38 if let StmtSemi(ref expr, _) = stmt.node {
39 if let ExprRet(Some(ref inner)) = expr.node {
40 self.emit_return_lint(cx, (stmt.span, inner.span));
46 // Check a the final expression in a block if it's a return.
47 fn check_final_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
49 // simple return is always "bad"
50 ExprRet(Some(ref inner)) => {
51 self.emit_return_lint(cx, (expr.span, inner.span));
53 // a whole block? check it!
54 ExprBlock(ref block) => {
55 self.check_block_return(cx, block);
57 // an if/if let expr, check both exprs
58 // note, if without else is going to be a type checking error anyways
59 // (except for unit type functions) so we don't match it
60 ExprIf(_, ref ifblock, Some(ref elsexpr)) => {
61 self.check_block_return(cx, ifblock);
62 self.check_final_expr(cx, elsexpr);
64 // a match expr, check all arms
65 ExprMatch(_, ref arms) => {
67 self.check_final_expr(cx, &arm.body);
74 fn emit_return_lint(&mut self, cx: &EarlyContext, spans: (Span, Span)) {
75 if in_external_macro(cx, spans.1) {
78 span_lint_and_then(cx, NEEDLESS_RETURN, spans.0, "unneeded return statement", |db| {
79 if let Some(snippet) = snippet_opt(cx, spans.1) {
80 db.span_suggestion(spans.0, "remove `return` as shown:", snippet);
85 // Check for "let x = EXPR; x"
86 fn check_let_return(&mut self, cx: &EarlyContext, block: &Block) {
87 // we need both a let-binding stmt and an expr
90 let Some(stmt) = block.stmts.last(),
91 let Some(ref retexpr) = block.expr,
92 let StmtDecl(ref decl, _) = stmt.node,
93 let DeclLocal(ref local) = decl.node,
94 let Some(ref initexpr) = local.init,
95 let PatIdent(_, Spanned { node: id, .. }, _) = local.pat.node,
96 let ExprPath(_, ref path) = retexpr.node,
97 match_path_ast(path, &[&id.name.as_str()])
99 self.emit_let_lint(cx, retexpr.span, initexpr.span);
104 fn emit_let_lint(&mut self, cx: &EarlyContext, lint_span: Span, note_span: Span) {
105 if in_external_macro(cx, note_span) {
108 let mut db = span_lint(cx,
111 "returning the result of a let binding from a block. Consider returning the \
112 expression directly.");
113 if cx.current_level(LET_AND_RETURN) != Level::Allow {
114 db.span_note(note_span, "this expression can be directly returned");
119 impl LintPass for ReturnPass {
120 fn get_lints(&self) -> LintArray {
121 lint_array!(NEEDLESS_RETURN, LET_AND_RETURN)
125 impl EarlyLintPass for ReturnPass {
126 fn check_fn(&mut self, cx: &EarlyContext, _: FnKind, _: &FnDecl, block: &Block, _: Span, _: NodeId) {
127 self.check_block_return(cx, block);
130 fn check_block(&mut self, cx: &EarlyContext, block: &Block) {
131 self.check_let_return(cx, block);