]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/returns.rs
Auto merge of #6931 - Jarcho:needless_borrow, r=phansch,flip1995
[rust.git] / clippy_lints / src / returns.rs
1 use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
2 use clippy_utils::source::snippet_opt;
3 use clippy_utils::{fn_def_id, in_macro, match_qpath};
4 use if_chain::if_chain;
5 use rustc_ast::ast::Attribute;
6 use rustc_errors::Applicability;
7 use rustc_hir::intravisit::{walk_expr, FnKind, NestedVisitorMap, Visitor};
8 use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, HirId, MatchSource, PatKind, StmtKind};
9 use rustc_lint::{LateContext, LateLintPass, LintContext};
10 use rustc_middle::hir::map::Map;
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;
15 use rustc_span::sym;
16
17 declare_clippy_lint! {
18     /// **What it does:** Checks for `let`-bindings, which are subsequently
19     /// returned.
20     ///
21     /// **Why is this bad?** It is just extraneous code. Remove it to make your code
22     /// more rusty.
23     ///
24     /// **Known problems:** None.
25     ///
26     /// **Example:**
27     /// ```rust
28     /// fn foo() -> String {
29     ///     let x = String::new();
30     ///     x
31     /// }
32     /// ```
33     /// instead, use
34     /// ```
35     /// fn foo() -> String {
36     ///     String::new()
37     /// }
38     /// ```
39     pub LET_AND_RETURN,
40     style,
41     "creating a let-binding and then immediately returning it like `let x = expr; x` at the end of a block"
42 }
43
44 declare_clippy_lint! {
45     /// **What it does:** Checks for return statements at the end of a block.
46     ///
47     /// **Why is this bad?** Removing the `return` and semicolon will make the code
48     /// more rusty.
49     ///
50     /// **Known problems:** None.
51     ///
52     /// **Example:**
53     /// ```rust
54     /// fn foo(x: usize) -> usize {
55     ///     return x;
56     /// }
57     /// ```
58     /// simplify to
59     /// ```rust
60     /// fn foo(x: usize) -> usize {
61     ///     x
62     /// }
63     /// ```
64     pub NEEDLESS_RETURN,
65     style,
66     "using a return statement like `return expr;` where an expression would suffice"
67 }
68
69 #[derive(PartialEq, Eq, Copy, Clone)]
70 enum RetReplacement {
71     Empty,
72     Block,
73 }
74
75 declare_lint_pass!(Return => [LET_AND_RETURN, NEEDLESS_RETURN]);
76
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
80         if_chain! {
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(.., ident, _) = local.pat.kind;
88             if let ExprKind::Path(qpath) = &retexpr.kind;
89             if match_qpath(qpath, &[&*ident.name.as_str()]);
90             if !last_statement_borrows(cx, initexpr);
91             if !in_external_macro(cx.sess(), initexpr.span);
92             if !in_external_macro(cx.sess(), retexpr.span);
93             if !in_external_macro(cx.sess(), local.span);
94             if !in_macro(local.span);
95             then {
96                 span_lint_and_then(
97                     cx,
98                     LET_AND_RETURN,
99                     retexpr.span,
100                     "returning the result of a `let` binding from a block",
101                     |err| {
102                         err.span_label(local.span, "unnecessary `let` binding");
103
104                         if let Some(mut snippet) = snippet_opt(cx, initexpr.span) {
105                             if !cx.typeck_results().expr_adjustments(retexpr).is_empty() {
106                                 snippet.push_str(" as _");
107                             }
108                             err.multipart_suggestion(
109                                 "return the expression directly",
110                                 vec![
111                                     (local.span, String::new()),
112                                     (retexpr.span, snippet),
113                                 ],
114                                 Applicability::MachineApplicable,
115                             );
116                         } else {
117                             err.span_help(initexpr.span, "this expression can be directly returned");
118                         }
119                     },
120                 );
121             }
122         }
123     }
124
125     fn check_fn(
126         &mut self,
127         cx: &LateContext<'tcx>,
128         kind: FnKind<'tcx>,
129         _: &'tcx FnDecl<'tcx>,
130         body: &'tcx Body<'tcx>,
131         _: Span,
132         _: HirId,
133     ) {
134         match kind {
135             FnKind::Closure => {
136                 // when returning without value in closure, replace this `return`
137                 // with an empty block to prevent invalid suggestion (see #6501)
138                 let replacement = if let ExprKind::Ret(None) = &body.value.kind {
139                     RetReplacement::Block
140                 } else {
141                     RetReplacement::Empty
142                 };
143                 check_final_expr(cx, &body.value, Some(body.value.span), replacement)
144             },
145             FnKind::ItemFn(..) | FnKind::Method(..) => {
146                 if let ExprKind::Block(block, _) = body.value.kind {
147                     check_block_return(cx, block);
148                 }
149             },
150         }
151     }
152 }
153
154 fn attr_is_cfg(attr: &Attribute) -> bool {
155     attr.meta_item_list().is_some() && attr.has_name(sym::cfg)
156 }
157
158 fn check_block_return<'tcx>(cx: &LateContext<'tcx>, block: &Block<'tcx>) {
159     if let Some(expr) = block.expr {
160         check_final_expr(cx, expr, Some(expr.span), RetReplacement::Empty);
161     } else if let Some(stmt) = block.stmts.iter().last() {
162         match stmt.kind {
163             StmtKind::Expr(expr) | StmtKind::Semi(expr) => {
164                 check_final_expr(cx, expr, Some(stmt.span), RetReplacement::Empty);
165             },
166             _ => (),
167         }
168     }
169 }
170
171 fn check_final_expr<'tcx>(
172     cx: &LateContext<'tcx>,
173     expr: &'tcx Expr<'tcx>,
174     span: Option<Span>,
175     replacement: RetReplacement,
176 ) {
177     match expr.kind {
178         // simple return is always "bad"
179         ExprKind::Ret(ref inner) => {
180             // allow `#[cfg(a)] return a; #[cfg(b)] return b;`
181             let attrs = cx.tcx.hir().attrs(expr.hir_id);
182             if !attrs.iter().any(attr_is_cfg) {
183                 let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
184                 if !borrows {
185                     emit_return_lint(
186                         cx,
187                         span.expect("`else return` is not possible"),
188                         inner.as_ref().map(|i| i.span),
189                         replacement,
190                     );
191                 }
192             }
193         },
194         // a whole block? check it!
195         ExprKind::Block(block, _) => {
196             check_block_return(cx, block);
197         },
198         ExprKind::If(_, then, else_clause_opt) => {
199             if let ExprKind::Block(ifblock, _) = then.kind {
200                 check_block_return(cx, ifblock);
201             }
202             if let Some(else_clause) = else_clause_opt {
203                 check_final_expr(cx, else_clause, None, RetReplacement::Empty);
204             }
205         },
206         // a match expr, check all arms
207         // an if/if let expr, check both exprs
208         // note, if without else is going to be a type checking error anyways
209         // (except for unit type functions) so we don't match it
210         ExprKind::Match(_, arms, source) => match source {
211             MatchSource::Normal => {
212                 for arm in arms.iter() {
213                     check_final_expr(cx, arm.body, Some(arm.body.span), RetReplacement::Block);
214                 }
215             },
216             MatchSource::IfLetDesugar {
217                 contains_else_clause: true,
218             } => {
219                 if let ExprKind::Block(ifblock, _) = arms[0].body.kind {
220                     check_block_return(cx, ifblock);
221                 }
222                 check_final_expr(cx, arms[1].body, None, RetReplacement::Empty);
223             },
224             _ => (),
225         },
226         _ => (),
227     }
228 }
229
230 fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, inner_span: Option<Span>, replacement: RetReplacement) {
231     if ret_span.from_expansion() {
232         return;
233     }
234     match inner_span {
235         Some(inner_span) => {
236             if in_external_macro(cx.tcx.sess, inner_span) || inner_span.from_expansion() {
237                 return;
238             }
239
240             span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| {
241                 if let Some(snippet) = snippet_opt(cx, inner_span) {
242                     diag.span_suggestion(ret_span, "remove `return`", snippet, Applicability::MachineApplicable);
243                 }
244             })
245         },
246         None => match replacement {
247             RetReplacement::Empty => {
248                 span_lint_and_sugg(
249                     cx,
250                     NEEDLESS_RETURN,
251                     ret_span,
252                     "unneeded `return` statement",
253                     "remove `return`",
254                     String::new(),
255                     Applicability::MachineApplicable,
256                 );
257             },
258             RetReplacement::Block => {
259                 span_lint_and_sugg(
260                     cx,
261                     NEEDLESS_RETURN,
262                     ret_span,
263                     "unneeded `return` statement",
264                     "replace `return` with an empty block",
265                     "{}".to_string(),
266                     Applicability::MachineApplicable,
267                 );
268             },
269         },
270     }
271 }
272
273 fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
274     let mut visitor = BorrowVisitor { cx, borrows: false };
275     walk_expr(&mut visitor, expr);
276     visitor.borrows
277 }
278
279 struct BorrowVisitor<'a, 'tcx> {
280     cx: &'a LateContext<'tcx>,
281     borrows: bool,
282 }
283
284 impl<'tcx> Visitor<'tcx> for BorrowVisitor<'_, 'tcx> {
285     type Map = Map<'tcx>;
286
287     fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
288         if self.borrows {
289             return;
290         }
291
292         if let Some(def_id) = fn_def_id(self.cx, expr) {
293             self.borrows = self
294                 .cx
295                 .tcx
296                 .fn_sig(def_id)
297                 .output()
298                 .skip_binder()
299                 .walk()
300                 .any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
301         }
302
303         walk_expr(self, expr);
304     }
305
306     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
307         NestedVisitorMap::None
308     }
309 }