]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/returns.rs
Rollup merge of #88647 - ChrisDenton:win-symlink-docs, r=joshtriplett
[rust.git] / src / tools / clippy / 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, 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, 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
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     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     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(_, 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 !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 => {
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 attr_is_cfg(attr: &Attribute) -> bool {
154     attr.meta_item_list().is_some() && attr.has_name(sym::cfg)
155 }
156
157 fn check_block_return<'tcx>(cx: &LateContext<'tcx>, block: &Block<'tcx>) {
158     if let Some(expr) = block.expr {
159         check_final_expr(cx, expr, Some(expr.span), RetReplacement::Empty);
160     } else if let Some(stmt) = block.stmts.iter().last() {
161         match stmt.kind {
162             StmtKind::Expr(expr) | StmtKind::Semi(expr) => {
163                 check_final_expr(cx, expr, Some(stmt.span), RetReplacement::Empty);
164             },
165             _ => (),
166         }
167     }
168 }
169
170 fn check_final_expr<'tcx>(
171     cx: &LateContext<'tcx>,
172     expr: &'tcx Expr<'tcx>,
173     span: Option<Span>,
174     replacement: RetReplacement,
175 ) {
176     match expr.kind {
177         // simple return is always "bad"
178         ExprKind::Ret(ref inner) => {
179             // allow `#[cfg(a)] return a; #[cfg(b)] return b;`
180             let attrs = cx.tcx.hir().attrs(expr.hir_id);
181             if !attrs.iter().any(attr_is_cfg) {
182                 let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
183                 if !borrows {
184                     emit_return_lint(
185                         cx,
186                         span.expect("`else return` is not possible"),
187                         inner.as_ref().map(|i| i.span),
188                         replacement,
189                     );
190                 }
191             }
192         },
193         // a whole block? check it!
194         ExprKind::Block(block, _) => {
195             check_block_return(cx, block);
196         },
197         ExprKind::If(_, then, else_clause_opt) => {
198             if let ExprKind::Block(ifblock, _) = then.kind {
199                 check_block_return(cx, ifblock);
200             }
201             if let Some(else_clause) = else_clause_opt {
202                 check_final_expr(cx, else_clause, None, RetReplacement::Empty);
203             }
204         },
205         // a match expr, check all arms
206         // an if/if let expr, check both exprs
207         // note, if without else is going to be a type checking error anyways
208         // (except for unit type functions) so we don't match it
209         ExprKind::Match(_, arms, source) => match source {
210             MatchSource::Normal => {
211                 for arm in arms.iter() {
212                     check_final_expr(cx, arm.body, Some(arm.body.span), RetReplacement::Block);
213                 }
214             },
215             _ => (),
216         },
217         ExprKind::DropTemps(expr) => check_final_expr(cx, expr, None, RetReplacement::Empty),
218         _ => (),
219     }
220 }
221
222 fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, inner_span: Option<Span>, replacement: RetReplacement) {
223     if ret_span.from_expansion() {
224         return;
225     }
226     match inner_span {
227         Some(inner_span) => {
228             if in_external_macro(cx.tcx.sess, inner_span) || inner_span.from_expansion() {
229                 return;
230             }
231
232             span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| {
233                 if let Some(snippet) = snippet_opt(cx, inner_span) {
234                     diag.span_suggestion(ret_span, "remove `return`", snippet, Applicability::MachineApplicable);
235                 }
236             });
237         },
238         None => match replacement {
239             RetReplacement::Empty => {
240                 span_lint_and_sugg(
241                     cx,
242                     NEEDLESS_RETURN,
243                     ret_span,
244                     "unneeded `return` statement",
245                     "remove `return`",
246                     String::new(),
247                     Applicability::MachineApplicable,
248                 );
249             },
250             RetReplacement::Block => {
251                 span_lint_and_sugg(
252                     cx,
253                     NEEDLESS_RETURN,
254                     ret_span,
255                     "unneeded `return` statement",
256                     "replace `return` with an empty block",
257                     "{}".to_string(),
258                     Applicability::MachineApplicable,
259                 );
260             },
261         },
262     }
263 }
264
265 fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
266     let mut visitor = BorrowVisitor { cx, borrows: false };
267     walk_expr(&mut visitor, expr);
268     visitor.borrows
269 }
270
271 struct BorrowVisitor<'a, 'tcx> {
272     cx: &'a LateContext<'tcx>,
273     borrows: bool,
274 }
275
276 impl<'tcx> Visitor<'tcx> for BorrowVisitor<'_, 'tcx> {
277     type Map = Map<'tcx>;
278
279     fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
280         if self.borrows {
281             return;
282         }
283
284         if let Some(def_id) = fn_def_id(self.cx, expr) {
285             self.borrows = self
286                 .cx
287                 .tcx
288                 .fn_sig(def_id)
289                 .output()
290                 .skip_binder()
291                 .walk(self.cx.tcx)
292                 .any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
293         }
294
295         walk_expr(self, expr);
296     }
297
298     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
299         NestedVisitorMap::None
300     }
301 }