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