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:** This lint checks for return statements at the end of a block.
10 /// **Why is this bad?** Removing the `return` and semicolon will make the code more rusty.
12 /// **Known problems:** Following this lint's advice may currently run afoul of Rust issue [#31439](https://github.com/rust-lang/rust/issues/31439), so if you get lifetime errors, please roll back the change until that issue is fixed.
14 /// **Example:** `fn foo(x: usize) { return x; }`
16 pub NEEDLESS_RETURN, Warn,
17 "using a return statement like `return expr;` where an expression would suffice"
20 /// **What it does:** This lint checks for `let`-bindings, which are subsequently returned.
22 /// **Why is this bad?** It is just extraneous code. Remove it to make your code more rusty.
24 /// **Known problems:** Following this lint's advice may currently run afoul of Rust issue [#31439](https://github.com/rust-lang/rust/issues/31439), so if you get lifetime errors, please roll back the change until that issue is fixed.
26 /// **Example:** `{ let x = ..; x }`
28 pub LET_AND_RETURN, Warn,
29 "creating a let-binding and then immediately returning it like `let x = expr; x` at \
33 #[derive(Copy, Clone)]
34 pub struct ReturnPass;
37 // Check the final stmt or expr in a block for unnecessary return.
38 fn check_block_return(&mut self, cx: &EarlyContext, block: &Block) {
39 if let Some(stmt) = block.stmts.last() {
41 StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => {
42 self.check_final_expr(cx, expr, Some(stmt.span));
49 // Check a the final expression in a block if it's a return.
50 fn check_final_expr(&mut self, cx: &EarlyContext, expr: &Expr, span: Option<Span>) {
52 // simple return is always "bad"
53 ExprKind::Ret(Some(ref inner)) => {
54 self.emit_return_lint(cx, span.expect("`else return` is not possible"), inner.span);
56 // a whole block? check it!
57 ExprKind::Block(ref block) => {
58 self.check_block_return(cx, block);
60 // an if/if let expr, check both exprs
61 // note, if without else is going to be a type checking error anyways
62 // (except for unit type functions) so we don't match it
63 ExprKind::If(_, ref ifblock, Some(ref elsexpr)) => {
64 self.check_block_return(cx, ifblock);
65 self.check_final_expr(cx, elsexpr, None);
67 // a match expr, check all arms
68 ExprKind::Match(_, ref arms) => {
70 self.check_final_expr(cx, &arm.body, Some(arm.body.span));
77 fn emit_return_lint(&mut self, cx: &EarlyContext, ret_span: Span, inner_span: Span) {
78 if in_external_macro(cx, inner_span) {
81 span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded return statement", |db| {
82 if let Some(snippet) = snippet_opt(cx, inner_span) {
83 db.span_suggestion(ret_span, "remove `return` as shown:", snippet);
88 // Check for "let x = EXPR; x"
89 fn check_let_return(&mut self, cx: &EarlyContext, block: &Block) {
90 let mut it = block.stmts.iter();
92 // we need both a let-binding stmt and an expr
94 let Some(ref retexpr) = it.next_back(),
95 let StmtKind::Expr(ref retexpr) = retexpr.node,
96 let Some(stmt) = it.next_back(),
97 let StmtKind::Local(ref local) = stmt.node,
98 let Some(ref initexpr) = local.init,
99 let PatKind::Ident(_, Spanned { node: id, .. }, _) = local.pat.node,
100 let ExprKind::Path(_, ref path) = retexpr.node,
101 match_path_ast(path, &[&id.name.as_str()]),
102 !in_external_macro(cx, initexpr.span),
104 span_note_and_lint(cx,
107 "returning the result of a let binding from a block. \
108 Consider returning the expression directly.",
110 "this expression can be directly returned");
115 impl LintPass for ReturnPass {
116 fn get_lints(&self) -> LintArray {
117 lint_array!(NEEDLESS_RETURN, LET_AND_RETURN)
121 impl EarlyLintPass for ReturnPass {
122 fn check_fn(&mut self, cx: &EarlyContext, _: FnKind, _: &FnDecl, block: &Block, _: Span, _: NodeId) {
123 self.check_block_return(cx, block);
126 fn check_block(&mut self, cx: &EarlyContext, block: &Block) {
127 self.check_let_return(cx, block);