1 use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
2 use clippy_utils::source::{snippet_opt, snippet_with_context};
3 use clippy_utils::visitors::{for_each_expr, Descend};
4 use clippy_utils::{fn_def_id, path_to_local_id};
5 use core::ops::ControlFlow;
6 use if_chain::if_chain;
7 use rustc_errors::Applicability;
8 use rustc_hir::intravisit::FnKind;
9 use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, HirId, MatchSource, PatKind, StmtKind};
10 use rustc_lint::{LateContext, LateLintPass, LintContext};
11 use rustc_middle::lint::in_external_macro;
12 use rustc_middle::ty::subst::GenericArgKind;
13 use rustc_session::{declare_lint_pass, declare_tool_lint};
14 use rustc_span::source_map::Span;
16 declare_clippy_lint! {
18 /// Checks for `let`-bindings, which are subsequently
21 /// ### Why is this bad?
22 /// It is just extraneous code. Remove it to make your code
27 /// fn foo() -> String {
28 /// let x = String::new();
34 /// fn foo() -> String {
38 #[clippy::version = "pre 1.29.0"]
41 "creating a let-binding and then immediately returning it like `let x = expr; x` at the end of a block"
44 declare_clippy_lint! {
46 /// Checks for return statements at the end of a block.
48 /// ### Why is this bad?
49 /// Removing the `return` and semicolon will make the code
54 /// fn foo(x: usize) -> usize {
60 /// fn foo(x: usize) -> usize {
64 #[clippy::version = "pre 1.29.0"]
67 "using a return statement like `return expr;` where an expression would suffice"
70 #[derive(PartialEq, Eq, Copy, Clone)]
78 fn sugg_help(self) -> &'static str {
80 Self::Empty => "remove `return`",
81 Self::Block => "replace `return` with an empty block",
82 Self::Unit => "replace `return` with a unit value",
87 impl ToString for RetReplacement {
88 fn to_string(&self) -> String {
98 declare_lint_pass!(Return => [LET_AND_RETURN, NEEDLESS_RETURN]);
100 impl<'tcx> LateLintPass<'tcx> for Return {
101 fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'_>) {
102 // we need both a let-binding stmt and an expr
104 if let Some(retexpr) = block.expr;
105 if let Some(stmt) = block.stmts.iter().last();
106 if let StmtKind::Local(local) = &stmt.kind;
107 if local.ty.is_none();
108 if cx.tcx.hir().attrs(local.hir_id).is_empty();
109 if let Some(initexpr) = &local.init;
110 if let PatKind::Binding(_, local_id, _, _) = local.pat.kind;
111 if path_to_local_id(retexpr, local_id);
112 if !last_statement_borrows(cx, initexpr);
113 if !in_external_macro(cx.sess(), initexpr.span);
114 if !in_external_macro(cx.sess(), retexpr.span);
115 if !local.span.from_expansion();
117 span_lint_hir_and_then(
122 "returning the result of a `let` binding from a block",
124 err.span_label(local.span, "unnecessary `let` binding");
126 if let Some(mut snippet) = snippet_opt(cx, initexpr.span) {
127 if !cx.typeck_results().expr_adjustments(retexpr).is_empty() {
128 snippet.push_str(" as _");
130 err.multipart_suggestion(
131 "return the expression directly",
133 (local.span, String::new()),
134 (retexpr.span, snippet),
136 Applicability::MachineApplicable,
139 err.span_help(initexpr.span, "this expression can be directly returned");
149 cx: &LateContext<'tcx>,
151 _: &'tcx FnDecl<'tcx>,
152 body: &'tcx Body<'tcx>,
158 // when returning without value in closure, replace this `return`
159 // with an empty block to prevent invalid suggestion (see #6501)
160 let replacement = if let ExprKind::Ret(None) = &body.value.kind {
161 RetReplacement::Block
163 RetReplacement::Empty
165 check_final_expr(cx, body.value, vec![], replacement);
167 FnKind::ItemFn(..) | FnKind::Method(..) => {
168 check_block_return(cx, &body.value.kind, vec![]);
174 // if `expr` is a block, check if there are needless returns in it
175 fn check_block_return<'tcx>(cx: &LateContext<'tcx>, expr_kind: &ExprKind<'tcx>, semi_spans: Vec<Span>) {
176 if let ExprKind::Block(block, _) = expr_kind {
177 if let Some(block_expr) = block.expr {
178 check_final_expr(cx, block_expr, semi_spans, RetReplacement::Empty);
179 } else if let Some(stmt) = block.stmts.iter().last() {
181 StmtKind::Expr(expr) => {
182 check_final_expr(cx, expr, semi_spans, RetReplacement::Empty);
184 StmtKind::Semi(semi_expr) => {
185 let mut semi_spans_and_this_one = semi_spans;
186 // we only want the span containing the semicolon so we can remove it later. From `entry.rs:382`
187 if let Some(semicolon_span) = stmt.span.trim_start(semi_expr.span) {
188 semi_spans_and_this_one.push(semicolon_span);
189 check_final_expr(cx, semi_expr, semi_spans_and_this_one, RetReplacement::Empty);
198 fn check_final_expr<'tcx>(
199 cx: &LateContext<'tcx>,
200 expr: &'tcx Expr<'tcx>,
201 semi_spans: Vec<Span>, /* containing all the places where we would need to remove semicolons if finding an
203 replacement: RetReplacement,
205 let peeled_drop_expr = expr.peel_drop_temps();
206 match &peeled_drop_expr.kind {
207 // simple return is always "bad"
208 ExprKind::Ret(ref inner) => {
209 if cx.tcx.hir().attrs(expr.hir_id).is_empty() {
210 let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
214 peeled_drop_expr.span,
216 inner.as_ref().map(|i| i.span),
222 ExprKind::If(_, then, else_clause_opt) => {
223 check_block_return(cx, &then.kind, semi_spans.clone());
224 if let Some(else_clause) = else_clause_opt {
225 check_block_return(cx, &else_clause.kind, semi_spans);
228 // a match expr, check all arms
229 // an if/if let expr, check both exprs
230 // note, if without else is going to be a type checking error anyways
231 // (except for unit type functions) so we don't match it
232 ExprKind::Match(_, arms, MatchSource::Normal) => {
233 for arm in arms.iter() {
234 check_final_expr(cx, arm.body, semi_spans.clone(), RetReplacement::Unit);
237 // if it's a whole block, check it
238 other_expr_kind => check_block_return(cx, other_expr_kind, semi_spans),
243 cx: &LateContext<'_>,
245 semi_spans: Vec<Span>,
246 inner_span: Option<Span>,
247 replacement: RetReplacement,
249 if ret_span.from_expansion() {
252 let mut applicability = Applicability::MachineApplicable;
253 let return_replacement = inner_span.map_or_else(
254 || replacement.to_string(),
256 let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability);
260 let sugg_help = if inner_span.is_some() {
263 replacement.sugg_help()
265 span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| {
266 diag.span_suggestion_hidden(ret_span, sugg_help, return_replacement, applicability);
267 // for each parent statement, we need to remove the semicolon
268 for semi_stmt_span in semi_spans {
269 diag.tool_only_span_suggestion(semi_stmt_span, "remove this semicolon", "", applicability);
274 fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
275 for_each_expr(expr, |e| {
276 if let Some(def_id) = fn_def_id(cx, e)
283 .any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(_)))
285 ControlFlow::Break(())
287 ControlFlow::Continue(Descend::from(!expr.span.from_expansion()))