Skip to content

Commit

Permalink
Move wrong_transmute to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
magurotuna authored and flip1995 committed Mar 2, 2021
1 parent 0d7dd00 commit ef97764
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
11 changes: 5 additions & 6 deletions clippy_lints/src/transmute/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod useless_transmute;
mod utils;
mod wrong_transmute;

use utils::*;

Expand Down Expand Up @@ -350,14 +351,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
if triggered {
return;
}
let triggered = wrong_transmute::check(cx, e, from_ty, to_ty);
if triggered {
return;
}

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),
),
(ty::RawPtr(from_ptr), _) if from_ptr.ty == to_ty => span_lint(
cx,
CROSSPOINTER_TRANSMUTE,
Expand Down
23 changes: 23 additions & 0 deletions clippy_lints/src/transmute/wrong_transmute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::WRONG_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 `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,
}
}

0 comments on commit ef97764

Please sign in to comment.