1 use rustc::lint::{Context, Lint, Level};
2 use syntax::ast::{DefId, Expr, Name, NodeId, Path};
3 use syntax::codemap::{ExpnInfo, Span};
5 use rustc::ast_map::Node::NodeExpr;
7 use std::borrow::{Cow, IntoCow};
8 use std::convert::From;
10 /// returns true if the macro that expanded the crate was outside of
11 /// the current crate or was a compiler plugin
12 pub fn in_macro(cx: &Context, opt_info: Option<&ExpnInfo>) -> bool {
13 // no ExpnInfo = no macro
14 opt_info.map_or(false, |info| {
15 // no span for the callee = external macro
16 info.callee.span.map_or(true, |span| {
17 // no snippet = external macro or compiler-builtin expansion
18 cx.sess().codemap().span_to_snippet(span).ok().map_or(true, |code|
19 // macro doesn't start with "macro_rules"
21 !code.starts_with("macro_rules")
27 /// invokes in_macro with the expansion info of the given span
28 pub fn in_external_macro(cx: &Context, span: Span) -> bool {
29 cx.sess().codemap().with_expn_info(span.expn_id,
30 |info| in_macro(cx, info))
33 /// check if a DefId's path matches the given absolute type path
35 /// `match_def_path(cx, id, &["core", "option", "Option"])`
36 pub fn match_def_path(cx: &Context, def_id: DefId, path: &[&str]) -> bool {
37 cx.tcx.with_path(def_id, |iter| iter.map(|elem| elem.name())
38 .zip(path.iter()).all(|(nm, p)| &nm.as_str() == p))
41 /// match a Path against a slice of segment string literals, e.g.
42 /// `match_path(path, &["std", "rt", "begin_unwind"])`
43 pub fn match_path(path: &Path, segments: &[&str]) -> bool {
44 path.segments.iter().rev().zip(segments.iter().rev()).all(
45 |(a,b)| &a.identifier.name.as_str() == b)
48 /// convert a span to a code snippet if available, otherwise use default, e.g.
49 /// `snippet(cx, expr.span, "..")`
50 pub fn snippet<'a>(cx: &Context, span: Span, default: &'a str) -> Cow<'a, str> {
51 cx.sess().codemap().span_to_snippet(span).map(From::from).unwrap_or(Cow::Borrowed(default))
54 /// get a parent expr if any – this is useful to constrain a lint
55 pub fn get_parent_expr<'c>(cx: &'c Context, e: &Expr) -> Option<&'c Expr> {
56 let map = &cx.tcx.map;
57 let node_id : NodeId = e.id;
58 let parent_id : NodeId = map.get_parent_node(node_id);
59 if node_id == parent_id { return None; }
60 map.find(parent_id).and_then(|node|
61 if let NodeExpr(parent) = node { Some(parent) } else { None } )
64 /// dereference a P<T> and return a ref on the result
65 pub fn de_p<T>(p: &P<T>) -> &T { &*p }
67 #[cfg(not(feature="structured_logging"))]
68 pub fn span_lint(cx: &Context, lint: &'static Lint, sp: Span, msg: &str) {
69 cx.span_lint(lint, sp, msg);
72 #[cfg(feature="structured_logging")]
73 pub fn span_lint(cx: &Context, lint: &'static Lint, sp: Span, msg: &str) {
74 // lint.name / lint.desc is can give details of the lint
75 // cx.sess().codemap() has all these nice functions for line/column/snippet details
76 // http://doc.rust-lang.org/syntax/codemap/struct.CodeMap.html#method.span_to_string
77 cx.span_lint(lint, sp, msg);
80 pub fn span_help_and_lint(cx: &Context, lint: &'static Lint, span: Span,
81 msg: &str, help: &str) {
82 span_lint(cx, lint, span, msg);
83 if cx.current_level(lint) != Level::Allow {
84 cx.sess().fileline_help(span, help);
88 /// return the base type for references and raw pointers
89 pub fn walk_ptrs_ty<'t>(ty: ty::Ty<'t>) -> ty::Ty<'t> {
91 ty::TyRef(_, ref tm) | ty::TyRawPtr(ref tm) => walk_ptrs_ty(tm.ty),
96 /// Produce a nested chain of if-lets and ifs from the patterns:
111 /// if let Some(y) = x {
112 /// if y.len() == 2 {
113 /// if let Some(z) = y {
119 macro_rules! if_let_chain {
120 ([let $pat:pat = $expr:expr, $($tt:tt)+], $block:block) => {
121 if let $pat = $expr {
122 if_let_chain!{ [$($tt)+], $block }
125 ([let $pat:pat = $expr:expr], $block:block) => {
126 if let $pat = $expr {
130 ([$expr:expr, $($tt:tt)+], $block:block) => {
132 if_let_chain!{ [$($tt)+], $block }
135 ([$expr:expr], $block:block) => {