diff options
Diffstat (limited to 'components/plugins/utils.rs')
-rw-r--r-- | components/plugins/utils.rs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/components/plugins/utils.rs b/components/plugins/utils.rs index 34d74a4001f..132d7afaaa8 100644 --- a/components/plugins/utils.rs +++ b/components/plugins/utils.rs @@ -4,12 +4,14 @@ use rustc::lint::Context; use rustc::middle::{ty, def}; +use rustc::middle::typeck::astconv::AstConv; use syntax::ptr::P; -use syntax::ast; +use syntax::{ast, ast_map}; use syntax::ast::{TyPath, Path, AngleBracketedParameters, PathSegment, Ty}; use syntax::attr::mark_used; + /// Matches a type with a provided string, and returns its type parameters if successful /// /// Try not to use this for types defined in crates you own, use match_lang_ty instead (for lint passes) @@ -60,3 +62,32 @@ pub fn match_lang_ty(cx: &Context, ty: &Ty, value: &str) -> bool { } found } + +// Determines if a block is in an unsafe context so that an unhelpful +// lint can be aborted. +pub fn unsafe_context(map: &ast_map::Map, id: ast::NodeId) -> bool { + match map.find(map.get_parent(id)) { + Some(ast_map::NodeImplItem(itm)) => { + match *itm { + ast::MethodImplItem(ref meth) => match meth.node { + ast::MethDecl(_, _, _, _, style, _, _, _) => match style { + ast::UnsafeFn => true, + _ => false, + }, + _ => false, + }, + _ => false, + } + }, + Some(ast_map::NodeItem(itm)) => { + match itm.node { + ast::ItemFn(_, style, _, _, _) => match style { + ast::UnsafeFn => true, + _ => false, + }, + _ => false, + } + } + _ => false // There are probably a couple of other unsafe cases we don't care to lint, those will need to be added. + } +} |