Extend match_ref_pats to desugared matches

This commit is contained in:
Seo Sanghyeon 2015-11-25 02:47:17 +09:00
parent a3e8091e87
commit 746991572f
2 changed files with 47 additions and 18 deletions

View File

@ -25,9 +25,8 @@ impl LintPass for MatchPass {
impl LateLintPass for MatchPass {
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
if let ExprMatch(ref ex, ref arms, MatchSource::Normal) = expr.node {
if in_external_macro(cx, expr.span) { return; }
if let ExprMatch(ref ex, ref arms, MatchSource::Normal) = expr.node {
// check preconditions for SINGLE_MATCH
// only two arms
if arms.len() == 2 &&
@ -53,19 +52,6 @@ impl LateLintPass for MatchPass {
expr_block(cx, &arms[0].body, None, "..")));
}
// check preconditions for MATCH_REF_PATS
if has_only_ref_pats(arms) {
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, "..")));
}
}
// check preconditions for MATCH_BOOL
// type of expression == bool
if cx.tcx.expr_ty(ex).sty == ty::TyBool {
@ -123,6 +109,22 @@ impl LateLintPass for MatchPass {
}
}
}
if let ExprMatch(ref ex, ref arms, source) = expr.node {
// check preconditions for MATCH_REF_PATS
if has_only_ref_pats(arms) {
if let ExprAddrOf(Mutability::MutImmutable, ref inner) = ex.node {
let template = match_template(source, "", &snippet(cx, inner.span, ".."));
span_lint(cx, MATCH_REF_PATS, expr.span, &format!(
"you don't need to add `&` to both the expression \
and the patterns: use `{}`", template));
} else {
let template = match_template(source, "*", &snippet(cx, ex.span, ".."));
span_lint(cx, MATCH_REF_PATS, expr.span, &format!(
"instead of prefixing all patterns with `&`, you can dereference the \
expression: `{}`", template));
}
}
}
}
}
@ -143,3 +145,20 @@ fn has_only_ref_pats(arms: &[Arm]) -> bool {
// look for Some(v) where there's at least one true element
mapped.map_or(false, |v| v.iter().any(|el| *el))
}
fn match_template(source: MatchSource, op: &str, expr: &str) -> String {
match source {
MatchSource::Normal => {
format!("match {}{} {{ ...", op, expr)
}
MatchSource::IfLetDesugar { .. } => {
format!("if let ... = {}{} {{", op, expr)
}
MatchSource::WhileLetDesugar => {
format!("while let ... = {}{} {{", op, expr)
}
MatchSource::ForLoopDesugar => {
panic!("for loop desugared to match with &-patterns!")
}
}
}

View File

@ -78,7 +78,7 @@ fn match_bool() {
fn ref_pats() {
{
let v = &Some(0);
match v { //~ERROR instead of prefixing all patterns with `&`
match v { //~ERROR dereference the expression: `match *v { ...`
&Some(v) => println!("{:?}", v),
&None => println!("none"),
}
@ -88,13 +88,13 @@ fn ref_pats() {
}
}
let tup =& (1, 2);
match tup { //~ERROR instead of prefixing all patterns with `&`
match tup { //~ERROR dereference the expression: `match *tup { ...`
&(v, 1) => println!("{}", v),
_ => println!("none"),
}
// special case: using & both in expr and pats
let w = Some(0);
match &w { //~ERROR you don't need to add `&` to both
match &w { //~ERROR use `match w { ...`
&Some(v) => println!("{:?}", v),
&None => println!("none"),
}
@ -103,6 +103,16 @@ fn ref_pats() {
match w {
_ => println!("none"),
}
let a = &Some(0);
if let &None = a { //~ERROR dereference the expression: `if let ... = *a {`
println!("none");
}
let b = Some(0);
if let &None = &b { //~ERROR use `if let ... = b {`
println!("none");
}
}
fn main() {