2015-08-21 17:32:21 +00:00
|
|
|
use rustc::lint::*;
|
2015-09-03 14:42:17 +00:00
|
|
|
use rustc_front::hir::*;
|
2015-08-21 17:32:21 +00:00
|
|
|
|
2015-08-29 09:41:06 +00:00
|
|
|
use utils::{snippet, span_lint, span_help_and_lint, in_external_macro, expr_block};
|
2015-08-21 17:32:21 +00:00
|
|
|
|
|
|
|
declare_lint!(pub SINGLE_MATCH, Warn,
|
|
|
|
"a match statement with a single nontrivial arm (i.e, where the other arm \
|
|
|
|
is `_ => {}`) is used; recommends `if let` instead");
|
2015-08-21 17:49:00 +00:00
|
|
|
declare_lint!(pub MATCH_REF_PATS, Warn,
|
|
|
|
"a match has all arms prefixed with `&`; the match expression can be \
|
|
|
|
dereferenced instead");
|
2015-08-21 17:32:21 +00:00
|
|
|
|
|
|
|
#[allow(missing_copy_implementations)]
|
|
|
|
pub struct MatchPass;
|
|
|
|
|
|
|
|
impl LintPass for MatchPass {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
2015-08-21 17:49:00 +00:00
|
|
|
lint_array!(SINGLE_MATCH, MATCH_REF_PATS)
|
2015-08-21 17:32:21 +00:00
|
|
|
}
|
2015-09-19 02:53:04 +00:00
|
|
|
}
|
2015-08-21 17:32:21 +00:00
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
impl LateLintPass for MatchPass {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
|
2015-09-03 14:42:17 +00:00
|
|
|
if let ExprMatch(ref ex, ref arms, MatchSource::Normal) = expr.node {
|
2015-08-21 17:49:00 +00:00
|
|
|
// check preconditions for SINGLE_MATCH
|
|
|
|
// only two arms
|
2015-08-21 17:32:21 +00:00
|
|
|
if arms.len() == 2 &&
|
|
|
|
// both of the arms have a single pattern and no guard
|
|
|
|
arms[0].pats.len() == 1 && arms[0].guard.is_none() &&
|
|
|
|
arms[1].pats.len() == 1 && arms[1].guard.is_none() &&
|
|
|
|
// and the second pattern is a `_` wildcard: this is not strictly necessary,
|
|
|
|
// since the exhaustiveness check will ensure the last one is a catch-all,
|
|
|
|
// but in some cases, an explicit match is preferred to catch situations
|
|
|
|
// when an enum is extended, so we don't consider these cases
|
|
|
|
arms[1].pats[0].node == PatWild(PatWildSingle) &&
|
|
|
|
// finally, we don't want any content in the second arm (unit or empty block)
|
2015-08-25 12:41:35 +00:00
|
|
|
is_unit_expr(&arms[1].body)
|
2015-08-21 17:32:21 +00:00
|
|
|
{
|
2015-08-26 22:31:35 +00:00
|
|
|
if in_external_macro(cx, expr.span) {return;}
|
2015-08-21 17:32:21 +00:00
|
|
|
span_help_and_lint(cx, SINGLE_MATCH, expr.span,
|
2015-09-02 14:46:12 +00:00
|
|
|
"you seem to be trying to use match for destructuring a \
|
|
|
|
single pattern. Consider using `if let`",
|
|
|
|
&format!("try\nif let {} = {} {}",
|
|
|
|
snippet(cx, arms[0].pats[0].span, ".."),
|
|
|
|
snippet(cx, ex.span, ".."),
|
|
|
|
expr_block(cx, &arms[0].body, "..")));
|
2015-08-21 17:32:21 +00:00
|
|
|
}
|
2015-08-21 17:49:00 +00:00
|
|
|
|
|
|
|
// check preconditions for MATCH_REF_PATS
|
|
|
|
if has_only_ref_pats(arms) {
|
2015-09-17 05:24:11 +00:00
|
|
|
if in_external_macro(cx, expr.span) { return; }
|
2015-08-21 18:49:59 +00:00
|
|
|
if let ExprAddrOf(Mutability::MutImmutable, ref inner) = ex.node {
|
|
|
|
span_lint(cx, MATCH_REF_PATS, expr.span, &format!(
|
|
|
|
"you don't need to add `&` to both the expression to match \
|
|
|
|
and the patterns: use `match {} {{ ...`", snippet(cx, inner.span, "..")));
|
|
|
|
} else {
|
|
|
|
span_lint(cx, MATCH_REF_PATS, expr.span, &format!(
|
|
|
|
"instead of prefixing all patterns with `&`, you can dereference the \
|
|
|
|
expression to match: `match *{} {{ ...`", snippet(cx, ex.span, "..")));
|
|
|
|
}
|
2015-08-21 17:49:00 +00:00
|
|
|
}
|
2015-08-21 17:32:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_unit_expr(expr: &Expr) -> bool {
|
|
|
|
match expr.node {
|
|
|
|
ExprTup(ref v) if v.is_empty() => true,
|
|
|
|
ExprBlock(ref b) if b.stmts.is_empty() && b.expr.is_none() => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2015-08-21 17:49:00 +00:00
|
|
|
|
|
|
|
fn has_only_ref_pats(arms: &[Arm]) -> bool {
|
2015-09-17 05:24:11 +00:00
|
|
|
let mapped = arms.iter().flat_map(|a| &a.pats).map(|p| match p.node {
|
|
|
|
PatRegion(..) => Some(true), // &-patterns
|
|
|
|
PatWild(..) => Some(false), // an "anything" wildcard is also fine
|
|
|
|
_ => None, // any other pattern is not fine
|
|
|
|
}).collect::<Option<Vec<bool>>>();
|
|
|
|
// look for Some(v) where there's at least one true element
|
|
|
|
mapped.map_or(false, |v| v.iter().any(|el| *el))
|
2015-08-21 17:49:00 +00:00
|
|
|
}
|