Use span_suggestion in matches lints

Ref #442
This commit is contained in:
mcarton 2016-01-13 12:57:44 +01:00
parent 9f641a1009
commit 44daa8bd72

View File

@ -9,7 +9,7 @@ use syntax::ast::Lit_::LitBool;
use syntax::codemap::Span; use syntax::codemap::Span;
use utils::{COW_PATH, OPTION_PATH, RESULT_PATH}; use utils::{COW_PATH, OPTION_PATH, RESULT_PATH};
use utils::{match_type, snippet, span_lint, span_note_and_lint, span_help_and_lint, in_external_macro, expr_block}; use utils::{match_type, snippet, span_lint, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block};
/// **What it does:** This lint checks for matches with a single arm where an `if let` will usually suffice. It is `Warn` by default. /// **What it does:** This lint checks for matches with a single arm where an `if let` will usually suffice. It is `Warn` by default.
/// ///
@ -124,15 +124,17 @@ fn check_single_match(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
fn check_single_match_single_pattern(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) { fn check_single_match_single_pattern(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
if arms[1].pats[0].node == PatWild { if arms[1].pats[0].node == PatWild {
span_help_and_lint(cx, span_lint_and_then(cx,
SINGLE_MATCH, SINGLE_MATCH,
expr.span, expr.span,
"you seem to be trying to use match for destructuring a single pattern. Consider using \ "you seem to be trying to use match for destructuring a single pattern. \
`if let`", Consider using `if let`", |db| {
&format!("try\nif let {} = {} {}", db.span_suggestion(expr.span, "try this",
snippet(cx, arms[0].pats[0].span, ".."), format!("if let {} = {} {}",
snippet(cx, ex.span, ".."), snippet(cx, arms[0].pats[0].span, ".."),
expr_block(cx, &arms[0].body, None, ".."))); snippet(cx, ex.span, ".."),
expr_block(cx, &arms[0].body, None, "..")));
});
} }
} }
@ -156,15 +158,17 @@ fn check_single_match_opt_like(cx: &LateContext, ex: &Expr, arms: &[Arm], expr:
for &(ty_path, pat_path) in candidates { for &(ty_path, pat_path) in candidates {
if &path == pat_path && match_type(cx, ty, ty_path) { if &path == pat_path && match_type(cx, ty, ty_path) {
span_help_and_lint(cx, span_lint_and_then(cx,
SINGLE_MATCH, SINGLE_MATCH,
expr.span, expr.span,
"you seem to be trying to use match for destructuring a single pattern. Consider using \ "you seem to be trying to use match for destructuring a single pattern. \
`if let`", Consider using `if let`", |db| {
&format!("try\nif let {} = {} {}", db.span_suggestion(expr.span, "try this",
snippet(cx, arms[0].pats[0].span, ".."), format!("if let {} = {} {}",
snippet(cx, ex.span, ".."), snippet(cx, arms[0].pats[0].span, ".."),
expr_block(cx, &arms[0].body, None, ".."))); snippet(cx, ex.span, ".."),
expr_block(cx, &arms[0].body, None, "..")));
});
} }
} }
} }
@ -172,7 +176,7 @@ fn check_single_match_opt_like(cx: &LateContext, ex: &Expr, arms: &[Arm], expr:
fn check_match_bool(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) { fn check_match_bool(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
// type of expression == bool // type of expression == bool
if cx.tcx.expr_ty(ex).sty == ty::TyBool { if cx.tcx.expr_ty(ex).sty == ty::TyBool {
if arms.len() == 2 && arms[0].pats.len() == 1 { let sugg = if arms.len() == 2 && arms[0].pats.len() == 1 {
// no guards // no guards
let exprs = if let PatLit(ref arm_bool) = arms[0].pats[0].node { let exprs = if let PatLit(ref arm_bool) = arms[0].pats[0].node {
if let ExprLit(ref lit) = arm_bool.node { if let ExprLit(ref lit) = arm_bool.node {
@ -187,56 +191,42 @@ fn check_match_bool(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
} else { } else {
None None
}; };
if let Some((ref true_expr, ref false_expr)) = exprs { if let Some((ref true_expr, ref false_expr)) = exprs {
if !is_unit_expr(true_expr) { if !is_unit_expr(true_expr) {
if !is_unit_expr(false_expr) { if !is_unit_expr(false_expr) {
span_help_and_lint(cx, Some(format!("if {} {} else {}",
MATCH_BOOL, snippet(cx, ex.span, "b"),
expr.span, expr_block(cx, true_expr, None, ".."),
"you seem to be trying to match on a boolean expression. Consider using \ expr_block(cx, false_expr, None, "..")))
an if..else block:",
&format!("try\nif {} {} else {}",
snippet(cx, ex.span, "b"),
expr_block(cx, true_expr, None, ".."),
expr_block(cx, false_expr, None, "..")));
} else { } else {
span_help_and_lint(cx, Some(format!("if {} {}",
MATCH_BOOL, snippet(cx, ex.span, "b"),
expr.span, expr_block(cx, true_expr, None, "..")))
"you seem to be trying to match on a boolean expression. Consider using \
an if..else block:",
&format!("try\nif {} {}",
snippet(cx, ex.span, "b"),
expr_block(cx, true_expr, None, "..")));
} }
} else if !is_unit_expr(false_expr) { } else if !is_unit_expr(false_expr) {
span_help_and_lint(cx, Some(format!("try\nif !{} {}",
MATCH_BOOL, snippet(cx, ex.span, "b"),
expr.span, expr_block(cx, false_expr, None, "..")))
"you seem to be trying to match on a boolean expression. Consider using an \
if..else block:",
&format!("try\nif !{} {}",
snippet(cx, ex.span, "b"),
expr_block(cx, false_expr, None, "..")));
} else { } else {
span_lint(cx, None
MATCH_BOOL,
expr.span,
"you seem to be trying to match on a boolean expression. Consider using an if..else \
block");
} }
} else { } else {
span_lint(cx, None
MATCH_BOOL,
expr.span,
"you seem to be trying to match on a boolean expression. Consider using an if..else block");
} }
} else { } else {
span_lint(cx, None
MATCH_BOOL, };
expr.span,
"you seem to be trying to match on a boolean expression. Consider using an if..else block"); span_lint_and_then(cx,
} MATCH_BOOL,
expr.span,
"you seem to be trying to match on a boolean expression. Consider using \
an if..else block:", move |db| {
if let Some(ref sugg) = sugg {
db.span_suggestion(expr.span, "try this", sugg.clone());
}
});
} }
} }