]> git.proxmox.com Git - rustc.git/blobdiff - src/tools/clippy/clippy_lints/src/transmute/wrong_transmute.rs
New upstream version 1.52.1+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / transmute / wrong_transmute.rs
diff --git a/src/tools/clippy/clippy_lints/src/transmute/wrong_transmute.rs b/src/tools/clippy/clippy_lints/src/transmute/wrong_transmute.rs
new file mode 100644 (file)
index 0000000..d6d77f2
--- /dev/null
@@ -0,0 +1,22 @@
+use super::WRONG_TRANSMUTE;
+use crate::utils::span_lint;
+use rustc_hir::Expr;
+use rustc_lint::LateContext;
+use rustc_middle::ty::{self, Ty};
+
+/// Checks for `wrong_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::Float(_) | ty::Char, ty::Ref(..) | ty::RawPtr(_)) => {
+            span_lint(
+                cx,
+                WRONG_TRANSMUTE,
+                e.span,
+                &format!("transmute from a `{}` to a pointer", from_ty),
+            );
+            true
+        },
+        _ => false,
+    }
+}