about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRyan1729 <Ryan1729@gmail.com>2020-08-03 02:47:25 -0600
committerRyan1729 <Ryan1729@gmail.com>2020-08-06 04:24:25 -0600
commitccc4747f468e0887f6d1c460237e2e6313ae97b9 (patch)
treeb0fc63ce4ecd7bb2963645f6ca21675ed7bd9e28
parentde05212987be6468387b004194761d5fad6d506c (diff)
downloadrust-ccc4747f468e0887f6d1c460237e2e6313ae97b9.tar.gz
rust-ccc4747f468e0887f6d1c460237e2e6313ae97b9.zip
get the expected number of errors by acknowledging that other lints are covering the same ground
-rw-r--r--clippy_lints/src/transmute.rs80
-rw-r--r--tests/ui/transmute.rs2
-rw-r--r--tests/ui/transmute_ptr_to_ptr.rs2
-rw-r--r--tests/ui/transmutes_expressible_as_ptr_casts.rs10
-rw-r--r--tests/ui/transmutes_expressible_as_ptr_casts.stderr51
5 files changed, 100 insertions, 45 deletions
diff --git a/clippy_lints/src/transmute.rs b/clippy_lints/src/transmute.rs
index 7ab3f0d9676..269d2f00353 100644
--- a/clippy_lints/src/transmute.rs
+++ b/clippy_lints/src/transmute.rs
@@ -50,6 +50,29 @@ declare_clippy_lint! {
     "transmutes that have the same to and from types or could be a cast/coercion"
 }
 
+// FIXME: Merge this lint with USELESS_TRANSMUTE once that is out of the nursery.
+declare_clippy_lint! {
+    /// **What it does:**Checks for transmutes that could be a pointer cast.
+    ///
+    /// **Why is this bad?** Readability. The code tricks people into thinking that
+    /// something complex is going on.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    ///
+    /// ```rust,ignore
+    /// core::intrinsics::transmute::<*const [i32], *const [u16]>(p)
+    /// ```
+    /// Use instead:
+    /// ```rust
+    /// p as *const [u16]
+    /// ```
+    pub TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
+    complexity,
+    "transmutes that could be a pointer cast"
+}
+
 declare_clippy_lint! {
     /// **What it does:** Checks for transmutes between a type `T` and `*T`.
     ///
@@ -272,27 +295,6 @@ declare_clippy_lint! {
     "transmute between collections of layout-incompatible types"
 }
 
-declare_clippy_lint! {
-    /// **What it does:**
-    ///
-    /// **Why is this bad?**
-    ///
-    /// **Known problems:** None.
-    ///
-    /// **Example:**
-    ///
-    /// ```rust
-    /// // example code where clippy issues a warning
-    /// ```
-    /// Use instead:
-    /// ```rust
-    /// // example code which does not raise clippy warning
-    /// ```
-    pub TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
-    complexity,
-    "default lint description"
-}
-
 declare_lint_pass!(Transmute => [
     CROSSPOINTER_TRANSMUTE,
     TRANSMUTE_PTR_TO_REF,
@@ -330,26 +332,6 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
                 let from_ty = cx.typeck_results().expr_ty(&args[0]);
                 let to_ty = cx.typeck_results().expr_ty(e);
 
-                if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) {
-                    span_lint_and_then(
-                        cx,
-                        TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
-                        e.span,
-                        &format!(
-                            "transmute from `{}` to `{}` which could be expressed as a pointer cast instead",
-                            from_ty,
-                            to_ty
-                        ),
-                        |diag| {
-                            if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
-                                let sugg = format!("{} as {}", arg, to_ty);
-                                diag.span_suggestion(e.span, "try", sugg, Applicability::Unspecified);
-                            }
-                        }
-                    );
-                    return
-                }
-
                 match (&from_ty.kind, &to_ty.kind) {
                     _ if from_ty == to_ty => span_lint(
                         cx,
@@ -646,6 +628,22 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
                             );
                         }
                     },
+                    (_, _) if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) => span_lint_and_then(
+                        cx,
+                        TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
+                        e.span,
+                        &format!(
+                            "transmute from `{}` to `{}` which could be expressed as a pointer cast instead",
+                            from_ty,
+                            to_ty
+                        ),
+                        |diag| {
+                            if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
+                                let sugg = arg.as_ty(&to_ty.to_string()).to_string();
+                                diag.span_suggestion(e.span, "try", sugg, Applicability::Unspecified);
+                            }
+                        }
+                    ),
                     _ => {
                         return;
                     },
diff --git a/tests/ui/transmute.rs b/tests/ui/transmute.rs
index b3171d2e7dc..bb853d23704 100644
--- a/tests/ui/transmute.rs
+++ b/tests/ui/transmute.rs
@@ -1,7 +1,7 @@
 #![allow(dead_code)]
-#![allow(clippy::transmutes_expressible_as_ptr_casts)]
 
 extern crate core;
+
 use std::mem::transmute as my_transmute;
 use std::vec::Vec as MyVec;
 
diff --git a/tests/ui/transmute_ptr_to_ptr.rs b/tests/ui/transmute_ptr_to_ptr.rs
index 009b5fa534c..0d8a322f2b2 100644
--- a/tests/ui/transmute_ptr_to_ptr.rs
+++ b/tests/ui/transmute_ptr_to_ptr.rs
@@ -1,5 +1,5 @@
 #![warn(clippy::transmute_ptr_to_ptr)]
-#![allow(clippy::transmutes_expressible_as_ptr_casts)]
+
 // Make sure we can modify lifetimes, which is one of the recommended uses
 // of transmute
 
diff --git a/tests/ui/transmutes_expressible_as_ptr_casts.rs b/tests/ui/transmutes_expressible_as_ptr_casts.rs
index e6b9dddd342..db544b438a2 100644
--- a/tests/ui/transmutes_expressible_as_ptr_casts.rs
+++ b/tests/ui/transmutes_expressible_as_ptr_casts.rs
@@ -1,10 +1,18 @@
 #![warn(clippy::transmutes_expressible_as_ptr_casts)]
+// These two warnings currrently cover the cases transmutes_expressible_as_ptr_casts
+// would otherwise be responsible for
+#![warn(clippy::useless_transmute)]
+#![warn(clippy::transmute_ptr_to_ptr)]
+
+use std::mem::transmute;
 
 // rustc_typeck::check::cast contains documentation about when a cast `e as U` is 
 // valid, which we quote from below.
-use std::mem::transmute;
 
 fn main() {
+    // We should see an error message for each transmute, and no error messages for
+    // the casts, since the casts are the recommended fixes.
+
     // e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
     let ptr_i32_transmute = unsafe {
         transmute::<isize, *const i32>(-1)
diff --git a/tests/ui/transmutes_expressible_as_ptr_casts.stderr b/tests/ui/transmutes_expressible_as_ptr_casts.stderr
index 6bae1fa1b4f..7cd316bf38a 100644
--- a/tests/ui/transmutes_expressible_as_ptr_casts.stderr
+++ b/tests/ui/transmutes_expressible_as_ptr_casts.stderr
@@ -1 +1,50 @@
-Should have 7 errors, one for each transmute
+error: transmute from an integer to a pointer
+  --> $DIR/transmutes_expressible_as_ptr_casts.rs:18:9
+   |
+LL |         transmute::<isize, *const i32>(-1)
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-1 as *const i32`
+   |
+   = note: `-D clippy::useless-transmute` implied by `-D warnings`
+
+error: transmute from a pointer to a pointer
+  --> $DIR/transmutes_expressible_as_ptr_casts.rs:24:9
+   |
+LL |         transmute::<*const i32, *const i8>(ptr_i32)
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as *const i8`
+   |
+   = note: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings`
+
+error: transmute from a pointer to a pointer
+  --> $DIR/transmutes_expressible_as_ptr_casts.rs:32:9
+   |
+LL |         transmute::<*const [i32], *const [u16]>(slice_ptr)
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `slice_ptr as *const [u16]`
+
+error: transmute from `*const i32` to `usize` which could be expressed as a pointer cast instead
+  --> $DIR/transmutes_expressible_as_ptr_casts.rs:40:9
+   |
+LL |         transmute::<*const i32, usize>(ptr_i32)
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as usize`
+   |
+   = note: `-D clippy::transmutes-expressible-as-ptr-casts` implied by `-D warnings`
+
+error: transmute from a reference to a pointer
+  --> $DIR/transmutes_expressible_as_ptr_casts.rs:48:9
+   |
+LL |         transmute::<&[i32; 4], *const [i32; 4]>(array_ref)
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `array_ref as *const [i32; 4]`
+
+error: transmute from `fn(usize) -> u8 {main::foo}` to `*const usize` which could be expressed as a pointer cast instead
+  --> $DIR/transmutes_expressible_as_ptr_casts.rs:56:9
+   |
+LL |         transmute::<fn(usize) -> u8, *const usize>(foo)
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as *const usize`
+
+error: transmute from `fn(usize) -> u8 {main::foo}` to `usize` which could be expressed as a pointer cast instead
+  --> $DIR/transmutes_expressible_as_ptr_casts.rs:62:9
+   |
+LL |         transmute::<fn(usize) -> u8, usize>(foo)
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize`
+
+error: aborting due to 7 previous errors
+