3 paths::{BEGIN_PANIC, BEGIN_PANIC_FMT},
4 snippet_opt, span_lint_and_then,
6 use if_chain::if_chain;
8 declare_lint_pass, declare_tool_lint,
9 hir::{intravisit::FnKind, Body, Expr, ExprKind, FnDecl, HirId, MatchSource, StmtKind},
10 lint::{LateContext, LateLintPass, LintArray, LintPass},
12 use rustc_errors::Applicability;
13 use syntax::source_map::Span;
15 declare_clippy_lint! {
16 /// **What it does:** Checks for missing return statements at the end of a block.
18 /// **Why is this bad?** Actually omitting the return keyword is idiomatic Rust code. Programmers
19 /// coming from other languages might prefer the expressiveness of `return`. It's possible to miss
20 /// the last returning statement because the only difference is a missing `;`. Especially in bigger
21 /// code with multiple return paths having a `return` keyword makes it easier to find the
22 /// corresponding statements.
24 /// **Known problems:** None.
28 /// fn foo(x: usize) -> usize {
34 /// fn foo(x: usize) -> usize {
40 "use a return statement like `return expr` instead of an expression"
43 declare_lint_pass!(ImplicitReturn => [IMPLICIT_RETURN]);
45 static LINT_BREAK: &str = "change `break` to `return` as shown";
46 static LINT_RETURN: &str = "add `return` as shown";
48 fn lint(cx: &LateContext<'_, '_>, outer_span: Span, inner_span: Span, msg: &str) {
49 let outer_span = outer_span.source_callsite();
50 let inner_span = inner_span.source_callsite();
52 span_lint_and_then(cx, IMPLICIT_RETURN, outer_span, "missing return statement", |db| {
53 if let Some(snippet) = snippet_opt(cx, inner_span) {
57 format!("return {}", snippet),
58 Applicability::MachineApplicable,
64 fn expr_match(cx: &LateContext<'_, '_>, expr: &Expr) {
66 // loops could be using `break` instead of `return`
67 ExprKind::Block(block, ..) | ExprKind::Loop(block, ..) => {
68 if let Some(expr) = &block.expr {
71 // only needed in the case of `break` with `;` at the end
72 else if let Some(stmt) = block.stmts.last() {
74 if let StmtKind::Semi(expr, ..) = &stmt.kind;
75 // make sure it's a break, otherwise we want to skip
76 if let ExprKind::Break(.., break_expr) = &expr.kind;
77 if let Some(break_expr) = break_expr;
79 lint(cx, expr.span, break_expr.span, LINT_BREAK);
84 // use `return` instead of `break`
85 ExprKind::Break(.., break_expr) => {
86 if let Some(break_expr) = break_expr {
87 lint(cx, expr.span, break_expr.span, LINT_BREAK);
90 ExprKind::Match(.., arms, source) => {
91 let check_all_arms = match source {
92 MatchSource::IfLetDesugar {
93 contains_else_clause: has_else,
100 expr_match(cx, &arm.body);
103 expr_match(cx, &arms.first().expect("if let doesn't have a single arm").body);
106 // skip if it already has a return statement
107 ExprKind::Ret(..) => (),
108 // make sure it's not a call that panics
109 ExprKind::Call(expr, ..) => {
111 if let ExprKind::Path(qpath) = &expr.kind;
112 if let Some(path_def_id) = cx.tables.qpath_res(qpath, expr.hir_id).opt_def_id();
113 if match_def_path(cx, path_def_id, &BEGIN_PANIC) ||
114 match_def_path(cx, path_def_id, &BEGIN_PANIC_FMT);
117 lint(cx, expr.span, expr.span, LINT_RETURN)
121 // everything else is missing `return`
122 _ => lint(cx, expr.span, expr.span, LINT_RETURN),
126 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitReturn {
129 cx: &LateContext<'a, 'tcx>,
136 let def_id = cx.tcx.hir().body_owner_def_id(body.id());
137 let mir = cx.tcx.optimized_mir(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);