]> git.lizzy.rs Git - rust.git/commitdiff
Cleanup and added transmute to ugly path list
authorTaylor Cramer <cramertj@cs.washington.edu>
Thu, 24 Mar 2016 23:38:16 +0000 (16:38 -0700)
committerTaylor Cramer <cramertj@cs.washington.edu>
Thu, 24 Mar 2016 23:38:16 +0000 (16:38 -0700)
src/transmute.rs
src/utils/mod.rs

index daefbaf07ad445e057590c508046e9d190970251..488d214b21f0f13e47b16537da7301b5bc61da29 100644 (file)
@@ -3,6 +3,7 @@
 use rustc::middle::ty::TyS;
 use rustc::middle::ty::TypeVariants::TyRawPtr;
 use utils;
+use utils::TRANSMUTE_PATH;
 
 /// **What it does:** This lint checks for transmutes to the original type of the object.
 ///
@@ -17,7 +18,7 @@
     "transmutes that have the same to and from types"
 }
 
-/// **What it does:*** This lint checks for transmutes between a type T and *T.
+/// **What it does:*** This lint checks for transmutes between a type `T` and `*T`.
 ///
 /// **Why is this bad?** It's easy to mistakenly transmute between a type and a pointer to that type.
 ///
@@ -44,7 +45,7 @@ fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
             if let ExprPath(None, _) = path_expr.node {
                 let def_id = cx.tcx.def_map.borrow()[&path_expr.id].def_id();
 
-                if utils::match_def_path(cx, def_id, &["core", "intrinsics", "transmute"]) {
+                if utils::match_def_path(cx, def_id, &TRANSMUTE_PATH) {
                     let from_ty = cx.tcx.expr_ty(&args[0]);
                     let to_ty = cx.tcx.expr_ty(e);
 
@@ -81,7 +82,7 @@ fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
             if let ExprPath(None, _) = path_expr.node {
                 let def_id = cx.tcx.def_map.borrow()[&path_expr.id].def_id();
 
-                if utils::match_def_path(cx, def_id, &["core", "intrinsics", "transmute"]) {
+                if utils::match_def_path(cx, def_id, &TRANSMUTE_PATH) {
                     let from_ty = cx.tcx.expr_ty(&args[0]);
                     let to_ty = cx.tcx.expr_ty(e);
 
@@ -89,9 +90,7 @@ fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
                         cx.span_lint(CROSSPOINTER_TRANSMUTE,
                                      e.span,
                                      &format!("transmute from a type (`{}`) to a pointer to that type (`{}`)", from_ty, to_ty));
-                    }
-
-                    if is_ptr_to(from_ty, to_ty) {
+                    } else if is_ptr_to(from_ty, to_ty) {
                         cx.span_lint(CROSSPOINTER_TRANSMUTE,
                                      e.span,
                                      &format!("transmute from a type (`{}`) to the type that it points to (`{}`)", from_ty, to_ty));
index 050aec0e43041824864086a02b6108dd26b14c77..e7b2a9d5210f01d1727bd138f608425a5718a3be 100644 (file)
@@ -51,6 +51,7 @@
 pub const REGEX_NEW_PATH: [&'static str; 3] = ["regex", "Regex", "new"];
 pub const RESULT_PATH: [&'static str; 3] = ["core", "result", "Result"];
 pub const STRING_PATH: [&'static str; 3] = ["collections", "string", "String"];
+pub const TRANSMUTE_PATH: [&'static str; 3] = ["core", "intrinsics", "transmute"];
 pub const VEC_FROM_ELEM_PATH: [&'static str; 3] = ["std", "vec", "from_elem"];
 pub const VEC_PATH: [&'static str; 3] = ["collections", "vec", "Vec"];