1 use crate::utils::{fn_has_unsatisfiable_preds, match_panic_def_id, snippet_opt, span_lint_and_then};
2 use if_chain::if_chain;
3 use rustc_errors::Applicability;
4 use rustc_hir::intravisit::FnKind;
5 use rustc_hir::{Body, Expr, ExprKind, FnDecl, HirId, MatchSource, StmtKind};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::source_map::Span;
10 declare_clippy_lint! {
11 /// **What it does:** Checks for missing return statements at the end of a block.
13 /// **Why is this bad?** Actually omitting the return keyword is idiomatic Rust code. Programmers
14 /// coming from other languages might prefer the expressiveness of `return`. It's possible to miss
15 /// the last returning statement because the only difference is a missing `;`. Especially in bigger
16 /// code with multiple return paths having a `return` keyword makes it easier to find the
17 /// corresponding statements.
19 /// **Known problems:** None.
23 /// fn foo(x: usize) -> usize {
29 /// fn foo(x: usize) -> usize {
35 "use a return statement like `return expr` instead of an expression"
38 declare_lint_pass!(ImplicitReturn => [IMPLICIT_RETURN]);
40 static LINT_BREAK: &str = "change `break` to `return` as shown";
41 static LINT_RETURN: &str = "add `return` as shown";
43 fn lint(cx: &LateContext<'_>, outer_span: Span, inner_span: Span, msg: &str) {
44 let outer_span = outer_span.source_callsite();
45 let inner_span = inner_span.source_callsite();
47 span_lint_and_then(cx, IMPLICIT_RETURN, outer_span, "missing `return` statement", |diag| {
48 if let Some(snippet) = snippet_opt(cx, inner_span) {
52 format!("return {}", snippet),
53 Applicability::MachineApplicable,
59 fn expr_match(cx: &LateContext<'_>, expr: &Expr<'_>) {
61 // loops could be using `break` instead of `return`
62 ExprKind::Block(block, ..) | ExprKind::Loop(block, ..) => {
63 if let Some(expr) = &block.expr {
66 // only needed in the case of `break` with `;` at the end
67 else if let Some(stmt) = block.stmts.last() {
69 if let StmtKind::Semi(expr, ..) = &stmt.kind;
70 // make sure it's a break, otherwise we want to skip
71 if let ExprKind::Break(.., break_expr) = &expr.kind;
72 if let Some(break_expr) = break_expr;
74 lint(cx, expr.span, break_expr.span, LINT_BREAK);
79 // use `return` instead of `break`
80 ExprKind::Break(.., break_expr) => {
81 if let Some(break_expr) = break_expr {
82 lint(cx, expr.span, break_expr.span, LINT_BREAK);
85 ExprKind::Match(.., arms, source) => {
86 let check_all_arms = match source {
87 MatchSource::IfLetDesugar {
88 contains_else_clause: has_else,
95 expr_match(cx, &arm.body);
98 expr_match(cx, &arms.first().expect("`if let` doesn't have a single arm").body);
101 // skip if it already has a return statement
102 ExprKind::Ret(..) => (),
103 // make sure it's not a call that panics
104 ExprKind::Call(expr, ..) => {
106 if let ExprKind::Path(qpath) = &expr.kind;
107 if let Some(path_def_id) = cx.qpath_res(qpath, expr.hir_id).opt_def_id();
108 if match_panic_def_id(cx, path_def_id);
111 lint(cx, expr.span, expr.span, LINT_RETURN)
115 // everything else is missing `return`
116 _ => lint(cx, expr.span, expr.span, LINT_RETURN),
120 impl<'tcx> LateLintPass<'tcx> for ImplicitReturn {
123 cx: &LateContext<'tcx>,
126 body: &'tcx Body<'_>,
130 let def_id = cx.tcx.hir().body_owner_def_id(body.id());
132 // Building MIR for `fn`s with unsatisfiable preds results in ICE.
133 if fn_has_unsatisfiable_preds(cx, def_id.to_def_id()) {
137 let mir = cx.tcx.optimized_mir(def_id.to_def_id());
139 // checking return type through MIR, HIR is not able to determine inferred closure return types
140 // make sure it's not a macro
141 if !mir.return_ty().is_unit() && !span.from_expansion() {
142 expr_match(cx, &body.value);