From efaed8e0c0bc67d46a647a0ceb94b4b095ce04db Mon Sep 17 00:00:00 2001 From: Alex Hamilton Date: Tue, 29 Jan 2019 14:25:40 -0600 Subject: [PATCH] wildcard_match_arm: lint only enum matches. --- CHANGELOG.md | 2 +- clippy_lints/src/lib.rs | 2 +- clippy_lints/src/matches.rs | 36 ++++++++++--------- ...atch_arm.rs => wildcard_enum_match_arm.rs} | 8 ++++- ....stderr => wildcard_enum_match_arm.stderr} | 8 ++--- 5 files changed, 32 insertions(+), 24 deletions(-) rename tests/ui/{wildcard_match_arm.rs => wildcard_enum_match_arm.rs} (83%) rename tests/ui/{wildcard_match_arm.stderr => wildcard_enum_match_arm.stderr} (58%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ac88d09a..71066aadf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1028,7 +1028,7 @@ All notable changes to this project will be documented in this file. [`while_let_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#while_let_loop [`while_let_on_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#while_let_on_iterator [`wildcard_dependencies`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_dependencies -[`wildcard_match_arm`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_match_arm +[`wildcard_enum_match_arm`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_enum_match_arm [`write_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#write_literal [`write_with_newline`]: https://rust-lang.github.io/rust-clippy/master/index.html#write_with_newline [`writeln_empty_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#writeln_empty_string diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index f4ba38496..52cc2a88d 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -499,7 +499,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { indexing_slicing::INDEXING_SLICING, inherent_impl::MULTIPLE_INHERENT_IMPL, literal_representation::DECIMAL_LITERAL_REPRESENTATION, - matches::WILDCARD_MATCH_ARM, + matches::WILDCARD_ENUM_MATCH_ARM, mem_forget::MEM_FORGET, methods::CLONE_ON_REF_PTR, methods::OPTION_UNWRAP_USED, diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 0245b5a13..e0094b199 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -187,9 +187,9 @@ declare_clippy_lint! { "a match on an Option value instead of using `as_ref()` or `as_mut`" } -/// **What it does:** Checks for wildcard matches using `_`. +/// **What it does:** Checks for wildcard enum matches using `_`. /// -/// **Why is this bad?** New variants added by library updates can be missed. +/// **Why is this bad?** New enum variants added by library updates can be missed. /// /// **Known problems:** None. /// @@ -201,9 +201,9 @@ declare_clippy_lint! { /// } /// ``` declare_clippy_lint! { - pub WILDCARD_MATCH_ARM, + pub WILDCARD_ENUM_MATCH_ARM, restriction, - "a wildcard match arm using `_`" + "a wildcard enum match arm using `_`" } #[allow(missing_copy_implementations)] @@ -219,7 +219,7 @@ impl LintPass for MatchPass { MATCH_OVERLAPPING_ARM, MATCH_WILD_ERR_ARM, MATCH_AS_REF, - WILDCARD_MATCH_ARM + WILDCARD_ENUM_MATCH_ARM ) } @@ -238,7 +238,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchPass { check_match_bool(cx, ex, arms, expr); check_overlapping_arms(cx, ex, arms); check_wild_err_arm(cx, ex, arms); - check_wild_match(cx, arms); + check_wild_enum_match(cx, ex, arms); check_match_as_ref(cx, ex, arms, expr); } if let ExprKind::Match(ref ex, ref arms, _) = expr.node { @@ -463,17 +463,19 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) { } } -fn check_wild_match(cx: &LateContext<'_, '_>, arms: &[Arm]) { - for arm in arms { - if is_wild(&arm.pats[0]) { - span_note_and_lint( - cx, - WILDCARD_MATCH_ARM, - arm.pats[0].span, - "wildcard match will miss any future added variants.", - arm.pats[0].span, - "to resolve, match each variant explicitly", - ); +fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) { + if cx.tables.expr_ty(ex).is_enum() { + for arm in arms { + if is_wild(&arm.pats[0]) { + span_note_and_lint( + cx, + WILDCARD_ENUM_MATCH_ARM, + arm.pats[0].span, + "wildcard match will miss any future added variants.", + arm.pats[0].span, + "to resolve, match each variant explicitly", + ); + } } } } diff --git a/tests/ui/wildcard_match_arm.rs b/tests/ui/wildcard_enum_match_arm.rs similarity index 83% rename from tests/ui/wildcard_match_arm.rs rename to tests/ui/wildcard_enum_match_arm.rs index 5d3a5ff2a..58daabf42 100644 --- a/tests/ui/wildcard_match_arm.rs +++ b/tests/ui/wildcard_enum_match_arm.rs @@ -1,4 +1,4 @@ -#![deny(clippy::wildcard_match_arm)] +#![deny(clippy::wildcard_enum_match_arm)] #[derive(Clone, Copy, Debug, Eq, PartialEq)] enum Color { @@ -33,4 +33,10 @@ fn main() { c if c.is_monochrome() => {}, Color::Rgb(_, _, _) => {}, }; + let x: u8 = unimplemented!(); + match x { + 0 => {}, + 140 => {}, + _ => {}, + }; } diff --git a/tests/ui/wildcard_match_arm.stderr b/tests/ui/wildcard_enum_match_arm.stderr similarity index 58% rename from tests/ui/wildcard_match_arm.stderr rename to tests/ui/wildcard_enum_match_arm.stderr index b78a82f60..6319a3f3d 100644 --- a/tests/ui/wildcard_match_arm.stderr +++ b/tests/ui/wildcard_enum_match_arm.stderr @@ -1,14 +1,14 @@ error: wildcard match will miss any future added variants. - --> $DIR/wildcard_match_arm.rs:26:9 + --> $DIR/wildcard_enum_match_arm.rs:26:9 | LL | _ => eprintln!("Not red"), | ^ | note: lint level defined here - --> $DIR/wildcard_match_arm.rs:1:9 + --> $DIR/wildcard_enum_match_arm.rs:1:9 | -LL | #![deny(clippy::wildcard_match_arm)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | #![deny(clippy::wildcard_enum_match_arm)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: to resolve, match each variant explicitly error: aborting due to previous error