1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use rustc::lint::*;
use rustc::hir::*;
use syntax::codemap::Span;
use utils::{paths, span_lint_and_then, match_path, snippet};
declare_lint! {
pub IF_LET_REDUNDANT_PATTERN_MATCHING,
Warn,
"use the proper utility function avoiding an `if let`"
}
#[derive(Copy, Clone)]
pub struct Pass;
impl LintPass for Pass {
fn get_lints(&self) -> LintArray {
lint_array!(IF_LET_REDUNDANT_PATTERN_MATCHING)
}
}
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if let ExprMatch(ref op, ref arms, MatchSource::IfLetDesugar { .. }) = expr.node {
if arms[0].pats.len() == 1 {
let good_method = match arms[0].pats[0].node {
PatKind::TupleStruct(ref path, ref pats, _) if pats.len() == 1 && pats[0].node == PatKind::Wild => {
if match_path(path, &paths::RESULT_OK) {
"is_ok()"
} else if match_path(path, &paths::RESULT_ERR) {
"is_err()"
} else if match_path(path, &paths::OPTION_SOME) {
"is_some()"
} else {
return;
}
},
PatKind::Path(ref path) if match_path(path, &paths::OPTION_NONE) => "is_none()",
_ => return,
};
span_lint_and_then(cx,
IF_LET_REDUNDANT_PATTERN_MATCHING,
arms[0].pats[0].span,
&format!("redundant pattern matching, consider using `{}`", good_method),
|db| {
let span = Span {
lo: expr.span.lo,
hi: op.span.hi,
expn_id: expr.span.expn_id,
};
db.span_suggestion(span, "try this", format!("if {}.{}", snippet(cx, op.span, "_"), good_method));
});
}
}
}
}