3 use syntax::codemap::{Span, Spanned};
4 use syntax::visit::FnKind;
5 use rustc::lint::{Context, LintPass, LintArray, Level};
7 use utils::{span_lint, snippet, match_path};
9 declare_lint!(pub NEEDLESS_RETURN, Warn,
10 "Warn on using a return statement where an expression would be enough");
11 declare_lint!(pub LET_AND_RETURN, Warn,
12 "Warn on creating a let-binding and then immediately returning it");
15 pub struct ReturnPass;
18 // Check the final stmt or expr in a block for unnecessary return.
19 fn check_block_return(&mut self, cx: &Context, block: &Block) {
20 if let Some(ref expr) = block.expr {
21 self.check_final_expr(cx, expr);
22 } else if let Some(stmt) = block.stmts.last() {
23 if let StmtSemi(ref expr, _) = stmt.node {
24 if let ExprRet(Some(ref inner)) = expr.node {
25 self.emit_return_lint(cx, (expr.span, inner.span));
31 // Check a the final expression in a block if it's a return.
32 fn check_final_expr(&mut self, cx: &Context, expr: &Expr) {
34 // simple return is always "bad"
35 ExprRet(Some(ref inner)) => {
36 self.emit_return_lint(cx, (expr.span, inner.span));
38 // a whole block? check it!
39 ExprBlock(ref block) => {
40 self.check_block_return(cx, block);
42 // an if/if let expr, check both exprs
43 // note, if without else is going to be a type checking error anyways
44 // (except for unit type functions) so we don't match it
45 ExprIf(_, ref ifblock, Some(ref elsexpr)) |
46 ExprIfLet(_, _, ref ifblock, Some(ref elsexpr)) => {
47 self.check_block_return(cx, ifblock);
48 self.check_final_expr(cx, elsexpr);
50 // a match expr, check all arms
51 ExprMatch(_, ref arms, _) => {
53 self.check_final_expr(cx, &*arm.body);
60 fn emit_return_lint(&mut self, cx: &Context, spans: (Span, Span)) {
61 span_lint(cx, NEEDLESS_RETURN, spans.0, &format!(
62 "unneeded return statement. Consider using `{}` \
63 without the trailing semicolon",
64 snippet(cx, spans.1, "..")))
67 // Check for "let x = EXPR; x"
68 fn check_let_return(&mut self, cx: &Context, block: &Block) {
69 // we need both a let-binding stmt and an expr
72 Some(stmt) = block.stmts.last(),
73 StmtDecl(ref decl, _) = stmt.node,
74 DeclLocal(ref local) = decl.node,
75 Some(ref initexpr) = local.init,
76 PatIdent(_, Spanned { node: id, .. }, _) = local.pat.node,
77 Some(ref retexpr) = block.expr,
78 ExprPath(_, ref path) = retexpr.node
80 if match_path(path, &[&*id.name.as_str()]) {
81 self.emit_let_lint(cx, retexpr.span, initexpr.span);
87 fn emit_let_lint(&mut self, cx: &Context, lint_span: Span, note_span: Span) {
88 span_lint(cx, LET_AND_RETURN, lint_span,
89 "returning the result of a let binding. \
90 Consider returning the expression directly.");
91 if cx.current_level(LET_AND_RETURN) != Level::Allow {
92 cx.sess().span_note(note_span,
93 "this expression can be directly returned");
98 impl LintPass for ReturnPass {
99 fn get_lints(&self) -> LintArray {
100 lint_array!(NEEDLESS_RETURN, LET_AND_RETURN)
103 fn check_fn(&mut self, cx: &Context, _: FnKind, _: &FnDecl,
104 block: &Block, _: Span, _: ast::NodeId) {
105 self.check_block_return(cx, block);
106 self.check_let_return(cx, block);