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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#![deny(missing_docs_in_private_items)]
use rustc::hir;
use rustc::lint::LateContext;
use syntax::ast;
use utils::{is_expn_of, match_path, match_def_path, resolve_node, paths};
pub fn binop(op: hir::BinOp_) -> ast::BinOpKind {
match op {
hir::BiEq => ast::BinOpKind::Eq,
hir::BiGe => ast::BinOpKind::Ge,
hir::BiGt => ast::BinOpKind::Gt,
hir::BiLe => ast::BinOpKind::Le,
hir::BiLt => ast::BinOpKind::Lt,
hir::BiNe => ast::BinOpKind::Ne,
hir::BiOr => ast::BinOpKind::Or,
hir::BiAdd => ast::BinOpKind::Add,
hir::BiAnd => ast::BinOpKind::And,
hir::BiBitAnd => ast::BinOpKind::BitAnd,
hir::BiBitOr => ast::BinOpKind::BitOr,
hir::BiBitXor => ast::BinOpKind::BitXor,
hir::BiDiv => ast::BinOpKind::Div,
hir::BiMul => ast::BinOpKind::Mul,
hir::BiRem => ast::BinOpKind::Rem,
hir::BiShl => ast::BinOpKind::Shl,
hir::BiShr => ast::BinOpKind::Shr,
hir::BiSub => ast::BinOpKind::Sub,
}
}
#[derive(Debug, Copy, Clone)]
pub struct Range<'a> {
pub start: Option<&'a hir::Expr>,
pub end: Option<&'a hir::Expr>,
pub limits: ast::RangeLimits,
}
pub fn range(expr: &hir::Expr) -> Option<Range> {
fn get_field<'a>(name: &str, fields: &'a [hir::Field]) -> Option<&'a hir::Expr> {
let expr = &fields.iter()
.find(|field| field.name.node == name)
.unwrap_or_else(|| panic!("missing {} field for range", name))
.expr;
Some(expr)
}
match expr.node {
hir::ExprPath(ref path) => {
if match_path(path, &paths::RANGE_FULL_STD) || match_path(path, &paths::RANGE_FULL) {
Some(Range {
start: None,
end: None,
limits: ast::RangeLimits::HalfOpen,
})
} else {
None
}
},
hir::ExprStruct(ref path, ref fields, None) => {
if match_path(path, &paths::RANGE_FROM_STD) || match_path(path, &paths::RANGE_FROM) {
Some(Range {
start: get_field("start", fields),
end: None,
limits: ast::RangeLimits::HalfOpen,
})
} else if match_path(path, &paths::RANGE_INCLUSIVE_NON_EMPTY_STD) ||
match_path(path, &paths::RANGE_INCLUSIVE_NON_EMPTY) {
Some(Range {
start: get_field("start", fields),
end: get_field("end", fields),
limits: ast::RangeLimits::Closed,
})
} else if match_path(path, &paths::RANGE_STD) || match_path(path, &paths::RANGE) {
Some(Range {
start: get_field("start", fields),
end: get_field("end", fields),
limits: ast::RangeLimits::HalfOpen,
})
} else if match_path(path, &paths::RANGE_TO_INCLUSIVE_STD) || match_path(path, &paths::RANGE_TO_INCLUSIVE) {
Some(Range {
start: None,
end: get_field("end", fields),
limits: ast::RangeLimits::Closed,
})
} else if match_path(path, &paths::RANGE_TO_STD) || match_path(path, &paths::RANGE_TO) {
Some(Range {
start: None,
end: get_field("end", fields),
limits: ast::RangeLimits::HalfOpen,
})
} else {
None
}
},
_ => None,
}
}
pub fn is_from_for_desugar(decl: &hir::Decl) -> bool {
if_let_chain! {[
let hir::DeclLocal(ref loc) = decl.node,
let Some(ref expr) = loc.init,
let hir::ExprMatch(_, _, hir::MatchSource::ForLoopDesugar) = expr.node,
], {
return true;
}}
false
}
pub fn for_loop(expr: &hir::Expr) -> Option<(&hir::Pat, &hir::Expr, &hir::Expr)> {
if_let_chain! {[
let hir::ExprMatch(ref iterexpr, ref arms, _) = expr.node,
let hir::ExprCall(_, ref iterargs) = iterexpr.node,
iterargs.len() == 1 && arms.len() == 1 && arms[0].guard.is_none(),
let hir::ExprLoop(ref block, _, _) = arms[0].body.node,
block.stmts.is_empty(),
let Some(ref loopexpr) = block.expr,
let hir::ExprMatch(_, ref innerarms, hir::MatchSource::ForLoopDesugar) = loopexpr.node,
innerarms.len() == 2 && innerarms[0].pats.len() == 1,
let hir::PatKind::TupleStruct(_, ref somepats, _) = innerarms[0].pats[0].node,
somepats.len() == 1
], {
return Some((&somepats[0],
&iterargs[0],
&innerarms[0].body));
}}
None
}
pub enum VecArgs<'a> {
Repeat(&'a hir::Expr, &'a hir::Expr),
Vec(&'a [hir::Expr]),
}
pub fn vec_macro<'e>(cx: &LateContext, expr: &'e hir::Expr) -> Option<VecArgs<'e>> {
if_let_chain!{[
let hir::ExprCall(ref fun, ref args) = expr.node,
let hir::ExprPath(ref path) = fun.node,
is_expn_of(cx, fun.span, "vec").is_some(),
], {
let fun_def = resolve_node(cx, path, fun.id);
return if match_def_path(cx, fun_def.def_id(), &paths::VEC_FROM_ELEM) && args.len() == 2 {
Some(VecArgs::Repeat(&args[0], &args[1]))
}
else if match_def_path(cx, fun_def.def_id(), &paths::SLICE_INTO_VEC) && args.len() == 1 {
if_let_chain!{[
let hir::ExprBox(ref boxed) = args[0].node,
let hir::ExprArray(ref args) = boxed.node
], {
return Some(VecArgs::Vec(&*args));
}}
None
}
else {
None
};
}}
None
}