about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/transmute/crosspointer_transmute.rs38
-rw-r--r--clippy_lints/src/transmute/mod.rs23
2 files changed, 43 insertions, 18 deletions
diff --git a/clippy_lints/src/transmute/crosspointer_transmute.rs b/clippy_lints/src/transmute/crosspointer_transmute.rs
new file mode 100644
index 00000000000..b570bb3a396
--- /dev/null
+++ b/clippy_lints/src/transmute/crosspointer_transmute.rs
@@ -0,0 +1,38 @@
+use super::CROSSPOINTER_TRANSMUTE;
+use crate::utils::span_lint;
+use rustc_hir::Expr;
+use rustc_lint::LateContext;
+use rustc_middle::ty;
+use rustc_middle::ty::Ty;
+
+/// Checks for `crosspointer_transmute` lint.
+/// Returns `true` if it's triggered, otherwise returns `false`.
+pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
+    match (&from_ty.kind(), &to_ty.kind()) {
+        (ty::RawPtr(from_ptr), _) if from_ptr.ty == to_ty => {
+            span_lint(
+                cx,
+                CROSSPOINTER_TRANSMUTE,
+                e.span,
+                &format!(
+                    "transmute from a type (`{}`) to the type that it points to (`{}`)",
+                    from_ty, to_ty
+                ),
+            );
+            true
+        },
+        (_, ty::RawPtr(to_ptr)) if to_ptr.ty == from_ty => {
+            span_lint(
+                cx,
+                CROSSPOINTER_TRANSMUTE,
+                e.span,
+                &format!(
+                    "transmute from a type (`{}`) to a pointer to that type (`{}`)",
+                    from_ty, to_ty
+                ),
+            );
+            true
+        },
+        _ => false,
+    }
+}
diff --git a/clippy_lints/src/transmute/mod.rs b/clippy_lints/src/transmute/mod.rs
index 09e0fe257a5..7e05771c885 100644
--- a/clippy_lints/src/transmute/mod.rs
+++ b/clippy_lints/src/transmute/mod.rs
@@ -1,3 +1,4 @@
+mod crosspointer_transmute;
 mod useless_transmute;
 mod utils;
 mod wrong_transmute;
@@ -355,26 +356,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
                 if triggered {
                     return;
                 }
+                let triggered = crosspointer_transmute::check(cx, e, from_ty, to_ty);
+                if triggered {
+                    return;
+                }
 
                 match (&from_ty.kind(), &to_ty.kind()) {
-                    (ty::RawPtr(from_ptr), _) if from_ptr.ty == to_ty => span_lint(
-                        cx,
-                        CROSSPOINTER_TRANSMUTE,
-                        e.span,
-                        &format!(
-                            "transmute from a type (`{}`) to the type that it points to (`{}`)",
-                            from_ty, to_ty
-                        ),
-                    ),
-                    (_, ty::RawPtr(to_ptr)) if to_ptr.ty == from_ty => span_lint(
-                        cx,
-                        CROSSPOINTER_TRANSMUTE,
-                        e.span,
-                        &format!(
-                            "transmute from a type (`{}`) to a pointer to that type (`{}`)",
-                            from_ty, to_ty
-                        ),
-                    ),
                     (ty::RawPtr(from_pty), ty::Ref(_, to_ref_ty, mutbl)) => span_lint_and_then(
                         cx,
                         TRANSMUTE_PTR_TO_REF,