mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-22 04:34:51 +00:00
move unneeded_wildcard_pattern to its own module
This commit is contained in:
parent
52cfde0586
commit
c0a106e252
@ -1,6 +1,7 @@
|
||||
mod builtin_type_shadow;
|
||||
mod double_neg;
|
||||
mod redundant_pattern;
|
||||
mod unneeded_wildcard_pattern;
|
||||
|
||||
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
|
||||
use clippy_utils::source::snippet_opt;
|
||||
@ -336,7 +337,7 @@ impl EarlyLintPass for MiscEarlyLints {
|
||||
}
|
||||
|
||||
redundant_pattern::check(cx, pat);
|
||||
check_unneeded_wildcard_pattern(cx, pat);
|
||||
unneeded_wildcard_pattern::check(cx, pat);
|
||||
}
|
||||
|
||||
fn check_fn(&mut self, cx: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: Span, _: NodeId) {
|
||||
@ -478,48 +479,3 @@ impl MiscEarlyLints {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_unneeded_wildcard_pattern(cx: &EarlyContext<'_>, pat: &Pat) {
|
||||
if let PatKind::TupleStruct(_, ref patterns) | PatKind::Tuple(ref patterns) = pat.kind {
|
||||
fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) {
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
UNNEEDED_WILDCARD_PATTERN,
|
||||
span,
|
||||
if only_one {
|
||||
"this pattern is unneeded as the `..` pattern can match that element"
|
||||
} else {
|
||||
"these patterns are unneeded as the `..` pattern can match those elements"
|
||||
},
|
||||
if only_one { "remove it" } else { "remove them" },
|
||||
"".to_string(),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(rest_index) = patterns.iter().position(|pat| pat.is_rest()) {
|
||||
if let Some((left_index, left_pat)) = patterns[..rest_index]
|
||||
.iter()
|
||||
.rev()
|
||||
.take_while(|pat| matches!(pat.kind, PatKind::Wild))
|
||||
.enumerate()
|
||||
.last()
|
||||
{
|
||||
span_lint(cx, left_pat.span.until(patterns[rest_index].span), left_index == 0);
|
||||
}
|
||||
|
||||
if let Some((right_index, right_pat)) = patterns[rest_index + 1..]
|
||||
.iter()
|
||||
.take_while(|pat| matches!(pat.kind, PatKind::Wild))
|
||||
.enumerate()
|
||||
.last()
|
||||
{
|
||||
span_lint(
|
||||
cx,
|
||||
patterns[rest_index].span.shrink_to_hi().to(right_pat.span),
|
||||
right_index == 0,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
52
clippy_lints/src/misc_early/unneeded_wildcard_pattern.rs
Normal file
52
clippy_lints/src/misc_early/unneeded_wildcard_pattern.rs
Normal file
@ -0,0 +1,52 @@
|
||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use rustc_ast::ast::{Pat, PatKind};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_lint::EarlyContext;
|
||||
use rustc_span::source_map::Span;
|
||||
|
||||
use super::UNNEEDED_WILDCARD_PATTERN;
|
||||
|
||||
pub(super) fn check(cx: &EarlyContext<'_>, pat: &Pat) {
|
||||
if let PatKind::TupleStruct(_, ref patterns) | PatKind::Tuple(ref patterns) = pat.kind {
|
||||
if let Some(rest_index) = patterns.iter().position(|pat| pat.is_rest()) {
|
||||
if let Some((left_index, left_pat)) = patterns[..rest_index]
|
||||
.iter()
|
||||
.rev()
|
||||
.take_while(|pat| matches!(pat.kind, PatKind::Wild))
|
||||
.enumerate()
|
||||
.last()
|
||||
{
|
||||
span_lint(cx, left_pat.span.until(patterns[rest_index].span), left_index == 0);
|
||||
}
|
||||
|
||||
if let Some((right_index, right_pat)) = patterns[rest_index + 1..]
|
||||
.iter()
|
||||
.take_while(|pat| matches!(pat.kind, PatKind::Wild))
|
||||
.enumerate()
|
||||
.last()
|
||||
{
|
||||
span_lint(
|
||||
cx,
|
||||
patterns[rest_index].span.shrink_to_hi().to(right_pat.span),
|
||||
right_index == 0,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) {
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
UNNEEDED_WILDCARD_PATTERN,
|
||||
span,
|
||||
if only_one {
|
||||
"this pattern is unneeded as the `..` pattern can match that element"
|
||||
} else {
|
||||
"these patterns are unneeded as the `..` pattern can match those elements"
|
||||
},
|
||||
if only_one { "remove it" } else { "remove them" },
|
||||
"".to_string(),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue
Block a user