rust/clippy_lints/src/transmute/wrong_transmute.rs

23 lines
729 B
Rust
Raw Normal View History

2021-02-10 23:57:56 +09:00
use super::WRONG_TRANSMUTE;
use clippy_utils::diagnostics::span_lint;
2021-02-10 23:57:56 +09:00
use rustc_hir::Expr;
use rustc_lint::LateContext;
2021-02-11 01:29:45 +09:00
use rustc_middle::ty::{self, Ty};
2021-02-10 23:57:56 +09:00
/// 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 `{from_ty}` to a pointer"),
2021-02-10 23:57:56 +09:00
);
true
},
_ => false,
}
}