Skip to content

Commit afc9275

Browse files
magurotunaflip1995
authored andcommitted
Move crosspointer_transmute to its own module
1 parent ef97764 commit afc9275

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use super::CROSSPOINTER_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 `crosspointer_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::RawPtr(from_ptr), _) if from_ptr.ty == to_ty => {
13+
span_lint(
14+
cx,
15+
CROSSPOINTER_TRANSMUTE,
16+
e.span,
17+
&format!(
18+
"transmute from a type (`{}`) to the type that it points to (`{}`)",
19+
from_ty, to_ty
20+
),
21+
);
22+
true
23+
},
24+
(_, ty::RawPtr(to_ptr)) if to_ptr.ty == from_ty => {
25+
span_lint(
26+
cx,
27+
CROSSPOINTER_TRANSMUTE,
28+
e.span,
29+
&format!(
30+
"transmute from a type (`{}`) to a pointer to that type (`{}`)",
31+
from_ty, to_ty
32+
),
33+
);
34+
true
35+
},
36+
_ => false,
37+
}
38+
}

clippy_lints/src/transmute/mod.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod crosspointer_transmute;
12
mod useless_transmute;
23
mod utils;
34
mod wrong_transmute;
@@ -355,26 +356,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
355356
if triggered {
356357
return;
357358
}
359+
let triggered = crosspointer_transmute::check(cx, e, from_ty, to_ty);
360+
if triggered {
361+
return;
362+
}
358363

359364
match (&from_ty.kind(), &to_ty.kind()) {
360-
(ty::RawPtr(from_ptr), _) if from_ptr.ty == to_ty => span_lint(
361-
cx,
362-
CROSSPOINTER_TRANSMUTE,
363-
e.span,
364-
&format!(
365-
"transmute from a type (`{}`) to the type that it points to (`{}`)",
366-
from_ty, to_ty
367-
),
368-
),
369-
(_, ty::RawPtr(to_ptr)) if to_ptr.ty == from_ty => span_lint(
370-
cx,
371-
CROSSPOINTER_TRANSMUTE,
372-
e.span,
373-
&format!(
374-
"transmute from a type (`{}`) to a pointer to that type (`{}`)",
375-
from_ty, to_ty
376-
),
377-
),
378365
(ty::RawPtr(from_pty), ty::Ref(_, to_ref_ty, mutbl)) => span_lint_and_then(
379366
cx,
380367
TRANSMUTE_PTR_TO_REF,

0 commit comments

Comments
 (0)