Skip to content

Commit 6442d45

Browse files
magurotunaflip1995
authored andcommitted
Move transmute_ptr_to_ref to its own module
1 parent afc9275 commit 6442d45

File tree

2 files changed

+61
-31
lines changed

2 files changed

+61
-31
lines changed

clippy_lints/src/transmute/mod.rs

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod crosspointer_transmute;
2+
mod transmute_ptr_to_ref;
23
mod useless_transmute;
34
mod utils;
45
mod wrong_transmute;
@@ -360,39 +361,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
360361
if triggered {
361362
return;
362363
}
364+
let triggered = transmute_ptr_to_ref::check(cx, e, from_ty, to_ty, args, qpath);
365+
if triggered {
366+
return;
367+
}
363368

364369
match (&from_ty.kind(), &to_ty.kind()) {
365-
(ty::RawPtr(from_pty), ty::Ref(_, to_ref_ty, mutbl)) => span_lint_and_then(
366-
cx,
367-
TRANSMUTE_PTR_TO_REF,
368-
e.span,
369-
&format!(
370-
"transmute from a pointer type (`{}`) to a reference type \
371-
(`{}`)",
372-
from_ty, to_ty
373-
),
374-
|diag| {
375-
let arg = sugg::Sugg::hir(cx, &args[0], "..");
376-
let (deref, cast) = if *mutbl == Mutability::Mut {
377-
("&mut *", "*mut")
378-
} else {
379-
("&*", "*const")
380-
};
381-
382-
let arg = if from_pty.ty == *to_ref_ty {
383-
arg
384-
} else {
385-
arg.as_ty(&format!("{} {}", cast, get_type_snippet(cx, qpath, to_ref_ty)))
386-
};
387-
388-
diag.span_suggestion(
389-
e.span,
390-
"try",
391-
sugg::make_unop(deref, arg).to_string(),
392-
Applicability::Unspecified,
393-
);
394-
},
395-
),
396370
(ty::Int(ty::IntTy::I32) | ty::Uint(ty::UintTy::U32), &ty::Char) => {
397371
span_lint_and_then(
398372
cx,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use super::utils::get_type_snippet;
2+
use super::TRANSMUTE_PTR_TO_REF;
3+
use crate::utils::{span_lint_and_then, sugg};
4+
use rustc_errors::Applicability;
5+
use rustc_hir::{Expr, Mutability, QPath};
6+
use rustc_lint::LateContext;
7+
use rustc_middle::ty;
8+
use rustc_middle::ty::Ty;
9+
10+
/// Checks for `transmute_ptr_to_ref` lint.
11+
/// Returns `true` if it's triggered, otherwise returns `false`.
12+
pub(super) fn check<'tcx>(
13+
cx: &LateContext<'tcx>,
14+
e: &'tcx Expr<'_>,
15+
from_ty: Ty<'tcx>,
16+
to_ty: Ty<'tcx>,
17+
args: &'tcx [Expr<'_>],
18+
qpath: &'tcx QPath<'_>,
19+
) -> bool {
20+
match (&from_ty.kind(), &to_ty.kind()) {
21+
(ty::RawPtr(from_pty), ty::Ref(_, to_ref_ty, mutbl)) => {
22+
span_lint_and_then(
23+
cx,
24+
TRANSMUTE_PTR_TO_REF,
25+
e.span,
26+
&format!(
27+
"transmute from a pointer type (`{}`) to a reference type (`{}`)",
28+
from_ty, to_ty
29+
),
30+
|diag| {
31+
let arg = sugg::Sugg::hir(cx, &args[0], "..");
32+
let (deref, cast) = if *mutbl == Mutability::Mut {
33+
("&mut *", "*mut")
34+
} else {
35+
("&*", "*const")
36+
};
37+
38+
let arg = if from_pty.ty == *to_ref_ty {
39+
arg
40+
} else {
41+
arg.as_ty(&format!("{} {}", cast, get_type_snippet(cx, qpath, to_ref_ty)))
42+
};
43+
44+
diag.span_suggestion(
45+
e.span,
46+
"try",
47+
sugg::make_unop(deref, arg).to_string(),
48+
Applicability::Unspecified,
49+
);
50+
},
51+
);
52+
true
53+
},
54+
_ => false,
55+
}
56+
}

0 commit comments

Comments
 (0)