Skip to content

Commit 76877d3

Browse files
committed
Use span_suggestion in matches lints
Ref rust-lang#442
1 parent 9f641a1 commit 76877d3

File tree

1 file changed

+45
-55
lines changed

1 file changed

+45
-55
lines changed

src/matches.rs

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use syntax::ast::Lit_::LitBool;
99
use syntax::codemap::Span;
1010

1111
use utils::{COW_PATH, OPTION_PATH, RESULT_PATH};
12-
use utils::{match_type, snippet, span_lint, span_note_and_lint, span_help_and_lint, in_external_macro, expr_block};
12+
use utils::{match_type, snippet, span_lint, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block};
1313

1414
/// **What it does:** This lint checks for matches with a single arm where an `if let` will usually suffice. It is `Warn` by default.
1515
///
@@ -124,15 +124,17 @@ fn check_single_match(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
124124

125125
fn check_single_match_single_pattern(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
126126
if arms[1].pats[0].node == PatWild {
127-
span_help_and_lint(cx,
127+
span_lint_and_then(cx,
128128
SINGLE_MATCH,
129129
expr.span,
130-
"you seem to be trying to use match for destructuring a single pattern. Consider using \
131-
`if let`",
132-
&format!("try\nif let {} = {} {}",
133-
snippet(cx, arms[0].pats[0].span, ".."),
134-
snippet(cx, ex.span, ".."),
135-
expr_block(cx, &arms[0].body, None, "..")));
130+
"you seem to be trying to use match for destructuring a single pattern. \
131+
Consider using `if let`", |db| {
132+
db.span_suggestion(expr.span, "try this",
133+
format!("if let {} = {} {}",
134+
snippet(cx, arms[0].pats[0].span, ".."),
135+
snippet(cx, ex.span, ".."),
136+
expr_block(cx, &arms[0].body, None, "..")));
137+
});
136138
}
137139
}
138140

@@ -156,23 +158,25 @@ fn check_single_match_opt_like(cx: &LateContext, ex: &Expr, arms: &[Arm], expr:
156158

157159
for &(ty_path, pat_path) in candidates {
158160
if &path == pat_path && match_type(cx, ty, ty_path) {
159-
span_help_and_lint(cx,
161+
span_lint_and_then(cx,
160162
SINGLE_MATCH,
161163
expr.span,
162-
"you seem to be trying to use match for destructuring a single pattern. Consider using \
163-
`if let`",
164-
&format!("try\nif let {} = {} {}",
165-
snippet(cx, arms[0].pats[0].span, ".."),
166-
snippet(cx, ex.span, ".."),
167-
expr_block(cx, &arms[0].body, None, "..")));
164+
"you seem to be trying to use match for destructuring a single pattern. \
165+
Consider using `if let`", |db| {
166+
db.span_suggestion(expr.span, "try this",
167+
format!("if let {} = {} {}",
168+
snippet(cx, arms[0].pats[0].span, ".."),
169+
snippet(cx, ex.span, ".."),
170+
expr_block(cx, &arms[0].body, None, "..")));
171+
});
168172
}
169173
}
170174
}
171175

172176
fn check_match_bool(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
173177
// type of expression == bool
174178
if cx.tcx.expr_ty(ex).sty == ty::TyBool {
175-
if arms.len() == 2 && arms[0].pats.len() == 1 {
179+
let sugg = if arms.len() == 2 && arms[0].pats.len() == 1 {
176180
// no guards
177181
let exprs = if let PatLit(ref arm_bool) = arms[0].pats[0].node {
178182
if let ExprLit(ref lit) = arm_bool.node {
@@ -187,56 +191,42 @@ fn check_match_bool(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
187191
} else {
188192
None
189193
};
194+
190195
if let Some((ref true_expr, ref false_expr)) = exprs {
191196
if !is_unit_expr(true_expr) {
192197
if !is_unit_expr(false_expr) {
193-
span_help_and_lint(cx,
194-
MATCH_BOOL,
195-
expr.span,
196-
"you seem to be trying to match on a boolean expression. Consider using \
197-
an if..else block:",
198-
&format!("try\nif {} {} else {}",
199-
snippet(cx, ex.span, "b"),
200-
expr_block(cx, true_expr, None, ".."),
201-
expr_block(cx, false_expr, None, "..")));
198+
Some(format!("if {} {} else {}",
199+
snippet(cx, ex.span, "b"),
200+
expr_block(cx, true_expr, None, ".."),
201+
expr_block(cx, false_expr, None, "..")))
202202
} else {
203-
span_help_and_lint(cx,
204-
MATCH_BOOL,
205-
expr.span,
206-
"you seem to be trying to match on a boolean expression. Consider using \
207-
an if..else block:",
208-
&format!("try\nif {} {}",
209-
snippet(cx, ex.span, "b"),
210-
expr_block(cx, true_expr, None, "..")));
203+
Some(format!("if {} {}",
204+
snippet(cx, ex.span, "b"),
205+
expr_block(cx, true_expr, None, "..")))
211206
}
212207
} else if !is_unit_expr(false_expr) {
213-
span_help_and_lint(cx,
214-
MATCH_BOOL,
215-
expr.span,
216-
"you seem to be trying to match on a boolean expression. Consider using an \
217-
if..else block:",
218-
&format!("try\nif !{} {}",
219-
snippet(cx, ex.span, "b"),
220-
expr_block(cx, false_expr, None, "..")));
208+
Some(format!("try\nif !{} {}",
209+
snippet(cx, ex.span, "b"),
210+
expr_block(cx, false_expr, None, "..")))
221211
} else {
222-
span_lint(cx,
223-
MATCH_BOOL,
224-
expr.span,
225-
"you seem to be trying to match on a boolean expression. Consider using an if..else \
226-
block");
212+
None
227213
}
228214
} else {
229-
span_lint(cx,
230-
MATCH_BOOL,
231-
expr.span,
232-
"you seem to be trying to match on a boolean expression. Consider using an if..else block");
215+
None
233216
}
234217
} else {
235-
span_lint(cx,
236-
MATCH_BOOL,
237-
expr.span,
238-
"you seem to be trying to match on a boolean expression. Consider using an if..else block");
239-
}
218+
None
219+
};
220+
221+
span_lint_and_then(cx,
222+
MATCH_BOOL,
223+
expr.span,
224+
"you seem to be trying to match on a boolean expression. Consider using \
225+
an if..else block:", move |db| {
226+
if let Some(ref sugg) = sugg {
227+
db.span_suggestion(expr.span, "try this", sugg.clone());
228+
}
229+
});
240230
}
241231
}
242232

0 commit comments

Comments
 (0)