]> git.lizzy.rs Git - rust.git/blob - src/returns.rs
Remove * dep
[rust.git] / src / returns.rs
1 use rustc::lint::*;
2 use syntax::ast::*;
3 //use reexport::*;
4 use syntax::codemap::{Span, Spanned};
5 use syntax::visit::FnKind;
6
7 use utils::{span_lint, span_lint_and_then, snippet_opt, match_path_ast, in_external_macro};
8
9 /// **What it does:** This lint checks for return statements at the end of a block. It is `Warn` by default.
10 ///
11 /// **Why is this bad?** Removing the `return` and semicolon will make the code more rusty.
12 ///
13 /// **Known problems:** None
14 ///
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.
19 ///
20 /// **Why is this bad?** It is just extraneous code. Remove it to make your code more rusty.
21 ///
22 /// **Known problems:** None
23 ///
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 \
27                the end of a block");
28
29 #[derive(Copy, Clone)]
30 pub struct ReturnPass;
31
32 impl 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));
41                 }
42             }
43         }
44     }
45
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) {
48         match expr.node {
49             // simple return is always "bad"
50             ExprRet(Some(ref inner)) => {
51                 self.emit_return_lint(cx, (expr.span, inner.span));
52             }
53             // a whole block? check it!
54             ExprBlock(ref block) => {
55                 self.check_block_return(cx, block);
56             }
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);
63             }
64             // a match expr, check all arms
65             ExprMatch(_, ref arms) => {
66                 for arm in arms {
67                     self.check_final_expr(cx, &arm.body);
68                 }
69             }
70             _ => { }
71         }
72     }
73
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",
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         });
85     }
86
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
90         if_let_chain! {
91             [
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()])
100             ], {
101                 self.emit_let_lint(cx, retexpr.span, initexpr.span);
102             }
103         }
104     }
105
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");
114         }
115     }
116 }
117
118 impl LintPass for ReturnPass {
119     fn get_lints(&self) -> LintArray {
120         lint_array!(NEEDLESS_RETURN, LET_AND_RETURN)
121     }
122 }
123
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);
128     }
129
130     fn check_block(&mut self, cx: &EarlyContext, block: &Block) {
131         self.check_let_return(cx, block);
132     }
133 }