"a match on an Option value instead of using `as_ref()` or `as_mut`"
}
+/// **What it does:** Checks for wildcard matches using `_`.
+///
+/// **Why is this bad?** New variants added by library updates can be missed.
+///
+/// **Known problems:** None.
+///
+/// **Example:**
+/// ```rust
+/// match x {
+/// A => {},
+/// _ => {}
+/// }
+/// ```
+declare_clippy_lint! {
+ pub MATCH_WILD,
+ restriction,
+ "a wildcard match arm using `_`"
+}
+
#[allow(missing_copy_implementations)]
pub struct MatchPass;
SINGLE_MATCH_ELSE,
MATCH_OVERLAPPING_ARM,
MATCH_WILD_ERR_ARM,
- MATCH_AS_REF
+ MATCH_AS_REF,
+ MATCH_WILD
)
}
check_match_bool(cx, ex, arms, expr);
check_overlapping_arms(cx, ex, arms);
check_wild_err_arm(cx, ex, arms);
+ check_wild_arm(cx, ex, arms);
check_match_as_ref(cx, ex, arms, expr);
}
if let ExprKind::Match(ref ex, ref arms, _) = expr.node {
}
}
+fn check_wild_arm(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
+ let ex_ty = walk_ptrs_ty(cx.tables.expr_ty(ex));
+ if match_type(cx, ex_ty, &paths::RESULT) {
+ for arm in arms {
+ if is_wild(&arm.pats[0]) {
+ span_note_and_lint(cx,
+ MATCH_WILD,
+ arm.pats[0].span,
+ "Wildcard match will miss any future added variants.",
+ arm.pats[0].span,
+ "to resolve, match each variant explicitly");
+ }
+ }
+ }
+}
+
// If the block contains only a `panic!` macro (as expression or statement)
fn is_panic_block(block: &Block) -> bool {
match (&block.expr, block.stmts.len(), block.stmts.first()) {