Skip to content

Commit a161d3f

Browse files
committed
deduplicate single_match_else reporting
1 parent 26270c7 commit a161d3f

File tree

1 file changed

+25
-40
lines changed

1 file changed

+25
-40
lines changed

clippy_lints/src/matches.rs

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -198,29 +198,33 @@ fn check_single_match(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
198198

199199
fn check_single_match_single_pattern(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr, els: Option<&Expr>) {
200200
if arms[1].pats[0].node == PatKind::Wild {
201-
let lint = if els.is_some() {
202-
SINGLE_MATCH_ELSE
203-
} else {
204-
SINGLE_MATCH
205-
};
206-
let els_str = els.map_or(String::new(), |els| format!(" else {}", expr_block(cx, els, None, "..")));
207-
span_lint_and_then(cx,
208-
lint,
209-
expr.span,
210-
"you seem to be trying to use match for destructuring a single pattern. \
211-
Consider using `if let`",
212-
|db| {
213-
db.span_suggestion(expr.span,
214-
"try this",
215-
format!("if let {} = {} {}{}",
216-
snippet(cx, arms[0].pats[0].span, ".."),
217-
snippet(cx, ex.span, ".."),
218-
expr_block(cx, &arms[0].body, None, ".."),
219-
els_str));
220-
});
201+
report_single_match_single_pattern(cx, ex, arms, expr, els);
221202
}
222203
}
223204

205+
fn report_single_match_single_pattern(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr, els: Option<&Expr>) {
206+
let lint = if els.is_some() {
207+
SINGLE_MATCH_ELSE
208+
} else {
209+
SINGLE_MATCH
210+
};
211+
let els_str = els.map_or(String::new(), |els| format!(" else {}", expr_block(cx, els, None, "..")));
212+
span_lint_and_then(cx,
213+
lint,
214+
expr.span,
215+
"you seem to be trying to use match for destructuring a single pattern. \
216+
Consider using `if let`",
217+
|db| {
218+
db.span_suggestion(expr.span,
219+
"try this",
220+
format!("if let {} = {} {}{}",
221+
snippet(cx, arms[0].pats[0].span, ".."),
222+
snippet(cx, ex.span, ".."),
223+
expr_block(cx, &arms[0].body, None, ".."),
224+
els_str));
225+
});
226+
}
227+
224228
fn check_single_match_opt_like(
225229
cx: &LateContext,
226230
ex: &Expr,
@@ -253,26 +257,7 @@ fn check_single_match_opt_like(
253257

254258
for &(ty_path, pat_path) in candidates {
255259
if &path == pat_path && match_type(cx, ty, ty_path) {
256-
let lint = if els.is_some() {
257-
SINGLE_MATCH_ELSE
258-
} else {
259-
SINGLE_MATCH
260-
};
261-
let els_str = els.map_or(String::new(), |els| format!(" else {}", expr_block(cx, els, None, "..")));
262-
span_lint_and_then(cx,
263-
lint,
264-
expr.span,
265-
"you seem to be trying to use match for destructuring a single pattern. Consider \
266-
using `if let`",
267-
|db| {
268-
db.span_suggestion(expr.span,
269-
"try this",
270-
format!("if let {} = {} {}{}",
271-
snippet(cx, arms[0].pats[0].span, ".."),
272-
snippet(cx, ex.span, ".."),
273-
expr_block(cx, &arms[0].body, None, ".."),
274-
els_str));
275-
});
260+
report_single_match_single_pattern(cx, ex, arms, expr, els);
276261
}
277262
}
278263
}

0 commit comments

Comments
 (0)