1 use clippy_utils::diagnostics::span_lint_hir_and_then;
2 use clippy_utils::source::{snippet_opt, snippet_with_context};
3 use clippy_utils::{fn_def_id, path_to_local_id};
4 use if_chain::if_chain;
5 use rustc_errors::Applicability;
6 use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
7 use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, HirId, MatchSource, PatKind, StmtKind};
8 use rustc_lint::{LateContext, LateLintPass, LintContext};
9 use rustc_middle::lint::in_external_macro;
10 use rustc_middle::ty::subst::GenericArgKind;
11 use rustc_session::{declare_lint_pass, declare_tool_lint};
12 use rustc_span::source_map::Span;
14 declare_clippy_lint! {
16 /// Checks for `let`-bindings, which are subsequently
19 /// ### Why is this bad?
20 /// It is just extraneous code. Remove it to make your code
25 /// fn foo() -> String {
26 /// let x = String::new();
32 /// fn foo() -> String {
36 #[clippy::version = "pre 1.29.0"]
39 "creating a let-binding and then immediately returning it like `let x = expr; x` at the end of a block"
42 declare_clippy_lint! {
44 /// Checks for return statements at the end of a block.
46 /// ### Why is this bad?
47 /// Removing the `return` and semicolon will make the code
52 /// fn foo(x: usize) -> usize {
58 /// fn foo(x: usize) -> usize {
62 #[clippy::version = "pre 1.29.0"]
65 "using a return statement like `return expr;` where an expression would suffice"
68 #[derive(PartialEq, Eq, Copy, Clone)]
75 declare_lint_pass!(Return => [LET_AND_RETURN, NEEDLESS_RETURN]);
77 impl<'tcx> LateLintPass<'tcx> for Return {
78 fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'_>) {
79 // we need both a let-binding stmt and an expr
81 if let Some(retexpr) = block.expr;
82 if let Some(stmt) = block.stmts.iter().last();
83 if let StmtKind::Local(local) = &stmt.kind;
84 if local.ty.is_none();
85 if cx.tcx.hir().attrs(local.hir_id).is_empty();
86 if let Some(initexpr) = &local.init;
87 if let PatKind::Binding(_, local_id, _, _) = local.pat.kind;
88 if path_to_local_id(retexpr, local_id);
89 if !last_statement_borrows(cx, initexpr);
90 if !in_external_macro(cx.sess(), initexpr.span);
91 if !in_external_macro(cx.sess(), retexpr.span);
92 if !local.span.from_expansion();
94 span_lint_hir_and_then(
99 "returning the result of a `let` binding from a block",
101 err.span_label(local.span, "unnecessary `let` binding");
103 if let Some(mut snippet) = snippet_opt(cx, initexpr.span) {
104 if !cx.typeck_results().expr_adjustments(retexpr).is_empty() {
105 snippet.push_str(" as _");
107 err.multipart_suggestion(
108 "return the expression directly",
110 (local.span, String::new()),
111 (retexpr.span, snippet),
113 Applicability::MachineApplicable,
116 err.span_help(initexpr.span, "this expression can be directly returned");
126 cx: &LateContext<'tcx>,
128 _: &'tcx FnDecl<'tcx>,
129 body: &'tcx Body<'tcx>,
135 // when returning without value in closure, replace this `return`
136 // with an empty block to prevent invalid suggestion (see #6501)
137 let replacement = if let ExprKind::Ret(None) = &body.value.kind {
138 RetReplacement::Block
140 RetReplacement::Empty
142 check_final_expr(cx, &body.value, Some(body.value.span), replacement);
144 FnKind::ItemFn(..) | FnKind::Method(..) => {
145 if let ExprKind::Block(block, _) = body.value.kind {
146 check_block_return(cx, block);
153 fn check_block_return<'tcx>(cx: &LateContext<'tcx>, block: &Block<'tcx>) {
154 if let Some(expr) = block.expr {
155 check_final_expr(cx, expr, Some(expr.span), RetReplacement::Empty);
156 } else if let Some(stmt) = block.stmts.iter().last() {
158 StmtKind::Expr(expr) | StmtKind::Semi(expr) => {
159 check_final_expr(cx, expr, Some(stmt.span), RetReplacement::Empty);
166 fn check_final_expr<'tcx>(
167 cx: &LateContext<'tcx>,
168 expr: &'tcx Expr<'tcx>,
170 replacement: RetReplacement,
173 // simple return is always "bad"
174 ExprKind::Ret(ref inner) => {
175 if cx.tcx.hir().attrs(expr.hir_id).is_empty() {
176 let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
180 inner.map_or(expr.hir_id, |inner| inner.hir_id),
181 span.expect("`else return` is not possible"),
182 inner.as_ref().map(|i| i.span),
188 // a whole block? check it!
189 ExprKind::Block(block, _) => {
190 check_block_return(cx, block);
192 ExprKind::If(_, then, else_clause_opt) => {
193 if let ExprKind::Block(ifblock, _) = then.kind {
194 check_block_return(cx, ifblock);
196 if let Some(else_clause) = else_clause_opt {
197 check_final_expr(cx, else_clause, None, RetReplacement::Empty);
200 // a match expr, check all arms
201 // an if/if let expr, check both exprs
202 // note, if without else is going to be a type checking error anyways
203 // (except for unit type functions) so we don't match it
204 ExprKind::Match(_, arms, MatchSource::Normal) => {
205 for arm in arms.iter() {
206 check_final_expr(cx, arm.body, Some(arm.body.span), RetReplacement::Unit);
209 ExprKind::DropTemps(expr) => check_final_expr(cx, expr, None, RetReplacement::Empty),
215 cx: &LateContext<'_>,
216 emission_place: HirId,
218 inner_span: Option<Span>,
219 replacement: RetReplacement,
221 if ret_span.from_expansion() {
225 Some(inner_span) => {
226 let mut applicability = Applicability::MachineApplicable;
227 span_lint_hir_and_then(
232 "unneeded `return` statement",
234 let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability);
235 diag.span_suggestion(ret_span, "remove `return`", snippet, applicability);
239 None => match replacement {
240 RetReplacement::Empty => {
241 span_lint_hir_and_then(
246 "unneeded `return` statement",
248 diag.span_suggestion(
252 Applicability::MachineApplicable,
257 RetReplacement::Block => {
258 span_lint_hir_and_then(
263 "unneeded `return` statement",
265 diag.span_suggestion(
267 "replace `return` with an empty block",
269 Applicability::MachineApplicable,
274 RetReplacement::Unit => {
275 span_lint_hir_and_then(
280 "unneeded `return` statement",
282 diag.span_suggestion(
284 "replace `return` with a unit value",
286 Applicability::MachineApplicable,
295 fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
296 let mut visitor = BorrowVisitor { cx, borrows: false };
297 walk_expr(&mut visitor, expr);
301 struct BorrowVisitor<'a, 'tcx> {
302 cx: &'a LateContext<'tcx>,
306 impl<'tcx> Visitor<'tcx> for BorrowVisitor<'_, 'tcx> {
307 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
308 if self.borrows || expr.span.from_expansion() {
312 if let Some(def_id) = fn_def_id(self.cx, expr) {
320 .any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
323 walk_expr(self, expr);