]> git.lizzy.rs Git - rust.git/blob - src/returns.rs
added wiki comments + wiki-generating python script
[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, snippet, 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, (expr.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(cx, NEEDLESS_RETURN, spans.0, &format!(
77             "unneeded return statement. Consider using `{}` \
78              without the return and trailing semicolon",
79             snippet(cx, spans.1, "..")))
80     }
81
82     // Check for "let x = EXPR; x"
83     fn check_let_return(&mut self, cx: &EarlyContext, block: &Block) {
84         // we need both a let-binding stmt and an expr
85         if_let_chain! {
86             [
87                 let Some(stmt) = block.stmts.last(),
88                 let Some(ref retexpr) = block.expr,
89                 let StmtDecl(ref decl, _) = stmt.node,
90                 let DeclLocal(ref local) = decl.node,
91                 let Some(ref initexpr) = local.init,
92                 let PatIdent(_, Spanned { node: id, .. }, _) = local.pat.node,
93                 let ExprPath(_, ref path) = retexpr.node,
94                 match_path_ast(path, &[&id.name.as_str()])
95             ], {
96                 self.emit_let_lint(cx, retexpr.span, initexpr.span);
97             }
98         }
99     }
100
101     fn emit_let_lint(&mut self, cx: &EarlyContext, lint_span: Span, note_span: Span) {
102         if in_external_macro(cx, note_span) {return;}
103         span_lint(cx, LET_AND_RETURN, lint_span,
104                   "returning the result of a let binding from a block. \
105                    Consider returning the expression directly.");
106         if cx.current_level(LET_AND_RETURN) != Level::Allow {
107             cx.sess().span_note(note_span,
108                                 "this expression can be directly returned");
109         }
110     }
111 }
112
113 impl LintPass for ReturnPass {
114     fn get_lints(&self) -> LintArray {
115         lint_array!(NEEDLESS_RETURN, LET_AND_RETURN)
116     }
117 }
118
119 impl EarlyLintPass for ReturnPass {
120     fn check_fn(&mut self, cx: &EarlyContext, _: FnKind, _: &FnDecl,
121                 block: &Block, _: Span, _: NodeId) {
122         self.check_block_return(cx, block);
123     }
124
125     fn check_block(&mut self, cx: &EarlyContext, block: &Block) {
126         self.check_let_return(cx, block);
127     }
128 }