1 use crate::utils::{in_macro_or_desugar, is_expn_of, snippet_opt, span_lint_and_then};
2 use rustc::hir::{intravisit::FnKind, Body, ExprKind, FnDecl, HirId, MatchSource};
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::{declare_lint_pass, declare_tool_lint};
5 use rustc_errors::Applicability;
6 use syntax::source_map::Span;
9 /// **What it does:** Checks for missing return statements at the end of a block.
11 /// **Why is this bad?** Actually omitting the return keyword is idiomatic Rust code. Programmers
12 /// coming from other languages might prefer the expressiveness of `return`. It's possible to miss
13 /// the last returning statement because the only difference is a missing `;`. Especially in bigger
14 /// code with multiple return paths having a `return` keyword makes it easier to find the
15 /// corresponding statements.
17 /// **Known problems:** None.
21 /// fn foo(x: usize) {
27 /// fn foo(x: usize) {
33 "use a return statement like `return expr` instead of an expression"
36 declare_lint_pass!(ImplicitReturn => [IMPLICIT_RETURN]);
39 fn lint(cx: &LateContext<'_, '_>, outer_span: syntax_pos::Span, inner_span: syntax_pos::Span, msg: &str) {
40 span_lint_and_then(cx, IMPLICIT_RETURN, outer_span, "missing return statement", |db| {
41 if let Some(snippet) = snippet_opt(cx, inner_span) {
45 format!("return {}", snippet),
46 Applicability::MachineApplicable,
52 fn expr_match(cx: &LateContext<'_, '_>, expr: &rustc::hir::Expr) {
54 // loops could be using `break` instead of `return`
55 ExprKind::Block(block, ..) | ExprKind::Loop(block, ..) => {
56 if let Some(expr) = &block.expr {
57 Self::expr_match(cx, expr);
59 // only needed in the case of `break` with `;` at the end
60 else if let Some(stmt) = block.stmts.last() {
61 if let rustc::hir::StmtKind::Semi(expr, ..) = &stmt.node {
62 // make sure it's a break, otherwise we want to skip
63 if let ExprKind::Break(.., break_expr) = &expr.node {
64 if let Some(break_expr) = break_expr {
65 Self::lint(cx, expr.span, break_expr.span, "change `break` to `return` as shown");
71 // use `return` instead of `break`
72 ExprKind::Break(.., break_expr) => {
73 if let Some(break_expr) = break_expr {
74 Self::lint(cx, expr.span, break_expr.span, "change `break` to `return` as shown");
77 ExprKind::Match(.., arms, source) => {
78 let check_all_arms = match source {
79 MatchSource::IfLetDesugar {
80 contains_else_clause: has_else,
87 Self::expr_match(cx, &arm.body);
90 Self::expr_match(cx, &arms.first().expect("if let doesn't have a single arm").body);
93 // skip if it already has a return statement
94 ExprKind::Ret(..) => (),
95 // everything else is missing `return`
97 // make sure it's not just an unreachable expression
98 if is_expn_of(expr.span, "unreachable").is_none() {
99 Self::lint(cx, expr.span, expr.span, "add `return` as shown")
106 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitReturn {
109 cx: &LateContext<'a, 'tcx>,
116 let def_id = cx.tcx.hir().body_owner_def_id(body.id());
117 let mir = cx.tcx.optimized_mir(def_id);
119 // checking return type through MIR, HIR is not able to determine inferred closure return types
120 // make sure it's not a macro
121 if !mir.return_ty().is_unit() && !in_macro_or_desugar(span) {
122 Self::expr_match(cx, &body.value);