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