wildcard_match_arm: lint only enum matches.

This commit is contained in:
Alex Hamilton 2019-01-29 14:25:40 -06:00
parent c676578097
commit efaed8e0c0
5 changed files with 32 additions and 24 deletions

View File

@ -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

View File

@ -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,

View File

@ -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,12 +463,13 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
}
}
fn check_wild_match(cx: &LateContext<'_, '_>, arms: &[Arm]) {
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_MATCH_ARM,
WILDCARD_ENUM_MATCH_ARM,
arm.pats[0].span,
"wildcard match will miss any future added variants.",
arm.pats[0].span,
@ -477,6 +478,7 @@ fn check_wild_match(cx: &LateContext<'_, '_>, arms: &[Arm]) {
}
}
}
}
// If the block contains only a `panic!` macro (as expression or statement)
fn is_panic_block(block: &Block) -> bool {

View File

@ -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 => {},
_ => {},
};
}

View File

@ -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