Skip to content

Commit 25c221e

Browse files
magurotunaflip1995
authored andcommitted
Move transmutes_expressible_as_ptr_casts to its own module
1 parent c57a826 commit 25c221e

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

clippy_lints/src/transmute/mod.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod transmute_int_to_float;
66
mod transmute_ptr_to_ptr;
77
mod transmute_ptr_to_ref;
88
mod transmute_ref_to_ref;
9+
mod transmutes_expressible_as_ptr_casts;
910
mod unsound_collection_transmute;
1011
mod useless_transmute;
1112
mod utils;
@@ -389,27 +390,9 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
389390
if triggered {
390391
return;
391392
}
392-
393-
match (&from_ty.kind(), &to_ty.kind()) {
394-
(_, _) if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) => span_lint_and_then(
395-
cx,
396-
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
397-
e.span,
398-
&format!(
399-
"transmute from `{}` to `{}` which could be expressed as a pointer cast instead",
400-
from_ty,
401-
to_ty
402-
),
403-
|diag| {
404-
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
405-
let sugg = arg.as_ty(&to_ty.to_string()).to_string();
406-
diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable);
407-
}
408-
}
409-
),
410-
_ => {
411-
return;
412-
},
393+
let triggered = transmutes_expressible_as_ptr_casts::check(cx, e, from_ty, to_ty, args);
394+
if triggered {
395+
return;
413396
}
414397
}
415398
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use super::utils::can_be_expressed_as_pointer_cast;
2+
use super::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS;
3+
use crate::utils::{span_lint_and_then, sugg};
4+
use rustc_errors::Applicability;
5+
use rustc_hir::Expr;
6+
use rustc_lint::LateContext;
7+
use rustc_middle::ty::Ty;
8+
9+
/// Checks for `transmutes_expressible_as_ptr_casts` 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+
if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) {
19+
span_lint_and_then(
20+
cx,
21+
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
22+
e.span,
23+
&format!(
24+
"transmute from `{}` to `{}` which could be expressed as a pointer cast instead",
25+
from_ty, to_ty
26+
),
27+
|diag| {
28+
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
29+
let sugg = arg.as_ty(&to_ty.to_string()).to_string();
30+
diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable);
31+
}
32+
},
33+
);
34+
true
35+
} else {
36+
false
37+
}
38+
}

0 commit comments

Comments
 (0)