]> git.lizzy.rs Git - rust.git/blob - src/returns.rs
fmt clippy
[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) {
76             return;
77         }
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);
81             }
82         });
83     }
84
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
88         if_let_chain! {
89             [
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()])
98             ], {
99                 self.emit_let_lint(cx, retexpr.span, initexpr.span);
100             }
101         }
102     }
103
104     fn emit_let_lint(&mut self, cx: &EarlyContext, lint_span: Span, note_span: Span) {
105         if in_external_macro(cx, note_span) {
106             return;
107         }
108         let mut db = span_lint(cx,
109                                LET_AND_RETURN,
110                                lint_span,
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");
115         }
116     }
117 }
118
119 impl LintPass for ReturnPass {
120     fn get_lints(&self) -> LintArray {
121         lint_array!(NEEDLESS_RETURN, LET_AND_RETURN)
122     }
123 }
124
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);
128     }
129
130     fn check_block(&mut self, cx: &EarlyContext, block: &Block) {
131         self.check_let_return(cx, block);
132     }
133 }