]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/unnecessary_lazy_eval.rs
Rollup merge of #105523 - estebank:suggest-collect-vec, r=compiler-errors
[rust.git] / src / tools / clippy / clippy_lints / src / methods / unnecessary_lazy_eval.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::source::snippet;
3 use clippy_utils::ty::is_type_diagnostic_item;
4 use clippy_utils::{eager_or_lazy, is_from_proc_macro, usage};
5 use rustc_errors::Applicability;
6 use rustc_hir as hir;
7 use rustc_lint::LateContext;
8 use rustc_span::sym;
9
10 use super::UNNECESSARY_LAZY_EVALUATIONS;
11
12 /// lint use of `<fn>_else(simple closure)` for `Option`s and `Result`s that can be
13 /// replaced with `<fn>(return value of simple closure)`
14 pub(super) fn check<'tcx>(
15     cx: &LateContext<'tcx>,
16     expr: &'tcx hir::Expr<'_>,
17     recv: &'tcx hir::Expr<'_>,
18     arg: &'tcx hir::Expr<'_>,
19     simplify_using: &str,
20 ) {
21     if is_from_proc_macro(cx, expr) {
22         return;
23     }
24
25     let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Option);
26     let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result);
27     let is_bool = cx.typeck_results().expr_ty(recv).is_bool();
28
29     if is_option || is_result || is_bool {
30         if let hir::ExprKind::Closure(&hir::Closure { body, .. }) = arg.kind {
31             let body = cx.tcx.hir().body(body);
32             let body_expr = &body.value;
33
34             if usage::BindingUsageFinder::are_params_used(cx, body) {
35                 return;
36             }
37
38             if eager_or_lazy::switch_to_eager_eval(cx, body_expr) {
39                 let msg = if is_option {
40                     "unnecessary closure used to substitute value for `Option::None`"
41                 } else if is_result {
42                     "unnecessary closure used to substitute value for `Result::Err`"
43                 } else {
44                     "unnecessary closure used with `bool::then`"
45                 };
46                 let applicability = if body
47                     .params
48                     .iter()
49                     // bindings are checked to be unused above
50                     .all(|param| matches!(param.pat.kind, hir::PatKind::Binding(..) | hir::PatKind::Wild))
51                 {
52                     Applicability::MachineApplicable
53                 } else {
54                     // replacing the lambda may break type inference
55                     Applicability::MaybeIncorrect
56                 };
57
58                 // This is a duplicate of what's happening in clippy_lints::methods::method_call,
59                 // which isn't ideal, We want to get the method call span,
60                 // but prefer to avoid changing the signature of the function itself.
61                 if let hir::ExprKind::MethodCall(.., span) = expr.kind {
62                     span_lint_and_then(cx, UNNECESSARY_LAZY_EVALUATIONS, expr.span, msg, |diag| {
63                         diag.span_suggestion(
64                             span,
65                             &format!("use `{simplify_using}(..)` instead"),
66                             format!("{simplify_using}({})", snippet(cx, body_expr.span, "..")),
67                             applicability,
68                         );
69                     });
70                 }
71             }
72         }
73     }
74 }