diff --git a/src/utils.rs b/src/utils.rs index 4b144452063..c41c0a8681b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,20 +1,20 @@ -use rustc::lint::*; -use rustc_front::hir::*; +use consts::constant; use reexport::*; -use syntax::codemap::{ExpnInfo, Span, ExpnFormat}; use rustc::front::map::Node::*; +use rustc::lint::*; use rustc::middle::def_id::DefId; -use rustc::middle::ty; +use rustc::middle::{cstore, def, infer, ty, traits}; +use rustc::session::Session; +use rustc_front::hir::*; use std::borrow::Cow; +use std::mem; +use std::ops::{Deref, DerefMut}; +use std::str::FromStr; use syntax::ast::Lit_::*; use syntax::ast; +use syntax::codemap::{ExpnInfo, Span, ExpnFormat}; use syntax::errors::DiagnosticBuilder; use syntax::ptr::P; -use consts::constant; - -use rustc::session::Session; -use std::str::FromStr; -use std::ops::{Deref, DerefMut}; pub type MethodArgs = HirVec
>;
@@ -23,6 +23,7 @@ pub const BEGIN_UNWIND: [&'static str; 3] = ["std", "rt", "begin_unwind"];
pub const BTREEMAP_PATH: [&'static str; 4] = ["collections", "btree", "map", "BTreeMap"];
pub const CLONE_PATH: [&'static str; 2] = ["Clone", "clone"];
pub const COW_PATH: [&'static str; 3] = ["collections", "borrow", "Cow"];
+pub const DEFAULT_TRAIT_PATH: [&'static str; 3] = ["core", "default", "Default"];
pub const HASHMAP_PATH: [&'static str; 5] = ["std", "collections", "hash", "map", "HashMap"];
pub const LL_PATH: [&'static str; 3] = ["collections", "linked_list", "LinkedList"];
pub const MUTEX_PATH: [&'static str; 4] = ["std", "sync", "mutex", "Mutex"];
@@ -132,7 +133,7 @@ pub fn match_type(cx: &LateContext, ty: ty::Ty, path: &[&str]) -> bool {
}
}
-/// Check if the method call given in `expr` belongs to given trait.
+/// Check if the method call given in `expr` belongs to given type.
pub fn match_impl_method(cx: &LateContext, expr: &Expr, path: &[&str]) -> bool {
let method_call = ty::MethodCall::expr(expr.id);
@@ -186,6 +187,73 @@ pub fn match_path_ast(path: &ast::Path, segments: &[&str]) -> bool {
path.segments.iter().rev().zip(segments.iter().rev()).all(|(a, b)| a.identifier.name.as_str() == *b)
}
+/// Get the definition associated to a path.
+/// TODO: investigate if there is something more efficient for that.
+pub fn path_to_def(cx: &LateContext, path: &[&str]) -> Option