1 use if_chain::if_chain;
2 use rustc_ast::ast::Attribute;
3 use rustc_errors::Applicability;
4 use rustc_hir::intravisit::{walk_expr, FnKind, NestedVisitorMap, Visitor};
5 use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, HirId, MatchSource, PatKind, StmtKind};
6 use rustc_lint::{LateContext, LateLintPass, LintContext};
7 use rustc_middle::hir::map::Map;
8 use rustc_middle::lint::in_external_macro;
9 use rustc_middle::ty::subst::GenericArgKind;
10 use rustc_session::{declare_lint_pass, declare_tool_lint};
11 use rustc_span::source_map::Span;
14 use crate::utils::{fn_def_id, in_macro, match_qpath, snippet_opt, span_lint_and_sugg, span_lint_and_then};
16 declare_clippy_lint! {
17 /// **What it does:** Checks for `let`-bindings, which are subsequently
20 /// **Why is this bad?** It is just extraneous code. Remove it to make your code
23 /// **Known problems:** None.
27 /// fn foo() -> String {
28 /// let x = String::new();
34 /// fn foo() -> String {
40 "creating a let-binding and then immediately returning it like `let x = expr; x` at the end of a block"
43 declare_clippy_lint! {
44 /// **What it does:** Checks for return statements at the end of a block.
46 /// **Why is this bad?** Removing the `return` and semicolon will make the code
49 /// **Known problems:** None.
53 /// fn foo(x: usize) -> usize {
59 /// fn foo(x: usize) -> usize {
65 "using a return statement like `return expr;` where an expression would suffice"
68 #[derive(PartialEq, Eq, Copy, Clone)]
74 declare_lint_pass!(Return => [LET_AND_RETURN, NEEDLESS_RETURN]);
76 impl<'tcx> LateLintPass<'tcx> for Return {
77 fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'_>) {
78 // we need both a let-binding stmt and an expr
80 if let Some(retexpr) = block.expr;
81 if let Some(stmt) = block.stmts.iter().last();
82 if let StmtKind::Local(local) = &stmt.kind;
83 if local.ty.is_none();
84 if local.attrs.is_empty();
85 if let Some(initexpr) = &local.init;
86 if let PatKind::Binding(.., ident, _) = local.pat.kind;
87 if let ExprKind::Path(qpath) = &retexpr.kind;
88 if match_qpath(qpath, &[&*ident.name.as_str()]);
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 !in_external_macro(cx.sess(), local.span);
93 if !in_macro(local.span);
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(ref block, _) = body.value.kind {
146 check_block_return(cx, block);
153 fn attr_is_cfg(attr: &Attribute) -> bool {
154 attr.meta_item_list().is_some() && attr.has_name(sym::cfg)
157 fn check_block_return<'tcx>(cx: &LateContext<'tcx>, block: &Block<'tcx>) {
158 if let Some(expr) = block.expr {
159 check_final_expr(cx, expr, Some(expr.span), RetReplacement::Empty);
160 } else if let Some(stmt) = block.stmts.iter().last() {
162 StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => {
163 check_final_expr(cx, expr, Some(stmt.span), RetReplacement::Empty);
170 fn check_final_expr<'tcx>(
171 cx: &LateContext<'tcx>,
172 expr: &'tcx Expr<'tcx>,
174 replacement: RetReplacement,
177 // simple return is always "bad"
178 ExprKind::Ret(ref inner) => {
179 // allow `#[cfg(a)] return a; #[cfg(b)] return b;`
180 if !expr.attrs.iter().any(attr_is_cfg) {
181 let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
185 span.expect("`else return` is not possible"),
186 inner.as_ref().map(|i| i.span),
192 // a whole block? check it!
193 ExprKind::Block(ref block, _) => {
194 check_block_return(cx, block);
196 ExprKind::If(_, then, else_clause_opt) => {
197 if let ExprKind::Block(ref ifblock, _) = then.kind {
198 check_block_return(cx, ifblock);
200 if let Some(else_clause) = else_clause_opt {
201 check_final_expr(cx, else_clause, None, RetReplacement::Empty);
204 // a match expr, check all arms
205 // an if/if let expr, check both exprs
206 // note, if without else is going to be a type checking error anyways
207 // (except for unit type functions) so we don't match it
208 ExprKind::Match(_, ref arms, source) => match source {
209 MatchSource::Normal => {
210 for arm in arms.iter() {
211 check_final_expr(cx, &arm.body, Some(arm.body.span), RetReplacement::Block);
214 MatchSource::IfLetDesugar {
215 contains_else_clause: true,
217 if let ExprKind::Block(ref ifblock, _) = arms[0].body.kind {
218 check_block_return(cx, ifblock);
220 check_final_expr(cx, arms[1].body, None, RetReplacement::Empty);
228 fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, inner_span: Option<Span>, replacement: RetReplacement) {
229 if ret_span.from_expansion() {
233 Some(inner_span) => {
234 if in_external_macro(cx.tcx.sess, inner_span) || inner_span.from_expansion() {
238 span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| {
239 if let Some(snippet) = snippet_opt(cx, inner_span) {
240 diag.span_suggestion(ret_span, "remove `return`", snippet, Applicability::MachineApplicable);
244 None => match replacement {
245 RetReplacement::Empty => {
250 "unneeded `return` statement",
253 Applicability::MachineApplicable,
256 RetReplacement::Block => {
261 "unneeded `return` statement",
262 "replace `return` with an empty block",
264 Applicability::MachineApplicable,
271 fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
272 let mut visitor = BorrowVisitor { cx, borrows: false };
273 walk_expr(&mut visitor, expr);
277 struct BorrowVisitor<'a, 'tcx> {
278 cx: &'a LateContext<'tcx>,
282 impl<'tcx> Visitor<'tcx> for BorrowVisitor<'_, 'tcx> {
283 type Map = Map<'tcx>;
285 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
290 if let Some(def_id) = fn_def_id(self.cx, expr) {
298 .any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
301 walk_expr(self, expr);
304 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
305 NestedVisitorMap::None