]> git.lizzy.rs Git - rust.git/commitdiff
Add lint for useless transmutes
authorAndrew Paseltiner <apaseltiner@gmail.com>
Wed, 11 Nov 2015 14:28:31 +0000 (09:28 -0500)
committerAndrew Paseltiner <apaseltiner@gmail.com>
Wed, 11 Nov 2015 15:53:11 +0000 (10:53 -0500)
Closes #441.

README.md
src/lib.rs
src/transmute.rs [new file with mode: 0644]
tests/compile-fail/transmute.rs [new file with mode: 0644]

index 6727707717f20bc75f9daae9d9f03aac76023f72..b893c4e7a1796ed120f8fd358b9c8cc055eca58c 100644 (file)
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@ A collection of lints to catch common mistakes and improve your Rust code.
 [Jump to usage instructions](#usage)
 
 ##Lints
-There are 73 lints included in this crate:
+There are 74 lints included in this crate:
 
 name                                                                                                   | default | meaning
 -------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -77,6 +77,7 @@ name
 [unstable_as_mut_slice](https://github.com/Manishearth/rust-clippy/wiki#unstable_as_mut_slice)         | warn    | as_mut_slice is not stable and can be replaced by &mut v[..]see https://github.com/rust-lang/rust/issues/27729
 [unstable_as_slice](https://github.com/Manishearth/rust-clippy/wiki#unstable_as_slice)                 | warn    | as_slice is not stable and can be replaced by & v[..]see https://github.com/rust-lang/rust/issues/27729
 [unused_collect](https://github.com/Manishearth/rust-clippy/wiki#unused_collect)                       | warn    | `collect()`ing an iterator without using the result; this is usually better written as a for loop
+[useless_transmute](https://github.com/Manishearth/rust-clippy/wiki#useless_transmute)                 | warn    | transmutes that have the same to and from types
 [while_let_loop](https://github.com/Manishearth/rust-clippy/wiki#while_let_loop)                       | warn    | `loop { if let { ... } else break }` can be written as a `while let` loop
 [while_let_on_iterator](https://github.com/Manishearth/rust-clippy/wiki#while_let_on_iterator)         | warn    | using a while-let loop instead of a for loop on an iterator
 [wrong_pub_self_convention](https://github.com/Manishearth/rust-clippy/wiki#wrong_pub_self_convention) | allow   | defining a public method named with an established prefix (like "into_") that takes `self` with the wrong convention
index 970a244e2cd7c8dc6c37dc85a32cff8287e165fb..afbf3d2a92c9d4894dafb4882cb767e948ffeafe 100644 (file)
@@ -55,6 +55,7 @@
 pub mod needless_update;
 pub mod no_effect;
 pub mod temporary_assignment;
+pub mod transmute;
 
 mod reexport {
     pub use syntax::ast::{Name, Ident, NodeId};
@@ -104,6 +105,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::UselessTransmute);
 
     reg.register_lint_group("clippy_pedantic", vec![
         methods::OPTION_UNWRAP_USED,
@@ -175,6 +177,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
         returns::LET_AND_RETURN,
         returns::NEEDLESS_RETURN,
         temporary_assignment::TEMPORARY_ASSIGNMENT,
+        transmute::USELESS_TRANSMUTE,
         types::BOX_VEC,
         types::LET_UNIT_VALUE,
         types::LINKEDLIST,
diff --git a/src/transmute.rs b/src/transmute.rs
new file mode 100644 (file)
index 0000000..ab1397e
--- /dev/null
@@ -0,0 +1,37 @@
+use rustc::lint::*;
+use rustc_front::hir::*;
+use utils;
+
+declare_lint! {
+    pub USELESS_TRANSMUTE,
+    Warn,
+    "transmutes that have the same to and from types"
+}
+
+pub struct UselessTransmute;
+
+impl LintPass for UselessTransmute {
+    fn get_lints(&self) -> LintArray {
+        lint_array!(USELESS_TRANSMUTE)
+    }
+}
+
+impl LateLintPass for UselessTransmute {
+    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 from_ty == to_ty {
+                        cx.span_lint(USELESS_TRANSMUTE, e.span,
+                                     &format!("transmute from a type (`{}`) to itself", from_ty));
+                    }
+                }
+            }
+        }
+    }
+}
diff --git a/tests/compile-fail/transmute.rs b/tests/compile-fail/transmute.rs
new file mode 100644 (file)
index 0000000..0a2d09d
--- /dev/null
@@ -0,0 +1,46 @@
+#![feature(core)]
+#![feature(plugin)]
+#![plugin(clippy)]
+#![deny(useless_transmute)]
+
+extern crate core;
+
+use std::mem::transmute as my_transmute;
+use std::vec::Vec as MyVec;
+
+fn my_vec() -> MyVec<i32> {
+    vec![]
+}
+
+#[allow(needless_lifetimes)]
+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
+
+    let _: &'a U = core::intrinsics::transmute(t);
+}
+
+fn main() {
+    unsafe {
+        let _: Vec<i32> = core::intrinsics::transmute(my_vec());
+        //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
+
+        let _: Vec<i32> = core::mem::transmute(my_vec());
+        //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
+
+        let _: Vec<i32> = std::intrinsics::transmute(my_vec());
+        //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
+
+        let _: Vec<i32> = std::mem::transmute(my_vec());
+        //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
+
+        let _: Vec<i32> = my_transmute(my_vec());
+        //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
+
+        let _: Vec<u32> = core::intrinsics::transmute(my_vec());
+        let _: Vec<u32> = core::mem::transmute(my_vec());
+        let _: Vec<u32> = std::intrinsics::transmute(my_vec());
+        let _: Vec<u32> = std::mem::transmute(my_vec());
+        let _: Vec<u32> = my_transmute(my_vec());
+    }
+}