2 use rustc_front::hir::*;
4 use syntax::codemap::{ExpnInfo, Span, ExpnFormat};
5 use rustc::front::map::Node::*;
6 use rustc::middle::def_id::DefId;
9 use syntax::ast::Lit_::*;
12 use rustc::session::Session;
13 use std::str::FromStr;
15 // module DefPaths for certain structs/enums we check for
16 pub const OPTION_PATH: [&'static str; 3] = ["core", "option", "Option"];
17 pub const RESULT_PATH: [&'static str; 3] = ["core", "result", "Result"];
18 pub const STRING_PATH: [&'static str; 3] = ["collections", "string", "String"];
19 pub const VEC_PATH: [&'static str; 3] = ["collections", "vec", "Vec"];
20 pub const LL_PATH: [&'static str; 3] = ["collections", "linked_list", "LinkedList"];
21 pub const OPEN_OPTIONS_PATH: [&'static str; 3] = ["std", "fs", "OpenOptions"];
22 pub const MUTEX_PATH: [&'static str; 4] = ["std", "sync", "mutex", "Mutex"];
23 pub const CLONE_PATH: [&'static str; 2] = ["Clone", "clone"];
25 /// Produce a nested chain of if-lets and ifs from the patterns:
40 /// if let Some(y) = x {
42 /// if let Some(z) = y {
48 macro_rules! if_let_chain {
49 ([let $pat:pat = $expr:expr, $($tt:tt)+], $block:block) => {
51 if_let_chain!{ [$($tt)+], $block }
54 ([let $pat:pat = $expr:expr], $block:block) => {
59 ([$expr:expr, $($tt:tt)+], $block:block) => {
61 if_let_chain!{ [$($tt)+], $block }
64 ([$expr:expr], $block:block) => {
71 /// returns true if this expn_info was expanded by any macro
72 pub fn in_macro<T: LintContext>(cx: &T, span: Span) -> bool {
73 cx.sess().codemap().with_expn_info(span.expn_id,
74 |info| info.is_some())
77 /// returns true if the macro that expanded the crate was outside of
78 /// the current crate or was a compiler plugin
79 pub fn in_external_macro<T: LintContext>(cx: &T, span: Span) -> bool {
80 /// invokes in_macro with the expansion info of the given span
81 /// slightly heavy, try to use this after other checks have already happened
82 fn in_macro_ext<T: LintContext>(cx: &T, opt_info: Option<&ExpnInfo>) -> bool {
83 // no ExpnInfo = no macro
84 opt_info.map_or(false, |info| {
85 if let ExpnFormat::MacroAttribute(..) = info.callee.format {
86 // these are all plugins
89 // no span for the callee = external macro
90 info.callee.span.map_or(true, |span| {
91 // no snippet = external macro or compiler-builtin expansion
92 cx.sess().codemap().span_to_snippet(span).ok().map_or(true, |code|
93 // macro doesn't start with "macro_rules"
95 !code.starts_with("macro_rules")
101 cx.sess().codemap().with_expn_info(span.expn_id,
102 |info| in_macro_ext(cx, info))
105 /// check if a DefId's path matches the given absolute type path
107 /// `match_def_path(cx, id, &["core", "option", "Option"])`
108 pub fn match_def_path(cx: &LateContext, def_id: DefId, path: &[&str]) -> bool {
109 cx.tcx.with_path(def_id, |iter| iter.zip(path)
110 .all(|(nm, p)| nm.name().as_str() == *p))
113 /// check if type is struct or enum type with given def path
114 pub fn match_type(cx: &LateContext, ty: ty::Ty, path: &[&str]) -> bool {
116 ty::TyEnum(ref adt, _) | ty::TyStruct(ref adt, _) => {
117 match_def_path(cx, adt.did, path)
125 /// check if method call given in "expr" belongs to given trait
126 pub fn match_impl_method(cx: &LateContext, expr: &Expr, path: &[&str]) -> bool {
127 let method_call = ty::MethodCall::expr(expr.id);
129 let trt_id = cx.tcx.tables
130 .borrow().method_map.get(&method_call)
131 .and_then(|callee| cx.tcx.impl_of_method(callee.def_id));
132 if let Some(trt_id) = trt_id {
133 match_def_path(cx, trt_id, path)
138 /// check if method call given in "expr" belongs to given trait
139 pub fn match_trait_method(cx: &LateContext, expr: &Expr, path: &[&str]) -> bool {
140 let method_call = ty::MethodCall::expr(expr.id);
141 let trt_id = cx.tcx.tables
142 .borrow().method_map.get(&method_call)
143 .and_then(|callee| cx.tcx.trait_of_item(callee.def_id));
144 if let Some(trt_id) = trt_id {
145 match_def_path(cx, trt_id, path)
151 /// match a Path against a slice of segment string literals, e.g.
152 /// `match_path(path, &["std", "rt", "begin_unwind"])`
153 pub fn match_path(path: &Path, segments: &[&str]) -> bool {
154 path.segments.iter().rev().zip(segments.iter().rev()).all(
155 |(a, b)| a.identifier.name.as_str() == *b)
158 /// match a Path against a slice of segment string literals, e.g.
159 /// `match_path(path, &["std", "rt", "begin_unwind"])`
160 pub fn match_path_ast(path: &ast::Path, segments: &[&str]) -> bool {
161 path.segments.iter().rev().zip(segments.iter().rev()).all(
162 |(a, b)| a.identifier.name.as_str() == *b)
165 /// get the name of the item the expression is in, if available
166 pub fn get_item_name(cx: &LateContext, expr: &Expr) -> Option<Name> {
167 let parent_id = cx.tcx.map.get_parent(expr.id);
168 match cx.tcx.map.find(parent_id) {
169 Some(NodeItem(&Item{ ref name, .. })) |
170 Some(NodeTraitItem(&TraitItem{ ref name, .. })) |
171 Some(NodeImplItem(&ImplItem{ ref name, .. })) => {
178 /// checks if a `let` decl is from a for loop desugaring
179 pub fn is_from_for_desugar(decl: &Decl) -> bool {
182 let DeclLocal(ref loc) = decl.node,
183 let Some(ref expr) = loc.init,
184 let ExprMatch(_, _, MatchSource::ForLoopDesugar) = expr.node
192 /// convert a span to a code snippet if available, otherwise use default, e.g.
193 /// `snippet(cx, expr.span, "..")`
194 pub fn snippet<'a, T: LintContext>(cx: &T, span: Span, default: &'a str) -> Cow<'a, str> {
195 cx.sess().codemap().span_to_snippet(span).map(From::from).unwrap_or(Cow::Borrowed(default))
198 /// convert a span (from a block) to a code snippet if available, otherwise use default, e.g.
199 /// `snippet(cx, expr.span, "..")`
200 /// This trims the code of indentation, except for the first line
201 /// Use it for blocks or block-like things which need to be printed as such
202 pub fn snippet_block<'a, T: LintContext>(cx: &T, span: Span, default: &'a str) -> Cow<'a, str> {
203 let snip = snippet(cx, span, default);
204 trim_multiline(snip, true)
207 /// Like snippet_block, but add braces if the expr is not an ExprBlock
208 /// Also takes an Option<String> which can be put inside the braces
209 pub fn expr_block<'a, T: LintContext>(cx: &T, expr: &Expr,
210 option: Option<String>,
211 default: &'a str) -> Cow<'a, str> {
212 let code = snippet_block(cx, expr.span, default);
213 let string = option.map_or("".to_owned(), |s| s);
214 if let ExprBlock(_) = expr.node {
215 Cow::Owned(format!("{}{}", code, string))
216 } else if string.is_empty() {
217 Cow::Owned(format!("{{ {} }}", code))
219 Cow::Owned(format!("{{\n{};\n{}\n}}", code, string))
223 /// Trim indentation from a multiline string
224 /// with possibility of ignoring the first line
225 pub fn trim_multiline(s: Cow<str>, ignore_first: bool) -> Cow<str> {
226 let s_space = trim_multiline_inner(s, ignore_first, ' ');
227 let s_tab = trim_multiline_inner(s_space, ignore_first, '\t');
228 trim_multiline_inner(s_tab, ignore_first, ' ')
231 fn trim_multiline_inner(s: Cow<str>, ignore_first: bool, ch: char) -> Cow<str> {
232 let x = s.lines().skip(ignore_first as usize)
233 .filter_map(|l| { if l.len() > 0 { // ignore empty lines
234 Some(l.char_indices()
235 .find(|&(_,x)| x != ch)
236 .unwrap_or((l.len(), ch)).0)
240 Cow::Owned(s.lines().enumerate().map(|(i,l)| if (ignore_first && i == 0) ||
245 }).collect::<Vec<_>>()
252 /// get a parent expr if any – this is useful to constrain a lint
253 pub fn get_parent_expr<'c>(cx: &'c LateContext, e: &Expr) -> Option<&'c Expr> {
254 let map = &cx.tcx.map;
255 let node_id : NodeId = e.id;
256 let parent_id : NodeId = map.get_parent_node(node_id);
257 if node_id == parent_id { return None; }
258 map.find(parent_id).and_then(|node|
259 if let NodeExpr(parent) = node { Some(parent) } else { None } )
262 pub fn get_enclosing_block<'c>(cx: &'c LateContext, node: NodeId) -> Option<&'c Block> {
263 let map = &cx.tcx.map;
264 let enclosing_node = map.get_enclosing_scope(node)
265 .and_then(|enclosing_id| map.find(enclosing_id));
266 if let Some(node) = enclosing_node {
268 NodeBlock(ref block) => Some(block),
269 NodeItem(&Item{ node: ItemFn(_, _, _, _, _, ref block), .. }) => Some(block),
275 #[cfg(not(feature="structured_logging"))]
276 pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: Span, msg: &str) {
277 cx.span_lint(lint, sp, msg);
278 if cx.current_level(lint) != Level::Allow {
279 cx.sess().fileline_help(sp, &format!("for further information visit \
280 https://github.com/Manishearth/rust-clippy/wiki#{}",
285 #[cfg(feature="structured_logging")]
286 pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: Span, msg: &str) {
287 // lint.name / lint.desc is can give details of the lint
288 // cx.sess().codemap() has all these nice functions for line/column/snippet details
289 // http://doc.rust-lang.org/syntax/codemap/struct.CodeMap.html#method.span_to_string
290 cx.span_lint(lint, sp, msg);
291 if cx.current_level(lint) != Level::Allow {
292 cx.sess().fileline_help(sp, &format!("for further information visit \
293 https://github.com/Manishearth/rust-clippy/wiki#{}",
298 pub fn span_help_and_lint<T: LintContext>(cx: &T, lint: &'static Lint, span: Span,
299 msg: &str, help: &str) {
300 cx.span_lint(lint, span, msg);
301 if cx.current_level(lint) != Level::Allow {
302 cx.sess().fileline_help(span, &format!("{}\nfor further information \
303 visit https://github.com/Manishearth/rust-clippy/wiki#{}",
304 help, lint.name_lower()))
308 pub fn span_note_and_lint<T: LintContext>(cx: &T, lint: &'static Lint, span: Span,
309 msg: &str, note_span: Span, note: &str) {
310 cx.span_lint(lint, span, msg);
311 if cx.current_level(lint) != Level::Allow {
312 if note_span == span {
313 cx.sess().fileline_note(note_span, note)
315 cx.sess().span_note(note_span, note)
317 cx.sess().fileline_help(span, &format!("for further information visit \
318 https://github.com/Manishearth/rust-clippy/wiki#{}",
323 /// return the base type for references and raw pointers
324 pub fn walk_ptrs_ty(ty: ty::Ty) -> ty::Ty {
326 ty::TyRef(_, ref tm) | ty::TyRawPtr(ref tm) => walk_ptrs_ty(tm.ty),
331 /// return the base type for references and raw pointers, and count reference depth
332 pub fn walk_ptrs_ty_depth(ty: ty::Ty) -> (ty::Ty, usize) {
333 fn inner(ty: ty::Ty, depth: usize) -> (ty::Ty, usize) {
335 ty::TyRef(_, ref tm) | ty::TyRawPtr(ref tm) => inner(tm.ty, depth + 1),
342 pub fn is_integer_literal(expr: &Expr, value: u64) -> bool
344 // FIXME: use constant folding
345 if let ExprLit(ref spanned) = expr.node {
346 if let LitInt(v, _) = spanned.node {
353 pub fn is_adjusted(cx: &LateContext, e: &Expr) -> bool {
354 cx.tcx.tables.borrow().adjustments.get(&e.id).is_some()
357 /// Produce a nested chain of if-lets and ifs from the patterns:
372 /// if let Some(y) = x {
373 /// if y.len() == 2 {
374 /// if let Some(z) = y {
380 macro_rules! if_let_chain {
381 ([let $pat:pat = $expr:expr, $($tt:tt)+], $block:block) => {
382 if let $pat = $expr {
383 if_let_chain!{ [$($tt)+], $block }
386 ([let $pat:pat = $expr:expr], $block:block) => {
387 if let $pat = $expr {
391 ([$expr:expr, $($tt:tt)+], $block:block) => {
393 if_let_chain!{ [$($tt)+], $block }
396 ([$expr:expr], $block:block) => {
403 pub struct LimitStack {
407 impl Drop for LimitStack {
409 assert_eq!(self.stack.len(), 1);
414 pub fn new(limit: u64) -> LimitStack {
419 pub fn limit(&self) -> u64 {
420 *self.stack.last().expect("there should always be a value in the stack")
422 pub fn push_attrs(&mut self, sess: &Session, attrs: &[ast::Attribute], name: &'static str) {
423 let stack = &mut self.stack;
428 |val| stack.push(val),
431 pub fn pop_attrs(&mut self, sess: &Session, attrs: &[ast::Attribute], name: &'static str) {
432 let stack = &mut self.stack;
437 |val| assert_eq!(stack.pop(), Some(val)),
442 fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[ast::Attribute], name: &'static str, mut f: F) {
444 let attr = &attr.node;
445 if attr.is_sugared_doc { continue; }
446 if let ast::MetaNameValue(ref key, ref value) = attr.value.node {
448 if let LitStr(ref s, _) = value.node {
449 if let Ok(value) = FromStr::from_str(s) {
452 sess.span_err(value.span, "not a number");