]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/loops/for_kv_map.rs
Rollup merge of #102187 - b-naber:inline-const-source-info, r=eholk
[rust.git] / src / tools / clippy / clippy_lints / src / loops / for_kv_map.rs
1 use super::FOR_KV_MAP;
2 use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
3 use clippy_utils::source::snippet;
4 use clippy_utils::sugg;
5 use clippy_utils::ty::is_type_diagnostic_item;
6 use clippy_utils::visitors::is_local_used;
7 use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Pat, PatKind};
8 use rustc_lint::LateContext;
9 use rustc_middle::ty;
10 use rustc_span::sym;
11
12 /// Checks for the `FOR_KV_MAP` lint.
13 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, arg: &'tcx Expr<'_>, body: &'tcx Expr<'_>) {
14     let pat_span = pat.span;
15
16     if let PatKind::Tuple(pat, _) = pat.kind {
17         if pat.len() == 2 {
18             let arg_span = arg.span;
19             let (new_pat_span, kind, ty, mutbl) = match *cx.typeck_results().expr_ty(arg).kind() {
20                 ty::Ref(_, ty, mutbl) => match (&pat[0].kind, &pat[1].kind) {
21                     (key, _) if pat_is_wild(cx, key, body) => (pat[1].span, "value", ty, mutbl),
22                     (_, value) if pat_is_wild(cx, value, body) => (pat[0].span, "key", ty, Mutability::Not),
23                     _ => return,
24                 },
25                 _ => return,
26             };
27             let mutbl = match mutbl {
28                 Mutability::Not => "",
29                 Mutability::Mut => "_mut",
30             };
31             let arg = match arg.kind {
32                 ExprKind::AddrOf(BorrowKind::Ref, _, expr) => expr,
33                 _ => arg,
34             };
35
36             if is_type_diagnostic_item(cx, ty, sym::HashMap) || is_type_diagnostic_item(cx, ty, sym::BTreeMap) {
37                 span_lint_and_then(
38                     cx,
39                     FOR_KV_MAP,
40                     arg_span,
41                     &format!("you seem to want to iterate on a map's {kind}s"),
42                     |diag| {
43                         let map = sugg::Sugg::hir(cx, arg, "map");
44                         multispan_sugg(
45                             diag,
46                             "use the corresponding method",
47                             vec![
48                                 (pat_span, snippet(cx, new_pat_span, kind).into_owned()),
49                                 (arg_span, format!("{}.{kind}s{mutbl}()", map.maybe_par())),
50                             ],
51                         );
52                     },
53                 );
54             }
55         }
56     }
57 }
58
59 /// Returns `true` if the pattern is a `PatWild` or an ident prefixed with `_`.
60 fn pat_is_wild<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx PatKind<'_>, body: &'tcx Expr<'_>) -> bool {
61     match *pat {
62         PatKind::Wild => true,
63         PatKind::Binding(_, id, ident, None) if ident.as_str().starts_with('_') => !is_local_used(cx, body, id),
64         _ => false,
65     }
66 }