about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYusuke Tanaka <yusuktan@maguro.dev>2021-02-10 23:54:11 +0900
committerflip1995 <philipp.krones@embecosm.com>2021-03-02 10:37:30 +0100
commit0d7dd00860e5a2b8b7c7a34daaa5553ff2e542f3 (patch)
tree6d2df1c5329276fc4ae72f57966dcf943c9141bc
parent4ac438bfed36c1de307cac0f86d8d4938157ac50 (diff)
downloadrust-0d7dd00860e5a2b8b7c7a34daaa5553ff2e542f3.tar.gz
rust-0d7dd00860e5a2b8b7c7a34daaa5553ff2e542f3.zip
Move useless_transmute to its own module
-rw-r--r--clippy_lints/src/transmute/mod.rs51
-rw-r--r--clippy_lints/src/transmute/useless_transmute.rs74
2 files changed, 81 insertions, 44 deletions
diff --git a/clippy_lints/src/transmute/mod.rs b/clippy_lints/src/transmute/mod.rs
index 0258244a07b..4897934376f 100644
--- a/clippy_lints/src/transmute/mod.rs
+++ b/clippy_lints/src/transmute/mod.rs
@@ -1,4 +1,6 @@
+mod useless_transmute;
 mod utils;
+
 use utils::*;
 
 use crate::utils::{
@@ -344,51 +346,12 @@ 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);
 
-                match (&from_ty.kind(), &to_ty.kind()) {
-                    _ if from_ty == to_ty => span_lint(
-                        cx,
-                        USELESS_TRANSMUTE,
-                        e.span,
-                        &format!("transmute from a type (`{}`) to itself", from_ty),
-                    ),
-                    (ty::Ref(_, rty, rty_mutbl), ty::RawPtr(ptr_ty)) => span_lint_and_then(
-                        cx,
-                        USELESS_TRANSMUTE,
-                        e.span,
-                        "transmute from a reference to a pointer",
-                        |diag| {
-                            if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
-                                let rty_and_mut = ty::TypeAndMut {
-                                    ty: rty,
-                                    mutbl: *rty_mutbl,
-                                };
-
-                                let sugg = if *ptr_ty == rty_and_mut {
-                                    arg.as_ty(to_ty)
-                                } else {
-                                    arg.as_ty(cx.tcx.mk_ptr(rty_and_mut)).as_ty(to_ty)
-                                };
+                let triggered = useless_transmute::check(cx, e, from_ty, to_ty, args);
+                if triggered {
+                    return;
+                }
 
-                                diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
-                            }
-                        },
-                    ),
-                    (ty::Int(_) | ty::Uint(_), ty::RawPtr(_)) => span_lint_and_then(
-                        cx,
-                        USELESS_TRANSMUTE,
-                        e.span,
-                        "transmute from an integer to a pointer",
-                        |diag| {
-                            if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
-                                diag.span_suggestion(
-                                    e.span,
-                                    "try",
-                                    arg.as_ty(&to_ty.to_string()).to_string(),
-                                    Applicability::Unspecified,
-                                );
-                            }
-                        },
-                    ),
+                match (&from_ty.kind(), &to_ty.kind()) {
                     (ty::Float(_) | ty::Char, ty::Ref(..) | ty::RawPtr(_)) => span_lint(
                         cx,
                         WRONG_TRANSMUTE,
diff --git a/clippy_lints/src/transmute/useless_transmute.rs b/clippy_lints/src/transmute/useless_transmute.rs
new file mode 100644
index 00000000000..86d75fd2ee0
--- /dev/null
+++ b/clippy_lints/src/transmute/useless_transmute.rs
@@ -0,0 +1,74 @@
+use super::USELESS_TRANSMUTE;
+use crate::utils::{span_lint, span_lint_and_then, sugg};
+use rustc_errors::Applicability;
+use rustc_hir::Expr;
+use rustc_lint::LateContext;
+use rustc_middle::ty;
+use rustc_middle::ty::Ty;
+
+/// Checks for `useless_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>,
+    args: &'tcx [Expr<'_>],
+) -> bool {
+    match (&from_ty.kind(), &to_ty.kind()) {
+        _ if from_ty == to_ty => {
+            span_lint(
+                cx,
+                USELESS_TRANSMUTE,
+                e.span,
+                &format!("transmute from a type (`{}`) to itself", from_ty),
+            );
+            true
+        },
+        (ty::Ref(_, rty, rty_mutbl), ty::RawPtr(ptr_ty)) => {
+            span_lint_and_then(
+                cx,
+                USELESS_TRANSMUTE,
+                e.span,
+                "transmute from a reference to a pointer",
+                |diag| {
+                    if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
+                        let rty_and_mut = ty::TypeAndMut {
+                            ty: rty,
+                            mutbl: *rty_mutbl,
+                        };
+
+                        let sugg = if *ptr_ty == rty_and_mut {
+                            arg.as_ty(to_ty)
+                        } else {
+                            arg.as_ty(cx.tcx.mk_ptr(rty_and_mut)).as_ty(to_ty)
+                        };
+
+                        diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
+                    }
+                },
+            );
+            true
+        },
+        (ty::Int(_) | ty::Uint(_), ty::RawPtr(_)) => {
+            span_lint_and_then(
+                cx,
+                USELESS_TRANSMUTE,
+                e.span,
+                "transmute from an integer to a pointer",
+                |diag| {
+                    if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
+                        diag.span_suggestion(
+                            e.span,
+                            "try",
+                            arg.as_ty(&to_ty.to_string()).to_string(),
+                            Applicability::Unspecified,
+                        );
+                    }
+                },
+            );
+            true
+        },
+        _ => false,
+    }
+}