]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/unnecessary_fold.rs
Auto merge of #6957 - camsteffen:eq-ty-kind, r=flip1995
[rust.git] / clippy_lints / src / methods / unnecessary_fold.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::source::snippet_with_applicability;
3 use clippy_utils::{is_trait_method, path_to_local_id, remove_blocks, strip_pat_refs};
4 use if_chain::if_chain;
5 use rustc_ast::ast;
6 use rustc_errors::Applicability;
7 use rustc_hir as hir;
8 use rustc_hir::PatKind;
9 use rustc_lint::LateContext;
10 use rustc_span::{source_map::Span, sym};
11
12 use super::UNNECESSARY_FOLD;
13
14 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, fold_args: &[hir::Expr<'_>], fold_span: Span) {
15     fn check_fold_with_op(
16         cx: &LateContext<'_>,
17         expr: &hir::Expr<'_>,
18         fold_args: &[hir::Expr<'_>],
19         fold_span: Span,
20         op: hir::BinOpKind,
21         replacement_method_name: &str,
22         replacement_has_args: bool,
23     ) {
24         if_chain! {
25             // Extract the body of the closure passed to fold
26             if let hir::ExprKind::Closure(_, _, body_id, _, _) = fold_args[2].kind;
27             let closure_body = cx.tcx.hir().body(body_id);
28             let closure_expr = remove_blocks(&closure_body.value);
29
30             // Check if the closure body is of the form `acc <op> some_expr(x)`
31             if let hir::ExprKind::Binary(ref bin_op, ref left_expr, ref right_expr) = closure_expr.kind;
32             if bin_op.node == op;
33
34             // Extract the names of the two arguments to the closure
35             if let [param_a, param_b] = closure_body.params;
36             if let PatKind::Binding(_, first_arg_id, ..) = strip_pat_refs(&param_a.pat).kind;
37             if let PatKind::Binding(_, second_arg_id, second_arg_ident, _) = strip_pat_refs(&param_b.pat).kind;
38
39             if path_to_local_id(left_expr, first_arg_id);
40             if replacement_has_args || path_to_local_id(right_expr, second_arg_id);
41
42             then {
43                 let mut applicability = Applicability::MachineApplicable;
44                 let sugg = if replacement_has_args {
45                     format!(
46                         "{replacement}(|{s}| {r})",
47                         replacement = replacement_method_name,
48                         s = second_arg_ident,
49                         r = snippet_with_applicability(cx, right_expr.span, "EXPR", &mut applicability),
50                     )
51                 } else {
52                     format!(
53                         "{replacement}()",
54                         replacement = replacement_method_name,
55                     )
56                 };
57
58                 span_lint_and_sugg(
59                     cx,
60                     UNNECESSARY_FOLD,
61                     fold_span.with_hi(expr.span.hi()),
62                     // TODO #2371 don't suggest e.g., .any(|x| f(x)) if we can suggest .any(f)
63                     "this `.fold` can be written more succinctly using another method",
64                     "try",
65                     sugg,
66                     applicability,
67                 );
68             }
69         }
70     }
71
72     // Check that this is a call to Iterator::fold rather than just some function called fold
73     if !is_trait_method(cx, expr, sym::Iterator) {
74         return;
75     }
76
77     assert!(
78         fold_args.len() == 3,
79         "Expected fold_args to have three entries - the receiver, the initial value and the closure"
80     );
81
82     // Check if the first argument to .fold is a suitable literal
83     if let hir::ExprKind::Lit(ref lit) = fold_args[1].kind {
84         match lit.node {
85             ast::LitKind::Bool(false) => {
86                 check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::Or, "any", true)
87             },
88             ast::LitKind::Bool(true) => {
89                 check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::And, "all", true)
90             },
91             ast::LitKind::Int(0, _) => {
92                 check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::Add, "sum", false)
93             },
94             ast::LitKind::Int(1, _) => {
95                 check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::Mul, "product", false)
96             },
97             _ => (),
98         }
99     }
100 }