Skip to content

Commit 7af3458

Browse files
magurotunaflip1995
authored andcommitted
Move transmute_bytes_to_str and transmute_ptr_to_ptr to transmute_ref_to_ref module
1 parent 4c65221 commit 7af3458

File tree

2 files changed

+94
-59
lines changed

2 files changed

+94
-59
lines changed

clippy_lints/src/transmute/mod.rs

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod crosspointer_transmute;
22
mod transmute_int_to_char;
33
mod transmute_ptr_to_ref;
4+
mod transmute_ref_to_ref;
45
mod useless_transmute;
56
mod utils;
67
mod wrong_transmute;
@@ -370,67 +371,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
370371
if triggered {
371372
return;
372373
}
374+
let triggered = transmute_ref_to_ref::check(cx, e, from_ty, to_ty, args, const_context);
375+
if triggered {
376+
return;
377+
}
373378

374379
match (&from_ty.kind(), &to_ty.kind()) {
375-
(ty::Ref(_, ty_from, from_mutbl), ty::Ref(_, ty_to, to_mutbl)) => {
376-
if_chain! {
377-
if let (&ty::Slice(slice_ty), &ty::Str) = (&ty_from.kind(), &ty_to.kind());
378-
if let ty::Uint(ty::UintTy::U8) = slice_ty.kind();
379-
if from_mutbl == to_mutbl;
380-
then {
381-
let postfix = if *from_mutbl == Mutability::Mut {
382-
"_mut"
383-
} else {
384-
""
385-
};
386-
387-
span_lint_and_sugg(
388-
cx,
389-
TRANSMUTE_BYTES_TO_STR,
390-
e.span,
391-
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
392-
"consider using",
393-
format!(
394-
"std::str::from_utf8{}({}).unwrap()",
395-
postfix,
396-
snippet(cx, args[0].span, ".."),
397-
),
398-
Applicability::Unspecified,
399-
);
400-
} else {
401-
if (cx.tcx.erase_regions(from_ty) != cx.tcx.erase_regions(to_ty))
402-
&& !const_context {
403-
span_lint_and_then(
404-
cx,
405-
TRANSMUTE_PTR_TO_PTR,
406-
e.span,
407-
"transmute from a reference to a reference",
408-
|diag| if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
409-
let ty_from_and_mut = ty::TypeAndMut {
410-
ty: ty_from,
411-
mutbl: *from_mutbl
412-
};
413-
let ty_to_and_mut = ty::TypeAndMut { ty: ty_to, mutbl: *to_mutbl };
414-
let sugg_paren = arg
415-
.as_ty(cx.tcx.mk_ptr(ty_from_and_mut))
416-
.as_ty(cx.tcx.mk_ptr(ty_to_and_mut));
417-
let sugg = if *to_mutbl == Mutability::Mut {
418-
sugg_paren.mut_addr_deref()
419-
} else {
420-
sugg_paren.addr_deref()
421-
};
422-
diag.span_suggestion(
423-
e.span,
424-
"try",
425-
sugg.to_string(),
426-
Applicability::Unspecified,
427-
);
428-
},
429-
)
430-
}
431-
}
432-
}
433-
},
434380
(ty::RawPtr(_), ty::RawPtr(to_ty)) => span_lint_and_then(
435381
cx,
436382
TRANSMUTE_PTR_TO_PTR,
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use super::{TRANSMUTE_BYTES_TO_STR, TRANSMUTE_PTR_TO_PTR};
2+
use crate::utils::{snippet, span_lint_and_sugg, span_lint_and_then, sugg};
3+
use if_chain::if_chain;
4+
use rustc_errors::Applicability;
5+
use rustc_hir::{Expr, Mutability};
6+
use rustc_lint::LateContext;
7+
use rustc_middle::ty;
8+
use rustc_middle::ty::Ty;
9+
10+
/// Checks for `transmute_bytes_to_str` and `transmute_ptr_to_ptr` lints.
11+
/// Returns `true` if either one 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+
const_context: bool,
19+
) -> bool {
20+
let mut triggered = false;
21+
22+
match (&from_ty.kind(), &to_ty.kind()) {
23+
(ty::Ref(_, ty_from, from_mutbl), ty::Ref(_, ty_to, to_mutbl)) => {
24+
if_chain! {
25+
if let (&ty::Slice(slice_ty), &ty::Str) = (&ty_from.kind(), &ty_to.kind());
26+
if let ty::Uint(ty::UintTy::U8) = slice_ty.kind();
27+
if from_mutbl == to_mutbl;
28+
then {
29+
let postfix = if *from_mutbl == Mutability::Mut {
30+
"_mut"
31+
} else {
32+
""
33+
};
34+
35+
span_lint_and_sugg(
36+
cx,
37+
TRANSMUTE_BYTES_TO_STR,
38+
e.span,
39+
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
40+
"consider using",
41+
format!(
42+
"std::str::from_utf8{}({}).unwrap()",
43+
postfix,
44+
snippet(cx, args[0].span, ".."),
45+
),
46+
Applicability::Unspecified,
47+
);
48+
triggered = true;
49+
} else {
50+
if (cx.tcx.erase_regions(from_ty) != cx.tcx.erase_regions(to_ty))
51+
&& !const_context {
52+
span_lint_and_then(
53+
cx,
54+
TRANSMUTE_PTR_TO_PTR,
55+
e.span,
56+
"transmute from a reference to a reference",
57+
|diag| if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
58+
let ty_from_and_mut = ty::TypeAndMut {
59+
ty: ty_from,
60+
mutbl: *from_mutbl
61+
};
62+
let ty_to_and_mut = ty::TypeAndMut { ty: ty_to, mutbl: *to_mutbl };
63+
let sugg_paren = arg
64+
.as_ty(cx.tcx.mk_ptr(ty_from_and_mut))
65+
.as_ty(cx.tcx.mk_ptr(ty_to_and_mut));
66+
let sugg = if *to_mutbl == Mutability::Mut {
67+
sugg_paren.mut_addr_deref()
68+
} else {
69+
sugg_paren.addr_deref()
70+
};
71+
diag.span_suggestion(
72+
e.span,
73+
"try",
74+
sugg.to_string(),
75+
Applicability::Unspecified,
76+
);
77+
},
78+
);
79+
80+
triggered = true;
81+
}
82+
}
83+
}
84+
},
85+
_ => {},
86+
}
87+
88+
triggered
89+
}

0 commit comments

Comments
 (0)