use rustc::lint::Context;
use syntax::ast::{DefId, Name, Path};
use syntax::codemap::{ExpnInfo, Span};
use rustc::middle::ty;
pub fn in_macro(cx: &Context, opt_info: Option<&ExpnInfo>) -> bool {
opt_info.map_or(false, |info| {
info.callee.span.map_or(true, |span| {
cx.sess().codemap().span_to_snippet(span).ok().map_or(true, |code|
!code.starts_with("macro_rules")
)
})
})
}
pub fn in_external_macro(cx: &Context, span: Span) -> bool {
cx.sess().codemap().with_expn_info(span.expn_id,
|info| in_macro(cx, info))
}
pub fn match_def_path(cx: &Context, def_id: DefId, path: &[&str]) -> bool {
ty::with_path(cx.tcx, def_id, |iter| iter.map(|elem| elem.name())
.zip(path.iter()).all(|(nm, p)| &nm.as_str() == p))
}
pub fn match_path(path: &Path, segments: &[&str]) -> bool {
path.segments.iter().rev().zip(segments.iter().rev()).all(
|(a,b)| a.identifier.as_str() == *b)
}