3 use syntax::codemap::{Span, Spanned};
4 use syntax::visit::FnKind;
6 use utils::{span_note_and_lint, span_lint_and_then, snippet_opt, match_path_ast, in_external_macro};
8 /// **What it does:** Checks for return statements at the end of a block.
10 /// **Why is this bad?** Removing the `return` and semicolon will make the code
13 /// **Known problems:** None.
17 /// fn foo(x: usize) { return x; }
22 "using a return statement like `return expr;` where an expression would suffice"
25 /// **What it does:** Checks for `let`-bindings, which are subsequently returned.
27 /// **Why is this bad?** It is just extraneous code. Remove it to make your code
30 /// **Known problems:** None.
39 "creating a let-binding and then immediately returning it like `let x = expr; x` at \
43 #[derive(Copy, Clone)]
44 pub struct ReturnPass;
47 // Check the final stmt or expr in a block for unnecessary return.
48 fn check_block_return(&mut self, cx: &EarlyContext, block: &ast::Block) {
49 if let Some(stmt) = block.stmts.last() {
51 ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => {
52 self.check_final_expr(cx, expr, Some(stmt.span));
59 // Check a the final expression in a block if it's a return.
60 fn check_final_expr(&mut self, cx: &EarlyContext, expr: &ast::Expr, span: Option<Span>) {
62 // simple return is always "bad"
63 ast::ExprKind::Ret(Some(ref inner)) => {
64 // allow `#[cfg(a)] return a; #[cfg(b)] return b;`
65 if !expr.attrs.iter().any(attr_is_cfg) {
66 self.emit_return_lint(cx, span.expect("`else return` is not possible"), inner.span);
69 // a whole block? check it!
70 ast::ExprKind::Block(ref block) => {
71 self.check_block_return(cx, block);
73 // an if/if let expr, check both exprs
74 // note, if without else is going to be a type checking error anyways
75 // (except for unit type functions) so we don't match it
76 ast::ExprKind::If(_, ref ifblock, Some(ref elsexpr)) => {
77 self.check_block_return(cx, ifblock);
78 self.check_final_expr(cx, elsexpr, None);
80 // a match expr, check all arms
81 ast::ExprKind::Match(_, ref arms) => {
83 self.check_final_expr(cx, &arm.body, Some(arm.body.span));
90 fn emit_return_lint(&mut self, cx: &EarlyContext, ret_span: Span, inner_span: Span) {
91 if in_external_macro(cx, inner_span) {
94 span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded return statement", |db| {
95 if let Some(snippet) = snippet_opt(cx, inner_span) {
96 db.span_suggestion(ret_span, "remove `return` as shown:", snippet);
101 // Check for "let x = EXPR; x"
102 fn check_let_return(&mut self, cx: &EarlyContext, block: &ast::Block) {
103 let mut it = block.stmts.iter();
105 // we need both a let-binding stmt and an expr
107 let Some(ref retexpr) = it.next_back(),
108 let ast::StmtKind::Expr(ref retexpr) = retexpr.node,
109 let Some(stmt) = it.next_back(),
110 let ast::StmtKind::Local(ref local) = stmt.node,
111 !local.attrs.iter().any(attr_is_cfg),
112 let Some(ref initexpr) = local.init,
113 let ast::PatKind::Ident(_, Spanned { node: id, .. }, _) = local.pat.node,
114 let ast::ExprKind::Path(_, ref path) = retexpr.node,
115 match_path_ast(path, &[&id.name.as_str()]),
116 !in_external_macro(cx, initexpr.span),
118 span_note_and_lint(cx,
121 "returning the result of a let binding from a block. \
122 Consider returning the expression directly.",
124 "this expression can be directly returned");
129 impl LintPass for ReturnPass {
130 fn get_lints(&self) -> LintArray {
131 lint_array!(NEEDLESS_RETURN, LET_AND_RETURN)
135 impl EarlyLintPass for ReturnPass {
136 fn check_fn(&mut self, cx: &EarlyContext, kind: FnKind, _: &ast::FnDecl, _: Span, _: ast::NodeId) {
138 FnKind::ItemFn(.., block) | FnKind::Method(.., block) => self.check_block_return(cx, block),
139 FnKind::Closure(body) => self.check_final_expr(cx, body, None),
143 fn check_block(&mut self, cx: &EarlyContext, block: &ast::Block) {
144 self.check_let_return(cx, block);
148 fn attr_is_cfg(attr: &ast::Attribute) -> bool {
149 if let ast::MetaItemKind::List(ref key, _) = attr.node.value.node {