diff options
| author | Jason Newcomb <jsnewcomb@pm.me> | 2022-02-07 13:00:19 -0500 |
|---|---|---|
| committer | Jason Newcomb <jsnewcomb@pm.me> | 2022-02-07 13:00:19 -0500 |
| commit | c65894cf1a3bc6b111305071942f77e8654a7a6a (patch) | |
| tree | 235a41dc13fd77e140964f9ae6fae5dbade5e40d | |
| parent | aa3af30dee07a82cd3fcec5732dc575a4571d392 (diff) | |
Split out `wild_in_or_pats`
| -rw-r--r-- | clippy_lints/src/matches/mod.rs | 26 | ||||
| -rw-r--r-- | clippy_lints/src/matches/wild_in_or_pats.rs | 24 |
2 files changed, 28 insertions, 22 deletions
diff --git a/clippy_lints/src/matches/mod.rs b/clippy_lints/src/matches/mod.rs index 4429065966d..b5ee4561f06 100644 --- a/clippy_lints/src/matches/mod.rs +++ b/clippy_lints/src/matches/mod.rs @@ -1,6 +1,5 @@ -use clippy_utils::diagnostics::span_lint_and_help; -use clippy_utils::{is_wild, meets_msrv, msrvs}; -use rustc_hir::{Arm, Expr, ExprKind, Local, MatchSource, Pat, PatKind}; +use clippy_utils::{meets_msrv, msrvs}; +use rustc_hir::{Expr, ExprKind, Local, MatchSource, Pat}; use rustc_lint::{LateContext, LateLintPass}; use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; @@ -18,6 +17,7 @@ mod overlapping_arms; mod redundant_pattern_match; mod rest_pat_in_fully_bound_struct; mod single_match; +mod wild_in_or_pats; declare_clippy_lint! { /// ### What it does @@ -621,7 +621,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches { match_wild_err_arm::check(cx, ex, arms); match_wild_enum::check(cx, ex, arms); match_as_ref::check(cx, ex, arms, expr); - check_wild_in_or_pats(cx, arms); + wild_in_or_pats::check(cx, arms); if self.infallible_destructuring_match_linted { self.infallible_destructuring_match_linted = false; @@ -644,21 +644,3 @@ impl<'tcx> LateLintPass<'tcx> for Matches { extract_msrv_attr!(LateContext); } - -fn check_wild_in_or_pats(cx: &LateContext<'_>, arms: &[Arm<'_>]) { - for arm in arms { - if let PatKind::Or(fields) = arm.pat.kind { - // look for multiple fields in this arm that contains at least one Wild pattern - if fields.len() > 1 && fields.iter().any(is_wild) { - span_lint_and_help( - cx, - WILDCARD_IN_OR_PATTERNS, - arm.pat.span, - "wildcard pattern covers any other pattern as it will match anyway", - None, - "consider handling `_` separately", - ); - } - } - } -} diff --git a/clippy_lints/src/matches/wild_in_or_pats.rs b/clippy_lints/src/matches/wild_in_or_pats.rs new file mode 100644 index 00000000000..459513e65bf --- /dev/null +++ b/clippy_lints/src/matches/wild_in_or_pats.rs @@ -0,0 +1,24 @@ +use clippy_utils::diagnostics::span_lint_and_help; +use clippy_utils::is_wild; +use rustc_hir::{Arm, PatKind}; +use rustc_lint::LateContext; + +use super::WILDCARD_IN_OR_PATTERNS; + +pub(crate) fn check(cx: &LateContext<'_>, arms: &[Arm<'_>]) { + for arm in arms { + if let PatKind::Or(fields) = arm.pat.kind { + // look for multiple fields in this arm that contains at least one Wild pattern + if fields.len() > 1 && fields.iter().any(is_wild) { + span_lint_and_help( + cx, + WILDCARD_IN_OR_PATTERNS, + arm.pat.span, + "wildcard pattern covers any other pattern as it will match anyway", + None, + "consider handling `_` separately", + ); + } + } + } +} |
