Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 829fde5

Browse files
committed
move unneeded_field_pattern to its own module
1 parent c0a106e commit 829fde5

File tree

2 files changed

+75
-66
lines changed

2 files changed

+75
-66
lines changed

clippy_lints/src/misc_early/mod.rs

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
mod builtin_type_shadow;
22
mod double_neg;
33
mod redundant_pattern;
4+
mod unneeded_field_pattern;
45
mod unneeded_wildcard_pattern;
56

6-
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
7+
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
78
use clippy_utils::source::snippet_opt;
89
use rustc_ast::ast::{Expr, Generics, Lit, LitFloatType, LitIntType, LitKind, NodeId, Pat, PatKind};
910
use rustc_ast::visit::FnKind;
@@ -271,71 +272,7 @@ impl EarlyLintPass for MiscEarlyLints {
271272
}
272273

273274
fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) {
274-
if let PatKind::Struct(ref npat, ref pfields, _) = pat.kind {
275-
let mut wilds = 0;
276-
let type_name = npat
277-
.segments
278-
.last()
279-
.expect("A path must have at least one segment")
280-
.ident
281-
.name;
282-
283-
for field in pfields {
284-
if let PatKind::Wild = field.pat.kind {
285-
wilds += 1;
286-
}
287-
}
288-
if !pfields.is_empty() && wilds == pfields.len() {
289-
span_lint_and_help(
290-
cx,
291-
UNNEEDED_FIELD_PATTERN,
292-
pat.span,
293-
"all the struct fields are matched to a wildcard pattern, consider using `..`",
294-
None,
295-
&format!("try with `{} {{ .. }}` instead", type_name),
296-
);
297-
return;
298-
}
299-
if wilds > 0 {
300-
for field in pfields {
301-
if let PatKind::Wild = field.pat.kind {
302-
wilds -= 1;
303-
if wilds > 0 {
304-
span_lint(
305-
cx,
306-
UNNEEDED_FIELD_PATTERN,
307-
field.span,
308-
"you matched a field with a wildcard pattern, consider using `..` instead",
309-
);
310-
} else {
311-
let mut normal = vec![];
312-
313-
for field in pfields {
314-
match field.pat.kind {
315-
PatKind::Wild => {},
316-
_ => {
317-
if let Ok(n) = cx.sess().source_map().span_to_snippet(field.span) {
318-
normal.push(n);
319-
}
320-
},
321-
}
322-
}
323-
324-
span_lint_and_help(
325-
cx,
326-
UNNEEDED_FIELD_PATTERN,
327-
field.span,
328-
"you matched a field with a wildcard pattern, consider using `..` \
329-
instead",
330-
None,
331-
&format!("try with `{} {{ {}, .. }}`", type_name, normal[..].join(", ")),
332-
);
333-
}
334-
}
335-
}
336-
}
337-
}
338-
275+
unneeded_field_pattern::check(cx, pat);
339276
redundant_pattern::check(cx, pat);
340277
unneeded_wildcard_pattern::check(cx, pat);
341278
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
2+
use rustc_ast::ast::{Pat, PatKind};
3+
use rustc_lint::{EarlyContext, LintContext};
4+
5+
use super::UNNEEDED_FIELD_PATTERN;
6+
7+
pub(super) fn check(cx: &EarlyContext<'_>, pat: &Pat) {
8+
if let PatKind::Struct(ref npat, ref pfields, _) = pat.kind {
9+
let mut wilds = 0;
10+
let type_name = npat
11+
.segments
12+
.last()
13+
.expect("A path must have at least one segment")
14+
.ident
15+
.name;
16+
17+
for field in pfields {
18+
if let PatKind::Wild = field.pat.kind {
19+
wilds += 1;
20+
}
21+
}
22+
if !pfields.is_empty() && wilds == pfields.len() {
23+
span_lint_and_help(
24+
cx,
25+
UNNEEDED_FIELD_PATTERN,
26+
pat.span,
27+
"all the struct fields are matched to a wildcard pattern, consider using `..`",
28+
None,
29+
&format!("try with `{} {{ .. }}` instead", type_name),
30+
);
31+
return;
32+
}
33+
if wilds > 0 {
34+
for field in pfields {
35+
if let PatKind::Wild = field.pat.kind {
36+
wilds -= 1;
37+
if wilds > 0 {
38+
span_lint(
39+
cx,
40+
UNNEEDED_FIELD_PATTERN,
41+
field.span,
42+
"you matched a field with a wildcard pattern, consider using `..` instead",
43+
);
44+
} else {
45+
let mut normal = vec![];
46+
47+
for field in pfields {
48+
match field.pat.kind {
49+
PatKind::Wild => {},
50+
_ => {
51+
if let Ok(n) = cx.sess().source_map().span_to_snippet(field.span) {
52+
normal.push(n);
53+
}
54+
},
55+
}
56+
}
57+
58+
span_lint_and_help(
59+
cx,
60+
UNNEEDED_FIELD_PATTERN,
61+
field.span,
62+
"you matched a field with a wildcard pattern, consider using `..` \
63+
instead",
64+
None,
65+
&format!("try with `{} {{ {}, .. }}`", type_name, normal[..].join(", ")),
66+
);
67+
}
68+
}
69+
}
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)