Skip to content

Commit

Permalink
wildcard_match_arm: lint only enum matches.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aehmlo committed Jan 29, 2019
1 parent c676578 commit efaed8e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Expand Up @@ -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,
Expand Down
36 changes: 19 additions & 17 deletions clippy_lints/src/matches.rs
Expand Up @@ -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.
///
Expand All @@ -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)]
Expand All @@ -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
)
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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",
);
}
}
}
}
Expand Down
@@ -1,4 +1,4 @@
#![deny(clippy::wildcard_match_arm)]
#![deny(clippy::wildcard_enum_match_arm)]

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Color {
Expand Down Expand Up @@ -33,4 +33,10 @@ fn main() {
c if c.is_monochrome() => {},
Color::Rgb(_, _, _) => {},
};
let x: u8 = unimplemented!();
match x {
0 => {},
140 => {},
_ => {},
};
}
@@ -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
Expand Down

0 comments on commit efaed8e

Please sign in to comment.