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) {return;}
76 span_lint_and_then(cx, NEEDLESS_RETURN, spans.0,
77 "unneeded return statement",
79 if let Some(snippet) = snippet_opt(cx, spans.1) {
80 cx.sess().span_suggestion(spans.0,
81 "remove `return` as shown:",
87 // Check for "let x = EXPR; x"
88 fn check_let_return(&mut self, cx: &EarlyContext, block: &Block) {
89 // we need both a let-binding stmt and an expr
92 let Some(stmt) = block.stmts.last(),
93 let Some(ref retexpr) = block.expr,
94 let StmtDecl(ref decl, _) = stmt.node,
95 let DeclLocal(ref local) = decl.node,
96 let Some(ref initexpr) = local.init,
97 let PatIdent(_, Spanned { node: id, .. }, _) = local.pat.node,
98 let ExprPath(_, ref path) = retexpr.node,
99 match_path_ast(path, &[&id.name.as_str()])
101 self.emit_let_lint(cx, retexpr.span, initexpr.span);
106 fn emit_let_lint(&mut self, cx: &EarlyContext, lint_span: Span, note_span: Span) {
107 if in_external_macro(cx, note_span) {return;}
108 span_lint(cx, LET_AND_RETURN, lint_span,
109 "returning the result of a let binding from a block. \
110 Consider returning the expression directly.");
111 if cx.current_level(LET_AND_RETURN) != Level::Allow {
112 cx.sess().span_note(note_span,
113 "this expression can be directly returned");
118 impl LintPass for ReturnPass {
119 fn get_lints(&self) -> LintArray {
120 lint_array!(NEEDLESS_RETURN, LET_AND_RETURN)
124 impl EarlyLintPass for ReturnPass {
125 fn check_fn(&mut self, cx: &EarlyContext, _: FnKind, _: &FnDecl,
126 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);