]> git.lizzy.rs Git - rust.git/commitdiff
Added crosspointer transmute error and tests
authorTaylor Cramer <cramertj@cs.washington.edu>
Thu, 24 Mar 2016 22:48:38 +0000 (15:48 -0700)
committerTaylor Cramer <cramertj@cs.washington.edu>
Thu, 24 Mar 2016 22:48:38 +0000 (15:48 -0700)
src/lib.rs
src/transmute.rs
tests/compile-fail/transmute.rs

index ffa66fd581b05453ca8034d7b41bf62a59ad76b6..8a4a84f5ddee84caa79ade439579f6e0a3dda1e9 100644 (file)
@@ -195,6 +195,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
     reg.register_late_lint_pass(box no_effect::NoEffectPass);
     reg.register_late_lint_pass(box map_clone::MapClonePass);
     reg.register_late_lint_pass(box temporary_assignment::TemporaryAssignmentPass);
+    reg.register_late_lint_pass(box transmute::CrosspointerTransmute);
     reg.register_late_lint_pass(box transmute::UselessTransmute);
     reg.register_late_lint_pass(box cyclomatic_complexity::CyclomaticComplexity::new(conf.cyclomatic_complexity_threshold));
     reg.register_late_lint_pass(box escape::EscapePass);
@@ -350,6 +351,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
         swap::ALMOST_SWAPPED,
         swap::MANUAL_SWAP,
         temporary_assignment::TEMPORARY_ASSIGNMENT,
+        transmute::CROSSPOINTER_TRANSMUTE,
         transmute::USELESS_TRANSMUTE,
         types::ABSURD_EXTREME_COMPARISONS,
         types::BOX_VEC,
index 0dd5b60e77adc161d8f6643c2eb4101122402693..daefbaf07ad445e057590c508046e9d190970251 100644 (file)
@@ -1,5 +1,7 @@
 use rustc::lint::*;
 use rustc_front::hir::*;
+use rustc::middle::ty::TyS;
+use rustc::middle::ty::TypeVariants::TyRawPtr;
 use utils;
 
 /// **What it does:** This lint checks for transmutes to the original type of the object.
     "transmutes that have the same to and from types"
 }
 
+/// **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.
+///
+/// **Known problems:** None.
+///
+/// **Example:** `core::intrinsics::transmute(t)` where the result type is the same as `*t` or `&t`'s.
+declare_lint! {
+    pub CROSSPOINTER_TRANSMUTE,
+    Warn,
+    "transmutes that have to or from types that are a pointer to the other"
+}
+
 pub struct UselessTransmute;
 
 impl LintPass for UselessTransmute {
@@ -43,3 +58,46 @@ fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
         }
     }
 }
+
+pub struct CrosspointerTransmute;
+
+impl LintPass for CrosspointerTransmute {
+    fn get_lints(&self) -> LintArray {
+        lint_array!(CROSSPOINTER_TRANSMUTE)
+    }
+}
+
+fn is_ptr_to(from: &TyS, to: &TyS) -> bool {
+    if let TyRawPtr(from_ptr) = from.sty {
+        from_ptr.ty == to
+    } else {
+        false
+    }
+}
+
+impl LateLintPass for CrosspointerTransmute {
+    fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
+        if let ExprCall(ref path_expr, ref args) = e.node {
+            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"]) {
+                    let from_ty = cx.tcx.expr_ty(&args[0]);
+                    let to_ty = cx.tcx.expr_ty(e);
+
+                    if is_ptr_to(to_ty, from_ty) {
+                        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) {
+                        cx.span_lint(CROSSPOINTER_TRANSMUTE,
+                                     e.span,
+                                     &format!("transmute from a type (`{}`) to the type that it points to (`{}`)", from_ty, to_ty));
+                    }
+                }
+            }
+        }
+    }
+}
index 94dd3a1854927fa80d04ef1514fa88748f1c3983..b875500808666a14dfb36865dfa8a769df096595 100644 (file)
@@ -1,6 +1,5 @@
 #![feature(plugin)]
 #![plugin(clippy)]
-#![deny(useless_transmute)]
 
 extern crate core;
 
@@ -12,6 +11,7 @@ fn my_vec() -> MyVec<i32> {
 }
 
 #[allow(needless_lifetimes)]
+#[deny(useless_transmute)]
 unsafe fn _generic<'a, T, U: 'a>(t: &'a T) {
     let _: &'a T = core::intrinsics::transmute(t);
     //~^ ERROR transmute from a type (`&'a T`) to itself
@@ -19,7 +19,8 @@ unsafe fn _generic<'a, T, U: 'a>(t: &'a T) {
     let _: &'a U = core::intrinsics::transmute(t);
 }
 
-fn main() {
+#[deny(useless_transmute)]
+fn useless() {
     unsafe {
         let _: Vec<i32> = core::intrinsics::transmute(my_vec());
         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
@@ -43,3 +44,29 @@ fn main() {
         let _: Vec<u32> = my_transmute(my_vec());
     }
 }
+
+#[deny(crosspointer_transmute)]
+fn crosspointer() {
+    let mut vec: Vec<i32> = vec![];
+    let vec_const_ptr: *const Vec<i32> = &vec as *const Vec<i32>;
+    let vec_mut_ptr: *mut Vec<i32> = &mut vec as *mut Vec<i32>;
+
+    unsafe {
+        let _: Vec<i32> = core::intrinsics::transmute(vec_const_ptr);
+        //~^ ERROR transmute from a type (`*const collections::vec::Vec<i32>`) to the type that it points to (`collections::vec::Vec<i32>`)
+
+        let _: Vec<i32> = core::intrinsics::transmute(vec_mut_ptr);
+        //~^ ERROR transmute from a type (`*mut collections::vec::Vec<i32>`) to the type that it points to (`collections::vec::Vec<i32>`)
+
+        let _: *const Vec<i32> = core::intrinsics::transmute(my_vec());
+        //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to a pointer to that type (`*const collections::vec::Vec<i32>`)
+
+        let _: *mut Vec<i32> = core::intrinsics::transmute(my_vec());
+        //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to a pointer to that type (`*mut collections::vec::Vec<i32>`)
+    }
+}
+
+fn main() {
+    useless();
+    crosspointer();
+}