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