use rustc::lint::*;
use rustc_front::hir::*;
+use rustc::middle::ty;
use utils::{snippet, span_lint, span_help_and_lint, in_external_macro, expr_block};
declare_lint!(pub MATCH_REF_PATS, Warn,
"a match has all arms prefixed with `&`; the match expression can be \
dereferenced instead");
+declare_lint!(pub MATCH_BOOL, Warn,
+ "a match on boolean expression; recommends `if..else` block instead");
#[allow(missing_copy_implementations)]
pub struct MatchPass;
impl LintPass for MatchPass {
fn get_lints(&self) -> LintArray {
- lint_array!(SINGLE_MATCH, MATCH_REF_PATS)
+ lint_array!(SINGLE_MATCH, MATCH_REF_PATS, MATCH_BOOL)
}
}
expression to match: `match *{} {{ ...`", snippet(cx, ex.span, "..")));
}
}
+
+ // check preconditions for MATCH_BOOL
+ // type of expression == bool
+ if is_bool_expr(cx, ex) {
+ if in_external_macro(cx, expr.span) { return; }
+
+ span_lint(cx, MATCH_BOOL, expr.span,
+ "you seem to be trying to match on a boolean expression. \
+ Consider using if..else block");
+ }
}
}
}
}
}
+fn is_bool_expr(cx: &LateContext, ex: &Expr ) -> bool {
+ cx.tcx.expr_ty(ex).sty == ty::TyBool
+}
+
fn has_only_ref_pats(arms: &[Arm]) -> bool {
let mapped = arms.iter().flat_map(|a| &a.pats).map(|p| match p.node {
PatRegion(..) => Some(true), // &-patterns