]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/returns.rs
Auto merge of #6586 - flip1995:rustup, r=flip1995
[rust.git] / clippy_lints / src / returns.rs
1 use if_chain::if_chain;
2 use rustc_ast::ast::Attribute;
3 use rustc_errors::Applicability;
4 use rustc_hir::intravisit::{walk_expr, FnKind, NestedVisitorMap, Visitor};
5 use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, HirId, MatchSource, PatKind, StmtKind};
6 use rustc_lint::{LateContext, LateLintPass, LintContext};
7 use rustc_middle::hir::map::Map;
8 use rustc_middle::lint::in_external_macro;
9 use rustc_middle::ty::subst::GenericArgKind;
10 use rustc_session::{declare_lint_pass, declare_tool_lint};
11 use rustc_span::source_map::Span;
12 use rustc_span::sym;
13
14 use crate::utils::{fn_def_id, in_macro, match_qpath, snippet_opt, span_lint_and_sugg, span_lint_and_then};
15
16 declare_clippy_lint! {
17     /// **What it does:** Checks for `let`-bindings, which are subsequently
18     /// returned.
19     ///
20     /// **Why is this bad?** It is just extraneous code. Remove it to make your code
21     /// more rusty.
22     ///
23     /// **Known problems:** None.
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     pub LET_AND_RETURN,
39     style,
40     "creating a let-binding and then immediately returning it like `let x = expr; x` at the end of a block"
41 }
42
43 declare_clippy_lint! {
44     /// **What it does:** Checks for return statements at the end of a block.
45     ///
46     /// **Why is this bad?** Removing the `return` and semicolon will make the code
47     /// more rusty.
48     ///
49     /// **Known problems:** None.
50     ///
51     /// **Example:**
52     /// ```rust
53     /// fn foo(x: usize) -> usize {
54     ///     return x;
55     /// }
56     /// ```
57     /// simplify to
58     /// ```rust
59     /// fn foo(x: usize) -> usize {
60     ///     x
61     /// }
62     /// ```
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 }
73
74 declare_lint_pass!(Return => [LET_AND_RETURN, NEEDLESS_RETURN]);
75
76 impl<'tcx> LateLintPass<'tcx> for Return {
77     fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'_>) {
78         // we need both a let-binding stmt and an expr
79         if_chain! {
80             if let Some(retexpr) = block.expr;
81             if let Some(stmt) = block.stmts.iter().last();
82             if let StmtKind::Local(local) = &stmt.kind;
83             if local.ty.is_none();
84             if local.attrs.is_empty();
85             if let Some(initexpr) = &local.init;
86             if let PatKind::Binding(.., ident, _) = local.pat.kind;
87             if let ExprKind::Path(qpath) = &retexpr.kind;
88             if match_qpath(qpath, &[&*ident.name.as_str()]);
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 !in_external_macro(cx.sess(), local.span);
93             if !in_macro(local.span);
94             then {
95                 span_lint_and_then(
96                     cx,
97                     LET_AND_RETURN,
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(_) => check_final_expr(cx, &body.value, Some(body.value.span), RetReplacement::Empty),
135             FnKind::ItemFn(..) | FnKind::Method(..) => {
136                 if let ExprKind::Block(ref block, _) = body.value.kind {
137                     check_block_return(cx, block);
138                 }
139             },
140         }
141     }
142 }
143
144 fn attr_is_cfg(attr: &Attribute) -> bool {
145     attr.meta_item_list().is_some() && attr.has_name(sym::cfg)
146 }
147
148 fn check_block_return<'tcx>(cx: &LateContext<'tcx>, block: &Block<'tcx>) {
149     if let Some(expr) = block.expr {
150         check_final_expr(cx, expr, Some(expr.span), RetReplacement::Empty);
151     } else if let Some(stmt) = block.stmts.iter().last() {
152         match stmt.kind {
153             StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => {
154                 check_final_expr(cx, expr, Some(stmt.span), RetReplacement::Empty);
155             },
156             _ => (),
157         }
158     }
159 }
160
161 fn check_final_expr<'tcx>(
162     cx: &LateContext<'tcx>,
163     expr: &'tcx Expr<'tcx>,
164     span: Option<Span>,
165     replacement: RetReplacement,
166 ) {
167     match expr.kind {
168         // simple return is always "bad"
169         ExprKind::Ret(ref inner) => {
170             // allow `#[cfg(a)] return a; #[cfg(b)] return b;`
171             if !expr.attrs.iter().any(attr_is_cfg) {
172                 let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
173                 if !borrows {
174                     emit_return_lint(
175                         cx,
176                         span.expect("`else return` is not possible"),
177                         inner.as_ref().map(|i| i.span),
178                         replacement,
179                     );
180                 }
181             }
182         },
183         // a whole block? check it!
184         ExprKind::Block(ref block, _) => {
185             check_block_return(cx, block);
186         },
187         ExprKind::If(_, then, else_clause_opt) => {
188             if let ExprKind::Block(ref ifblock, _) = then.kind {
189                 check_block_return(cx, ifblock);
190             }
191             if let Some(else_clause) = else_clause_opt {
192                 check_final_expr(cx, else_clause, None, RetReplacement::Empty);
193             }
194         },
195         // a match expr, check all arms
196         // an if/if let expr, check both exprs
197         // note, if without else is going to be a type checking error anyways
198         // (except for unit type functions) so we don't match it
199         ExprKind::Match(_, ref arms, source) => match source {
200             MatchSource::Normal => {
201                 for arm in arms.iter() {
202                     check_final_expr(cx, &arm.body, Some(arm.body.span), RetReplacement::Block);
203                 }
204             },
205             MatchSource::IfLetDesugar {
206                 contains_else_clause: true,
207             } => {
208                 if let ExprKind::Block(ref ifblock, _) = arms[0].body.kind {
209                     check_block_return(cx, ifblock);
210                 }
211                 check_final_expr(cx, arms[1].body, None, RetReplacement::Empty);
212             },
213             _ => (),
214         },
215         _ => (),
216     }
217 }
218
219 fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, inner_span: Option<Span>, replacement: RetReplacement) {
220     if ret_span.from_expansion() {
221         return;
222     }
223     match inner_span {
224         Some(inner_span) => {
225             if in_external_macro(cx.tcx.sess, inner_span) || inner_span.from_expansion() {
226                 return;
227             }
228
229             span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| {
230                 if let Some(snippet) = snippet_opt(cx, inner_span) {
231                     diag.span_suggestion(ret_span, "remove `return`", snippet, Applicability::MachineApplicable);
232                 }
233             })
234         },
235         None => match replacement {
236             RetReplacement::Empty => {
237                 span_lint_and_sugg(
238                     cx,
239                     NEEDLESS_RETURN,
240                     ret_span,
241                     "unneeded `return` statement",
242                     "remove `return`",
243                     String::new(),
244                     Applicability::MachineApplicable,
245                 );
246             },
247             RetReplacement::Block => {
248                 span_lint_and_sugg(
249                     cx,
250                     NEEDLESS_RETURN,
251                     ret_span,
252                     "unneeded `return` statement",
253                     "replace `return` with an empty block",
254                     "{}".to_string(),
255                     Applicability::MachineApplicable,
256                 );
257             },
258         },
259     }
260 }
261
262 fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
263     let mut visitor = BorrowVisitor { cx, borrows: false };
264     walk_expr(&mut visitor, expr);
265     visitor.borrows
266 }
267
268 struct BorrowVisitor<'a, 'tcx> {
269     cx: &'a LateContext<'tcx>,
270     borrows: bool,
271 }
272
273 impl<'tcx> Visitor<'tcx> for BorrowVisitor<'_, 'tcx> {
274     type Map = Map<'tcx>;
275
276     fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
277         if self.borrows {
278             return;
279         }
280
281         if let Some(def_id) = fn_def_id(self.cx, expr) {
282             self.borrows = self
283                 .cx
284                 .tcx
285                 .fn_sig(def_id)
286                 .output()
287                 .skip_binder()
288                 .walk()
289                 .any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
290         }
291
292         walk_expr(self, expr);
293     }
294
295     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
296         NestedVisitorMap::None
297     }
298 }