]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/misc_early/unneeded_field_pattern.rs
Rollup merge of #94145 - ssomers:binary_heap_tests, r=jyn514
[rust.git] / src / tools / clippy / clippy_lints / src / misc_early / unneeded_field_pattern.rs
1 use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
2 use clippy_utils::source::snippet_opt;
3 use rustc_ast::ast::{Pat, PatKind};
4 use rustc_lint::EarlyContext;
5
6 use super::UNNEEDED_FIELD_PATTERN;
7
8 pub(super) fn check(cx: &EarlyContext<'_>, pat: &Pat) {
9     if let PatKind::Struct(_, ref npat, ref pfields, _) = pat.kind {
10         let mut wilds = 0;
11         let type_name = npat
12             .segments
13             .last()
14             .expect("A path must have at least one segment")
15             .ident
16             .name;
17
18         for field in pfields {
19             if let PatKind::Wild = field.pat.kind {
20                 wilds += 1;
21             }
22         }
23         if !pfields.is_empty() && wilds == pfields.len() {
24             span_lint_and_help(
25                 cx,
26                 UNNEEDED_FIELD_PATTERN,
27                 pat.span,
28                 "all the struct fields are matched to a wildcard pattern, consider using `..`",
29                 None,
30                 &format!("try with `{type_name} {{ .. }}` instead"),
31             );
32             return;
33         }
34         if wilds > 0 {
35             for field in pfields {
36                 if let PatKind::Wild = field.pat.kind {
37                     wilds -= 1;
38                     if wilds > 0 {
39                         span_lint(
40                             cx,
41                             UNNEEDED_FIELD_PATTERN,
42                             field.span,
43                             "you matched a field with a wildcard pattern, consider using `..` instead",
44                         );
45                     } else {
46                         let mut normal = vec![];
47
48                         for field in pfields {
49                             match field.pat.kind {
50                                 PatKind::Wild => {},
51                                 _ => {
52                                     if let Some(n) = snippet_opt(cx, field.span) {
53                                         normal.push(n);
54                                     }
55                                 },
56                             }
57                         }
58
59                         span_lint_and_help(
60                             cx,
61                             UNNEEDED_FIELD_PATTERN,
62                             field.span,
63                             "you matched a field with a wildcard pattern, consider using `..` \
64                              instead",
65                             None,
66                             &format!("try with `{type_name} {{ {}, .. }}`", normal[..].join(", ")),
67                         );
68                     }
69                 }
70             }
71         }
72     }
73 }