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