]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/returns.rs
Rollup merge of #102846 - zertosh:update-syn, r=dtolnay
[rust.git] / src / tools / clippy / clippy_lints / src / returns.rs
1 use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
2 use clippy_utils::source::{snippet_opt, snippet_with_context};
3 use clippy_utils::visitors::{for_each_expr, Descend};
4 use clippy_utils::{fn_def_id, path_to_local_id};
5 use core::ops::ControlFlow;
6 use if_chain::if_chain;
7 use rustc_errors::Applicability;
8 use rustc_hir::intravisit::FnKind;
9 use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, HirId, MatchSource, PatKind, StmtKind};
10 use rustc_lint::{LateContext, LateLintPass, LintContext};
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
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 impl RetReplacement {
78     fn sugg_help(self) -> &'static str {
79         match self {
80             Self::Empty => "remove `return`",
81             Self::Block => "replace `return` with an empty block",
82             Self::Unit => "replace `return` with a unit value",
83         }
84     }
85 }
86
87 impl ToString for RetReplacement {
88     fn to_string(&self) -> String {
89         match *self {
90             Self::Empty => "",
91             Self::Block => "{}",
92             Self::Unit => "()",
93         }
94         .to_string()
95     }
96 }
97
98 declare_lint_pass!(Return => [LET_AND_RETURN, NEEDLESS_RETURN]);
99
100 impl<'tcx> LateLintPass<'tcx> for Return {
101     fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'_>) {
102         // we need both a let-binding stmt and an expr
103         if_chain! {
104             if let Some(retexpr) = block.expr;
105             if let Some(stmt) = block.stmts.iter().last();
106             if let StmtKind::Local(local) = &stmt.kind;
107             if local.ty.is_none();
108             if cx.tcx.hir().attrs(local.hir_id).is_empty();
109             if let Some(initexpr) = &local.init;
110             if let PatKind::Binding(_, local_id, _, _) = local.pat.kind;
111             if path_to_local_id(retexpr, local_id);
112             if !last_statement_borrows(cx, initexpr);
113             if !in_external_macro(cx.sess(), initexpr.span);
114             if !in_external_macro(cx.sess(), retexpr.span);
115             if !local.span.from_expansion();
116             then {
117                 span_lint_hir_and_then(
118                     cx,
119                     LET_AND_RETURN,
120                     retexpr.hir_id,
121                     retexpr.span,
122                     "returning the result of a `let` binding from a block",
123                     |err| {
124                         err.span_label(local.span, "unnecessary `let` binding");
125
126                         if let Some(mut snippet) = snippet_opt(cx, initexpr.span) {
127                             if !cx.typeck_results().expr_adjustments(retexpr).is_empty() {
128                                 snippet.push_str(" as _");
129                             }
130                             err.multipart_suggestion(
131                                 "return the expression directly",
132                                 vec![
133                                     (local.span, String::new()),
134                                     (retexpr.span, snippet),
135                                 ],
136                                 Applicability::MachineApplicable,
137                             );
138                         } else {
139                             err.span_help(initexpr.span, "this expression can be directly returned");
140                         }
141                     },
142                 );
143             }
144         }
145     }
146
147     fn check_fn(
148         &mut self,
149         cx: &LateContext<'tcx>,
150         kind: FnKind<'tcx>,
151         _: &'tcx FnDecl<'tcx>,
152         body: &'tcx Body<'tcx>,
153         _: Span,
154         _: HirId,
155     ) {
156         match kind {
157             FnKind::Closure => {
158                 // when returning without value in closure, replace this `return`
159                 // with an empty block to prevent invalid suggestion (see #6501)
160                 let replacement = if let ExprKind::Ret(None) = &body.value.kind {
161                     RetReplacement::Block
162                 } else {
163                     RetReplacement::Empty
164                 };
165                 check_final_expr(cx, body.value, vec![], replacement);
166             },
167             FnKind::ItemFn(..) | FnKind::Method(..) => {
168                 check_block_return(cx, &body.value.kind, vec![]);
169             },
170         }
171     }
172 }
173
174 // if `expr` is a block, check if there are needless returns in it
175 fn check_block_return<'tcx>(cx: &LateContext<'tcx>, expr_kind: &ExprKind<'tcx>, semi_spans: Vec<Span>) {
176     if let ExprKind::Block(block, _) = expr_kind {
177         if let Some(block_expr) = block.expr {
178             check_final_expr(cx, block_expr, semi_spans, RetReplacement::Empty);
179         } else if let Some(stmt) = block.stmts.iter().last() {
180             match stmt.kind {
181                 StmtKind::Expr(expr) => {
182                     check_final_expr(cx, expr, semi_spans, RetReplacement::Empty);
183                 },
184                 StmtKind::Semi(semi_expr) => {
185                     let mut semi_spans_and_this_one = semi_spans;
186                     // we only want the span containing the semicolon so we can remove it later. From `entry.rs:382`
187                     if let Some(semicolon_span) = stmt.span.trim_start(semi_expr.span) {
188                         semi_spans_and_this_one.push(semicolon_span);
189                         check_final_expr(cx, semi_expr, semi_spans_and_this_one, RetReplacement::Empty);
190                     }
191                 },
192                 _ => (),
193             }
194         }
195     }
196 }
197
198 fn check_final_expr<'tcx>(
199     cx: &LateContext<'tcx>,
200     expr: &'tcx Expr<'tcx>,
201     semi_spans: Vec<Span>, /* containing all the places where we would need to remove semicolons if finding an
202                             * needless return */
203     replacement: RetReplacement,
204 ) {
205     let peeled_drop_expr = expr.peel_drop_temps();
206     match &peeled_drop_expr.kind {
207         // simple return is always "bad"
208         ExprKind::Ret(ref inner) => {
209             if cx.tcx.hir().attrs(expr.hir_id).is_empty() {
210                 let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
211                 if !borrows {
212                     emit_return_lint(
213                         cx,
214                         peeled_drop_expr.span,
215                         semi_spans,
216                         inner.as_ref().map(|i| i.span),
217                         replacement,
218                     );
219                 }
220             }
221         },
222         ExprKind::If(_, then, else_clause_opt) => {
223             check_block_return(cx, &then.kind, semi_spans.clone());
224             if let Some(else_clause) = else_clause_opt {
225                 check_block_return(cx, &else_clause.kind, semi_spans);
226             }
227         },
228         // a match expr, check all arms
229         // an if/if let expr, check both exprs
230         // note, if without else is going to be a type checking error anyways
231         // (except for unit type functions) so we don't match it
232         ExprKind::Match(_, arms, MatchSource::Normal) => {
233             for arm in arms.iter() {
234                 check_final_expr(cx, arm.body, semi_spans.clone(), RetReplacement::Unit);
235             }
236         },
237         // if it's a whole block, check it
238         other_expr_kind => check_block_return(cx, other_expr_kind, semi_spans),
239     }
240 }
241
242 fn emit_return_lint(
243     cx: &LateContext<'_>,
244     ret_span: Span,
245     semi_spans: Vec<Span>,
246     inner_span: Option<Span>,
247     replacement: RetReplacement,
248 ) {
249     if ret_span.from_expansion() {
250         return;
251     }
252     let mut applicability = Applicability::MachineApplicable;
253     let return_replacement = inner_span.map_or_else(
254         || replacement.to_string(),
255         |inner_span| {
256             let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability);
257             snippet.to_string()
258         },
259     );
260     let sugg_help = if inner_span.is_some() {
261         "remove `return`"
262     } else {
263         replacement.sugg_help()
264     };
265     span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| {
266         diag.span_suggestion_hidden(ret_span, sugg_help, return_replacement, applicability);
267         // for each parent statement, we need to remove the semicolon
268         for semi_stmt_span in semi_spans {
269             diag.tool_only_span_suggestion(semi_stmt_span, "remove this semicolon", "", applicability);
270         }
271     });
272 }
273
274 fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
275     for_each_expr(expr, |e| {
276         if let Some(def_id) = fn_def_id(cx, e)
277             && cx
278                 .tcx
279                 .fn_sig(def_id)
280                 .skip_binder()
281                 .output()
282                 .walk()
283                 .any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(_)))
284         {
285             ControlFlow::Break(())
286         } else {
287             ControlFlow::Continue(Descend::from(!expr.span.from_expansion()))
288         }
289     })
290     .is_some()
291 }