Skip to content

Commit ef97764

Browse files
magurotunaflip1995
authored andcommitted
Move wrong_transmute to its own module
1 parent 0d7dd00 commit ef97764

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

clippy_lints/src/transmute/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod useless_transmute;
22
mod utils;
3+
mod wrong_transmute;
34

45
use utils::*;
56

@@ -350,14 +351,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
350351
if triggered {
351352
return;
352353
}
354+
let triggered = wrong_transmute::check(cx, e, from_ty, to_ty);
355+
if triggered {
356+
return;
357+
}
353358

354359
match (&from_ty.kind(), &to_ty.kind()) {
355-
(ty::Float(_) | ty::Char, ty::Ref(..) | ty::RawPtr(_)) => span_lint(
356-
cx,
357-
WRONG_TRANSMUTE,
358-
e.span,
359-
&format!("transmute from a `{}` to a pointer", from_ty),
360-
),
361360
(ty::RawPtr(from_ptr), _) if from_ptr.ty == to_ty => span_lint(
362361
cx,
363362
CROSSPOINTER_TRANSMUTE,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use super::WRONG_TRANSMUTE;
2+
use crate::utils::span_lint;
3+
use rustc_hir::Expr;
4+
use rustc_lint::LateContext;
5+
use rustc_middle::ty;
6+
use rustc_middle::ty::Ty;
7+
8+
/// Checks for `wrong_transmute` lint.
9+
/// Returns `true` if it's triggered, otherwise returns `false`.
10+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
11+
match (&from_ty.kind(), &to_ty.kind()) {
12+
(ty::Float(_) | ty::Char, ty::Ref(..) | ty::RawPtr(_)) => {
13+
span_lint(
14+
cx,
15+
WRONG_TRANSMUTE,
16+
e.span,
17+
&format!("transmute from a `{}` to a pointer", from_ty),
18+
);
19+
true
20+
},
21+
_ => false,
22+
}
23+
}

0 commit comments

Comments
 (0)