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

Commit c0a106e

Browse files
committed
move unneeded_wildcard_pattern to its own module
1 parent 52cfde0 commit c0a106e

File tree

2 files changed

+54
-46
lines changed

2 files changed

+54
-46
lines changed

clippy_lints/src/misc_early/mod.rs

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod builtin_type_shadow;
22
mod double_neg;
33
mod redundant_pattern;
4+
mod unneeded_wildcard_pattern;
45

56
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
67
use clippy_utils::source::snippet_opt;
@@ -336,7 +337,7 @@ impl EarlyLintPass for MiscEarlyLints {
336337
}
337338

338339
redundant_pattern::check(cx, pat);
339-
check_unneeded_wildcard_pattern(cx, pat);
340+
unneeded_wildcard_pattern::check(cx, pat);
340341
}
341342

342343
fn check_fn(&mut self, cx: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: Span, _: NodeId) {
@@ -478,48 +479,3 @@ impl MiscEarlyLints {
478479
}
479480
}
480481
}
481-
482-
fn check_unneeded_wildcard_pattern(cx: &EarlyContext<'_>, pat: &Pat) {
483-
if let PatKind::TupleStruct(_, ref patterns) | PatKind::Tuple(ref patterns) = pat.kind {
484-
fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) {
485-
span_lint_and_sugg(
486-
cx,
487-
UNNEEDED_WILDCARD_PATTERN,
488-
span,
489-
if only_one {
490-
"this pattern is unneeded as the `..` pattern can match that element"
491-
} else {
492-
"these patterns are unneeded as the `..` pattern can match those elements"
493-
},
494-
if only_one { "remove it" } else { "remove them" },
495-
"".to_string(),
496-
Applicability::MachineApplicable,
497-
);
498-
}
499-
500-
if let Some(rest_index) = patterns.iter().position(|pat| pat.is_rest()) {
501-
if let Some((left_index, left_pat)) = patterns[..rest_index]
502-
.iter()
503-
.rev()
504-
.take_while(|pat| matches!(pat.kind, PatKind::Wild))
505-
.enumerate()
506-
.last()
507-
{
508-
span_lint(cx, left_pat.span.until(patterns[rest_index].span), left_index == 0);
509-
}
510-
511-
if let Some((right_index, right_pat)) = patterns[rest_index + 1..]
512-
.iter()
513-
.take_while(|pat| matches!(pat.kind, PatKind::Wild))
514-
.enumerate()
515-
.last()
516-
{
517-
span_lint(
518-
cx,
519-
patterns[rest_index].span.shrink_to_hi().to(right_pat.span),
520-
right_index == 0,
521-
);
522-
}
523-
}
524-
}
525-
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use rustc_ast::ast::{Pat, PatKind};
3+
use rustc_errors::Applicability;
4+
use rustc_lint::EarlyContext;
5+
use rustc_span::source_map::Span;
6+
7+
use super::UNNEEDED_WILDCARD_PATTERN;
8+
9+
pub(super) fn check(cx: &EarlyContext<'_>, pat: &Pat) {
10+
if let PatKind::TupleStruct(_, ref patterns) | PatKind::Tuple(ref patterns) = pat.kind {
11+
if let Some(rest_index) = patterns.iter().position(|pat| pat.is_rest()) {
12+
if let Some((left_index, left_pat)) = patterns[..rest_index]
13+
.iter()
14+
.rev()
15+
.take_while(|pat| matches!(pat.kind, PatKind::Wild))
16+
.enumerate()
17+
.last()
18+
{
19+
span_lint(cx, left_pat.span.until(patterns[rest_index].span), left_index == 0);
20+
}
21+
22+
if let Some((right_index, right_pat)) = patterns[rest_index + 1..]
23+
.iter()
24+
.take_while(|pat| matches!(pat.kind, PatKind::Wild))
25+
.enumerate()
26+
.last()
27+
{
28+
span_lint(
29+
cx,
30+
patterns[rest_index].span.shrink_to_hi().to(right_pat.span),
31+
right_index == 0,
32+
);
33+
}
34+
}
35+
}
36+
}
37+
38+
fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) {
39+
span_lint_and_sugg(
40+
cx,
41+
UNNEEDED_WILDCARD_PATTERN,
42+
span,
43+
if only_one {
44+
"this pattern is unneeded as the `..` pattern can match that element"
45+
} else {
46+
"these patterns are unneeded as the `..` pattern can match those elements"
47+
},
48+
if only_one { "remove it" } else { "remove them" },
49+
"".to_string(),
50+
Applicability::MachineApplicable,
51+
);
52+
}

0 commit comments

Comments
 (0)