Skip to content

Commit 16d2ba4

Browse files
committed
wildcard_match_arm: lint only enum matches.
1 parent f56e013 commit 16d2ba4

File tree

5 files changed

+32
-24
lines changed

5 files changed

+32
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ All notable changes to this project will be documented in this file.
10271027
[`while_let_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#while_let_loop
10281028
[`while_let_on_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#while_let_on_iterator
10291029
[`wildcard_dependencies`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_dependencies
1030-
[`wildcard_match_arm`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_match_arm
1030+
[`wildcard_enum_match_arm`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_enum_match_arm
10311031
[`write_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#write_literal
10321032
[`write_with_newline`]: https://rust-lang.github.io/rust-clippy/master/index.html#write_with_newline
10331033
[`writeln_empty_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#writeln_empty_string

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
495495
indexing_slicing::INDEXING_SLICING,
496496
inherent_impl::MULTIPLE_INHERENT_IMPL,
497497
literal_representation::DECIMAL_LITERAL_REPRESENTATION,
498-
matches::WILDCARD_MATCH_ARM,
498+
matches::WILDCARD_ENUM_MATCH_ARM,
499499
mem_forget::MEM_FORGET,
500500
methods::CLONE_ON_REF_PTR,
501501
methods::OPTION_UNWRAP_USED,

clippy_lints/src/matches.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ declare_clippy_lint! {
187187
"a match on an Option value instead of using `as_ref()` or `as_mut`"
188188
}
189189

190-
/// **What it does:** Checks for wildcard matches using `_`.
190+
/// **What it does:** Checks for wildcard enum matches using `_`.
191191
///
192-
/// **Why is this bad?** New variants added by library updates can be missed.
192+
/// **Why is this bad?** New enum variants added by library updates can be missed.
193193
///
194194
/// **Known problems:** None.
195195
///
@@ -201,9 +201,9 @@ declare_clippy_lint! {
201201
/// }
202202
/// ```
203203
declare_clippy_lint! {
204-
pub WILDCARD_MATCH_ARM,
204+
pub WILDCARD_ENUM_MATCH_ARM,
205205
restriction,
206-
"a wildcard match arm using `_`"
206+
"a wildcard enum match arm using `_`"
207207
}
208208

209209
#[allow(missing_copy_implementations)]
@@ -219,7 +219,7 @@ impl LintPass for MatchPass {
219219
MATCH_OVERLAPPING_ARM,
220220
MATCH_WILD_ERR_ARM,
221221
MATCH_AS_REF,
222-
WILDCARD_MATCH_ARM
222+
WILDCARD_ENUM_MATCH_ARM
223223
)
224224
}
225225

@@ -238,7 +238,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchPass {
238238
check_match_bool(cx, ex, arms, expr);
239239
check_overlapping_arms(cx, ex, arms);
240240
check_wild_err_arm(cx, ex, arms);
241-
check_wild_match(cx, arms);
241+
check_wild_enum_match(cx, ex, arms);
242242
check_match_as_ref(cx, ex, arms, expr);
243243
}
244244
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]) {
463463
}
464464
}
465465

466-
fn check_wild_match(cx: &LateContext<'_, '_>, arms: &[Arm]) {
467-
for arm in arms {
468-
if is_wild(&arm.pats[0]) {
469-
span_note_and_lint(
470-
cx,
471-
WILDCARD_MATCH_ARM,
472-
arm.pats[0].span,
473-
"wildcard match will miss any future added variants.",
474-
arm.pats[0].span,
475-
"to resolve, match each variant explicitly",
476-
);
466+
fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
467+
if cx.tables.expr_ty(ex).is_enum() {
468+
for arm in arms {
469+
if is_wild(&arm.pats[0]) {
470+
span_note_and_lint(
471+
cx,
472+
WILDCARD_ENUM_MATCH_ARM,
473+
arm.pats[0].span,
474+
"wildcard match will miss any future added variants.",
475+
arm.pats[0].span,
476+
"to resolve, match each variant explicitly",
477+
);
478+
}
477479
}
478480
}
479481
}

tests/ui/wildcard_match_arm.rs renamed to tests/ui/wildcard_enum_match_arm.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![deny(clippy::wildcard_match_arm)]
1+
#![deny(clippy::wildcard_enum_match_arm)]
22

33
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
44
enum Color {
@@ -33,4 +33,10 @@ fn main() {
3333
c if c.is_monochrome() => {},
3434
Color::Rgb(_, _, _) => {},
3535
};
36+
let x: u8 = unimplemented!();
37+
match x {
38+
0 => {},
39+
140 => {},
40+
_ => {},
41+
};
3642
}

tests/ui/wildcard_match_arm.stderr renamed to tests/ui/wildcard_enum_match_arm.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
error: wildcard match will miss any future added variants.
2-
--> $DIR/wildcard_match_arm.rs:26:9
2+
--> $DIR/wildcard_enum_match_arm.rs:26:9
33
|
44
LL | _ => eprintln!("Not red"),
55
| ^
66
|
77
note: lint level defined here
8-
--> $DIR/wildcard_match_arm.rs:1:9
8+
--> $DIR/wildcard_enum_match_arm.rs:1:9
99
|
10-
LL | #![deny(clippy::wildcard_match_arm)]
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
LL | #![deny(clippy::wildcard_enum_match_arm)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
= note: to resolve, match each variant explicitly
1313

1414
error: aborting due to previous error

0 commit comments

Comments
 (0)