]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/let_and_return.rs
Rollup merge of #74334 - jyn514:config-toml-docs, r=spastorino
[rust.git] / src / tools / clippy / clippy_lints / src / let_and_return.rs
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};
10
11 use crate::utils::{fn_def_id, in_macro, match_qpath, snippet_opt, span_lint_and_then};
12
13 declare_clippy_lint! {
14     /// **What it does:** Checks for `let`-bindings, which are subsequently
15     /// returned.
16     ///
17     /// **Why is this bad?** It is just extraneous code. Remove it to make your code
18     /// more rusty.
19     ///
20     /// **Known problems:** None.
21     ///
22     /// **Example:**
23     /// ```rust
24     /// fn foo() -> String {
25     ///     let x = String::new();
26     ///     x
27     /// }
28     /// ```
29     /// instead, use
30     /// ```
31     /// fn foo() -> String {
32     ///     String::new()
33     /// }
34     /// ```
35     pub LET_AND_RETURN,
36     style,
37     "creating a let-binding and then immediately returning it like `let x = expr; x` at the end of a block"
38 }
39
40 declare_lint_pass!(LetReturn => [LET_AND_RETURN]);
41
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
45         if_chain! {
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);
60             then {
61                 span_lint_and_then(
62                     cx,
63                     LET_AND_RETURN,
64                     retexpr.span,
65                     "returning the result of a `let` binding from a block",
66                     |err| {
67                         err.span_label(local.span, "unnecessary `let` binding");
68
69                         if let Some(snippet) = snippet_opt(cx, initexpr.span) {
70                             err.multipart_suggestion(
71                                 "return the expression directly",
72                                 vec![
73                                     (local.span, String::new()),
74                                     (retexpr.span, snippet),
75                                 ],
76                                 Applicability::MachineApplicable,
77                             );
78                         } else {
79                             err.span_help(initexpr.span, "this expression can be directly returned");
80                         }
81                     },
82                 );
83             }
84         }
85     }
86 }
87
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);
91     visitor.borrows
92 }
93
94 struct BorrowVisitor<'a, 'tcx> {
95     cx: &'a LateContext<'tcx>,
96     borrows: bool,
97 }
98
99 impl<'tcx> Visitor<'tcx> for BorrowVisitor<'_, 'tcx> {
100     type Map = Map<'tcx>;
101
102     fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
103         if self.borrows {
104             return;
105         }
106
107         if let Some(def_id) = fn_def_id(self.cx, expr) {
108             self.borrows = self
109                 .cx
110                 .tcx
111                 .fn_sig(def_id)
112                 .output()
113                 .skip_binder()
114                 .walk()
115                 .any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
116         }
117
118         walk_expr(self, expr);
119     }
120
121     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
122         NestedVisitorMap::None
123     }
124 }