1 use if_chain::if_chain;
2 use rustc_errors::Applicability;
3 use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
4 use rustc_hir::{Block, Expr, ExprKind, PatKind, StmtKind};
5 use rustc_lint::{LateContext, LateLintPass, LintContext};
6 use rustc_middle::hir::map::Map;
7 use rustc_middle::lint::in_external_macro;
8 use rustc_middle::ty::subst::GenericArgKind;
9 use rustc_session::{declare_lint_pass, declare_tool_lint};
11 use crate::utils::{fn_def_id, in_macro, match_qpath, snippet_opt, span_lint_and_then};
13 declare_clippy_lint! {
14 /// **What it does:** Checks for `let`-bindings, which are subsequently
17 /// **Why is this bad?** It is just extraneous code. Remove it to make your code
20 /// **Known problems:** None.
24 /// fn foo() -> String {
25 /// let x = String::new();
31 /// fn foo() -> String {
37 "creating a let-binding and then immediately returning it like `let x = expr; x` at the end of a block"
40 declare_lint_pass!(LetReturn => [LET_AND_RETURN]);
42 impl<'tcx> LateLintPass<'tcx> for LetReturn {
43 fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'_>) {
44 // we need both a let-binding stmt and an expr
46 if let Some(retexpr) = block.expr;
47 if let Some(stmt) = block.stmts.iter().last();
48 if let StmtKind::Local(local) = &stmt.kind;
49 if local.ty.is_none();
50 if local.attrs.is_empty();
51 if let Some(initexpr) = &local.init;
52 if let PatKind::Binding(.., ident, _) = local.pat.kind;
53 if let ExprKind::Path(qpath) = &retexpr.kind;
54 if match_qpath(qpath, &[&*ident.name.as_str()]);
55 if !last_statement_borrows(cx, initexpr);
56 if !in_external_macro(cx.sess(), initexpr.span);
57 if !in_external_macro(cx.sess(), retexpr.span);
58 if !in_external_macro(cx.sess(), local.span);
59 if !in_macro(local.span);
65 "returning the result of a `let` binding from a block",
67 err.span_label(local.span, "unnecessary `let` binding");
69 if let Some(snippet) = snippet_opt(cx, initexpr.span) {
70 err.multipart_suggestion(
71 "return the expression directly",
73 (local.span, String::new()),
74 (retexpr.span, snippet),
76 Applicability::MachineApplicable,
79 err.span_help(initexpr.span, "this expression can be directly returned");
88 fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
89 let mut visitor = BorrowVisitor { cx, borrows: false };
90 walk_expr(&mut visitor, expr);
94 struct BorrowVisitor<'a, 'tcx> {
95 cx: &'a LateContext<'tcx>,
99 impl<'tcx> Visitor<'tcx> for BorrowVisitor<'_, 'tcx> {
100 type Map = Map<'tcx>;
102 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
107 if let Some(def_id) = fn_def_id(self.cx, expr) {
115 .any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
118 walk_expr(self, expr);
121 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
122 NestedVisitorMap::None