]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/unnecessary_lazy_eval.rs
Merge commit 'b20d4c155d2fe3a8391f86dcf9a8c49e17188703' into clippyup
[rust.git] / src / tools / clippy / clippy_lints / src / methods / unnecessary_lazy_eval.rs
1 use crate::utils::{eager_or_lazy, usage};
2 use crate::utils::{is_type_diagnostic_item, snippet, span_lint_and_sugg};
3 use rustc_errors::Applicability;
4 use rustc_hir as hir;
5 use rustc_lint::LateContext;
6 use rustc_span::sym;
7
8 use super::UNNECESSARY_LAZY_EVALUATIONS;
9
10 /// lint use of `<fn>_else(simple closure)` for `Option`s and `Result`s that can be
11 /// replaced with `<fn>(return value of simple closure)`
12 pub(super) fn lint<'tcx>(
13     cx: &LateContext<'tcx>,
14     expr: &'tcx hir::Expr<'_>,
15     args: &'tcx [hir::Expr<'_>],
16     simplify_using: &str,
17 ) {
18     let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&args[0]), sym::option_type);
19     let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&args[0]), sym::result_type);
20
21     if is_option || is_result {
22         if let hir::ExprKind::Closure(_, _, eid, _, _) = args[1].kind {
23             let body = cx.tcx.hir().body(eid);
24             let body_expr = &body.value;
25
26             if usage::BindingUsageFinder::are_params_used(cx, body) {
27                 return;
28             }
29
30             if eager_or_lazy::is_eagerness_candidate(cx, body_expr) {
31                 let msg = if is_option {
32                     "unnecessary closure used to substitute value for `Option::None`"
33                 } else {
34                     "unnecessary closure used to substitute value for `Result::Err`"
35                 };
36
37                 span_lint_and_sugg(
38                     cx,
39                     UNNECESSARY_LAZY_EVALUATIONS,
40                     expr.span,
41                     msg,
42                     &format!("Use `{}` instead", simplify_using),
43                     format!(
44                         "{0}.{1}({2})",
45                         snippet(cx, args[0].span, ".."),
46                         simplify_using,
47                         snippet(cx, body_expr.span, ".."),
48                     ),
49                     Applicability::MachineApplicable,
50                 );
51             }
52         }
53     }
54 }