Skip to content

Commit f8bc0e2

Browse files
magurotunaflip1995
authored andcommitted
Move transmute_ptr_to_ptr to its own module
1 parent 7af3458 commit f8bc0e2

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

clippy_lints/src/transmute/mod.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod crosspointer_transmute;
22
mod transmute_int_to_char;
3+
mod transmute_ptr_to_ptr;
34
mod transmute_ptr_to_ref;
45
mod transmute_ref_to_ref;
56
mod useless_transmute;
@@ -375,20 +376,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
375376
if triggered {
376377
return;
377378
}
379+
let triggered = transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, args);
380+
if triggered {
381+
return;
382+
}
378383

379384
match (&from_ty.kind(), &to_ty.kind()) {
380-
(ty::RawPtr(_), ty::RawPtr(to_ty)) => span_lint_and_then(
381-
cx,
382-
TRANSMUTE_PTR_TO_PTR,
383-
e.span,
384-
"transmute from a pointer to a pointer",
385-
|diag| {
386-
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
387-
let sugg = arg.as_ty(cx.tcx.mk_ptr(*to_ty));
388-
diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
389-
}
390-
},
391-
),
392385
(ty::Int(ty::IntTy::I8) | ty::Uint(ty::UintTy::U8), ty::Bool) => {
393386
span_lint_and_then(
394387
cx,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use super::TRANSMUTE_PTR_TO_PTR;
2+
use crate::utils::{span_lint_and_then, sugg};
3+
use rustc_errors::Applicability;
4+
use rustc_hir::Expr;
5+
use rustc_lint::LateContext;
6+
use rustc_middle::ty;
7+
use rustc_middle::ty::Ty;
8+
9+
/// Checks for `transmute_ptr_to_ptr` lint.
10+
/// Returns `true` if it's triggered, otherwise returns `false`.
11+
pub(super) fn check<'tcx>(
12+
cx: &LateContext<'tcx>,
13+
e: &'tcx Expr<'_>,
14+
from_ty: Ty<'tcx>,
15+
to_ty: Ty<'tcx>,
16+
args: &'tcx [Expr<'_>],
17+
) -> bool {
18+
match (&from_ty.kind(), &to_ty.kind()) {
19+
(ty::RawPtr(_), ty::RawPtr(to_ty)) => {
20+
span_lint_and_then(
21+
cx,
22+
TRANSMUTE_PTR_TO_PTR,
23+
e.span,
24+
"transmute from a pointer to a pointer",
25+
|diag| {
26+
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
27+
let sugg = arg.as_ty(cx.tcx.mk_ptr(*to_ty));
28+
diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
29+
}
30+
},
31+
);
32+
true
33+
},
34+
_ => false,
35+
}
36+
}

0 commit comments

Comments
 (0)