Skip to content

Commit 7000e75

Browse files
committed
Auto merge of #8564 - Jarcho:transmute_erase_regions, r=Alexendoo
Don't lint `useless_transmute` on types with erased regions fixes #6356 fixes #3340 fixes #2906 This should get a proper fix at some point, but this at least gets the lint running on some types. cc #5343 changelog: Don't lint `useless_transmute` on types with erased regions
2 parents 9add456 + 0c6ebf1 commit 7000e75

8 files changed

+88
-73
lines changed

clippy_lints/src/lib.register_all.rs

+1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
305305
LintId::of(transmute::TRANSMUTE_NUM_TO_BYTES),
306306
LintId::of(transmute::TRANSMUTE_PTR_TO_REF),
307307
LintId::of(transmute::UNSOUND_COLLECTION_TRANSMUTE),
308+
LintId::of(transmute::USELESS_TRANSMUTE),
308309
LintId::of(transmute::WRONG_TRANSMUTE),
309310
LintId::of(transmuting_null::TRANSMUTING_NULL),
310311
LintId::of(types::BORROWED_BOX),

clippy_lints/src/lib.register_complexity.rs

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
9090
LintId::of(transmute::TRANSMUTE_INT_TO_FLOAT),
9191
LintId::of(transmute::TRANSMUTE_NUM_TO_BYTES),
9292
LintId::of(transmute::TRANSMUTE_PTR_TO_REF),
93+
LintId::of(transmute::USELESS_TRANSMUTE),
9394
LintId::of(types::BORROWED_BOX),
9495
LintId::of(types::TYPE_COMPLEXITY),
9596
LintId::of(types::VEC_BOX),

clippy_lints/src/lib.register_nursery.rs

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
3131
LintId::of(trait_bounds::TRAIT_DUPLICATION_IN_BOUNDS),
3232
LintId::of(trait_bounds::TYPE_REPETITION_IN_BOUNDS),
3333
LintId::of(transmute::TRANSMUTE_UNDEFINED_REPR),
34-
LintId::of(transmute::USELESS_TRANSMUTE),
3534
LintId::of(unused_rounding::UNUSED_ROUNDING),
3635
LintId::of(use_self::USE_SELF),
3736
])

clippy_lints/src/transmute/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ declare_clippy_lint! {
5959
/// ```
6060
#[clippy::version = "pre 1.29.0"]
6161
pub USELESS_TRANSMUTE,
62-
nursery,
62+
complexity,
6363
"transmutes that have the same to and from types or could be a cast/coercion"
6464
}
6565

clippy_lints/src/transmute/useless_transmute.rs

+25-22
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::sugg;
44
use rustc_errors::Applicability;
55
use rustc_hir::Expr;
66
use rustc_lint::LateContext;
7-
use rustc_middle::ty::{self, Ty};
7+
use rustc_middle::ty::{self, Ty, TypeFoldable};
88

99
/// Checks for `useless_transmute` lint.
1010
/// Returns `true` if it's triggered, otherwise returns `false`.
@@ -16,7 +16,7 @@ pub(super) fn check<'tcx>(
1616
arg: &'tcx Expr<'_>,
1717
) -> bool {
1818
match (&from_ty.kind(), &to_ty.kind()) {
19-
_ if from_ty == to_ty => {
19+
_ if from_ty == to_ty && !from_ty.has_erased_regions() => {
2020
span_lint(
2121
cx,
2222
USELESS_TRANSMUTE,
@@ -26,28 +26,31 @@ pub(super) fn check<'tcx>(
2626
true
2727
},
2828
(ty::Ref(_, rty, rty_mutbl), ty::RawPtr(ptr_ty)) => {
29-
span_lint_and_then(
30-
cx,
31-
USELESS_TRANSMUTE,
32-
e.span,
33-
"transmute from a reference to a pointer",
34-
|diag| {
35-
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
36-
let rty_and_mut = ty::TypeAndMut {
37-
ty: *rty,
38-
mutbl: *rty_mutbl,
39-
};
29+
// No way to give the correct suggestion here. Avoid linting for now.
30+
if !rty.has_erased_regions() {
31+
span_lint_and_then(
32+
cx,
33+
USELESS_TRANSMUTE,
34+
e.span,
35+
"transmute from a reference to a pointer",
36+
|diag| {
37+
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
38+
let rty_and_mut = ty::TypeAndMut {
39+
ty: *rty,
40+
mutbl: *rty_mutbl,
41+
};
4042

41-
let sugg = if *ptr_ty == rty_and_mut {
42-
arg.as_ty(to_ty)
43-
} else {
44-
arg.as_ty(cx.tcx.mk_ptr(rty_and_mut)).as_ty(to_ty)
45-
};
43+
let sugg = if *ptr_ty == rty_and_mut {
44+
arg.as_ty(to_ty)
45+
} else {
46+
arg.as_ty(cx.tcx.mk_ptr(rty_and_mut)).as_ty(to_ty)
47+
};
4648

47-
diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
48-
}
49-
},
50-
);
49+
diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
50+
}
51+
},
52+
);
53+
}
5154
true
5255
},
5356
(ty::Int(_) | ty::Uint(_), ty::RawPtr(_)) => {

tests/ui/transmute.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ fn my_vec() -> MyVec<i32> {
1616
#[allow(clippy::needless_lifetimes, clippy::transmute_ptr_to_ptr)]
1717
#[warn(clippy::useless_transmute)]
1818
unsafe fn _generic<'a, T, U: 'a>(t: &'a T) {
19-
let _: &'a T = core::intrinsics::transmute(t);
19+
// FIXME: should lint
20+
// let _: &'a T = core::intrinsics::transmute(t);
2021

2122
let _: &'a U = core::intrinsics::transmute(t);
2223

@@ -48,6 +49,22 @@ fn useless() {
4849

4950
let _ = (1 + 1_usize) as *const usize;
5051
}
52+
53+
unsafe fn _f<'a, 'b>(x: &'a u32) -> &'b u32 {
54+
std::mem::transmute(x)
55+
}
56+
57+
unsafe fn _f2<'a, 'b>(x: *const (dyn Iterator<Item = u32> + 'a)) -> *const (dyn Iterator<Item = u32> + 'b) {
58+
std::mem::transmute(x)
59+
}
60+
61+
unsafe fn _f3<'a, 'b>(x: fn(&'a u32)) -> fn(&'b u32) {
62+
std::mem::transmute(x)
63+
}
64+
65+
unsafe fn _f4<'a, 'b>(x: std::borrow::Cow<'a, str>) -> std::borrow::Cow<'b, str> {
66+
std::mem::transmute(x)
67+
}
5168
}
5269

5370
struct Usize(usize);

0 commit comments

Comments
 (0)