Skip to content

Commit 093e320

Browse files
committed
Auto merge of #8398 - Jarcho:unordered_transmute, r=llogiq
Add lint `transmute_undefined_repr` Partially implements #3999 and #546 This doesn't consider `enum`s at all right now as those are going to be a pain to deal with. This also allows `#[repr(Rust)]` structs with only one non-zero sized fields. I think those are technically undefined when transmuted. changelog: Add lint `transmute_undefined_repr`
2 parents 68b4498 + 68993b1 commit 093e320

20 files changed

+463
-51
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3475,6 +3475,7 @@ Released 2018-09-13
34753475
[`transmute_num_to_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_num_to_bytes
34763476
[`transmute_ptr_to_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ptr
34773477
[`transmute_ptr_to_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ref
3478+
[`transmute_undefined_repr`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_undefined_repr
34783479
[`transmutes_expressible_as_ptr_casts`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmutes_expressible_as_ptr_casts
34793480
[`transmuting_null`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmuting_null
34803481
[`trivial_regex`]: https://rust-lang.github.io/rust-clippy/master/index.html#trivial_regex

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
277277
LintId::of(transmute::TRANSMUTE_INT_TO_FLOAT),
278278
LintId::of(transmute::TRANSMUTE_NUM_TO_BYTES),
279279
LintId::of(transmute::TRANSMUTE_PTR_TO_REF),
280+
LintId::of(transmute::TRANSMUTE_UNDEFINED_REPR),
280281
LintId::of(transmute::UNSOUND_COLLECTION_TRANSMUTE),
281282
LintId::of(transmute::WRONG_TRANSMUTE),
282283
LintId::of(transmuting_null::TRANSMUTING_NULL),

clippy_lints/src/lib.register_correctness.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ store.register_group(true, "clippy::correctness", Some("clippy_correctness"), ve
5858
LintId::of(size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT),
5959
LintId::of(swap::ALMOST_SWAPPED),
6060
LintId::of(to_string_in_display::TO_STRING_IN_DISPLAY),
61+
LintId::of(transmute::TRANSMUTE_UNDEFINED_REPR),
6162
LintId::of(transmute::UNSOUND_COLLECTION_TRANSMUTE),
6263
LintId::of(transmute::WRONG_TRANSMUTE),
6364
LintId::of(transmuting_null::TRANSMUTING_NULL),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ store.register_lints(&[
473473
transmute::TRANSMUTE_NUM_TO_BYTES,
474474
transmute::TRANSMUTE_PTR_TO_PTR,
475475
transmute::TRANSMUTE_PTR_TO_REF,
476+
transmute::TRANSMUTE_UNDEFINED_REPR,
476477
transmute::UNSOUND_COLLECTION_TRANSMUTE,
477478
transmute::USELESS_TRANSMUTE,
478479
transmute::WRONG_TRANSMUTE,

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(control_flow_enum)]
66
#![feature(drain_filter)]
77
#![feature(iter_intersperse)]
8+
#![feature(let_chains)]
89
#![feature(let_else)]
910
#![feature(once_cell)]
1011
#![feature(rustc_private)]

clippy_lints/src/transmute/mod.rs

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod transmute_num_to_bytes;
77
mod transmute_ptr_to_ptr;
88
mod transmute_ptr_to_ref;
99
mod transmute_ref_to_ref;
10+
mod transmute_undefined_repr;
1011
mod transmutes_expressible_as_ptr_casts;
1112
mod unsound_collection_transmute;
1213
mod useless_transmute;
@@ -355,6 +356,30 @@ declare_clippy_lint! {
355356
"transmute between collections of layout-incompatible types"
356357
}
357358

359+
declare_clippy_lint! {
360+
/// ### What it does
361+
/// Checks for transmutes either to or from a type which does not have a defined representation.
362+
///
363+
/// ### Why is this bad?
364+
/// The results of such a transmute are not defined.
365+
///
366+
/// ### Example
367+
/// ```rust
368+
/// struct Foo<T>(u32, T);
369+
/// let _ = unsafe { core::mem::transmute::<Foo<u32>, Foo<i32>>(Foo(0u32, 0u32)) };
370+
/// ```
371+
/// Use instead:
372+
/// ```rust
373+
/// #[repr(C)]
374+
/// struct Foo<T>(u32, T);
375+
/// let _ = unsafe { core::mem::transmute::<Foo<u32>, Foo<i32>>(Foo(0u32, 0u32)) };
376+
/// ```
377+
#[clippy::version = "1.60.0"]
378+
pub TRANSMUTE_UNDEFINED_REPR,
379+
correctness,
380+
"transmute to or from a type with an undefined representation"
381+
}
382+
358383
declare_lint_pass!(Transmute => [
359384
CROSSPOINTER_TRANSMUTE,
360385
TRANSMUTE_PTR_TO_REF,
@@ -369,13 +394,13 @@ declare_lint_pass!(Transmute => [
369394
TRANSMUTE_NUM_TO_BYTES,
370395
UNSOUND_COLLECTION_TRANSMUTE,
371396
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
397+
TRANSMUTE_UNDEFINED_REPR,
372398
]);
373399

374400
impl<'tcx> LateLintPass<'tcx> for Transmute {
375-
#[allow(clippy::similar_names, clippy::too_many_lines)]
376401
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
377402
if_chain! {
378-
if let ExprKind::Call(path_expr, args) = e.kind;
403+
if let ExprKind::Call(path_expr, [arg]) = e.kind;
379404
if let ExprKind::Path(ref qpath) = path_expr.kind;
380405
if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id();
381406
if cx.tcx.is_diagnostic_item(sym::transmute, def_id);
@@ -385,28 +410,31 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
385410
// And see https://github.com/rust-lang/rust/issues/51911 for dereferencing raw pointers.
386411
let const_context = in_constant(cx, e.hir_id);
387412

388-
let from_ty = cx.typeck_results().expr_ty(&args[0]);
413+
let from_ty = cx.typeck_results().expr_ty(arg);
389414
let to_ty = cx.typeck_results().expr_ty(e);
390415

391416
// If useless_transmute is triggered, the other lints can be skipped.
392-
if useless_transmute::check(cx, e, from_ty, to_ty, args) {
417+
if useless_transmute::check(cx, e, from_ty, to_ty, arg) {
393418
return;
394419
}
395420

396-
let mut linted = wrong_transmute::check(cx, e, from_ty, to_ty);
397-
linted |= crosspointer_transmute::check(cx, e, from_ty, to_ty);
398-
linted |= transmute_ptr_to_ref::check(cx, e, from_ty, to_ty, args, qpath);
399-
linted |= transmute_int_to_char::check(cx, e, from_ty, to_ty, args);
400-
linted |= transmute_ref_to_ref::check(cx, e, from_ty, to_ty, args, const_context);
401-
linted |= transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, args);
402-
linted |= transmute_int_to_bool::check(cx, e, from_ty, to_ty, args);
403-
linted |= transmute_int_to_float::check(cx, e, from_ty, to_ty, args, const_context);
404-
linted |= transmute_float_to_int::check(cx, e, from_ty, to_ty, args, const_context);
405-
linted |= transmute_num_to_bytes::check(cx, e, from_ty, to_ty, args, const_context);
406-
linted |= unsound_collection_transmute::check(cx, e, from_ty, to_ty);
421+
let linted = wrong_transmute::check(cx, e, from_ty, to_ty)
422+
| crosspointer_transmute::check(cx, e, from_ty, to_ty)
423+
| transmute_ptr_to_ref::check(cx, e, from_ty, to_ty, arg, qpath)
424+
| transmute_int_to_char::check(cx, e, from_ty, to_ty, arg)
425+
| transmute_ref_to_ref::check(cx, e, from_ty, to_ty, arg, const_context)
426+
| transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, arg)
427+
| transmute_int_to_bool::check(cx, e, from_ty, to_ty, arg)
428+
| transmute_int_to_float::check(cx, e, from_ty, to_ty, arg, const_context)
429+
| transmute_float_to_int::check(cx, e, from_ty, to_ty, arg, const_context)
430+
| transmute_num_to_bytes::check(cx, e, from_ty, to_ty, arg, const_context)
431+
| (
432+
unsound_collection_transmute::check(cx, e, from_ty, to_ty)
433+
|| transmute_undefined_repr::check(cx, e, from_ty, to_ty)
434+
);
407435

408436
if !linted {
409-
transmutes_expressible_as_ptr_casts::check(cx, e, from_ty, to_ty, args);
437+
transmutes_expressible_as_ptr_casts::check(cx, e, from_ty, to_ty, arg);
410438
}
411439
}
412440
}

clippy_lints/src/transmute/transmute_float_to_int.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub(super) fn check<'tcx>(
1515
e: &'tcx Expr<'_>,
1616
from_ty: Ty<'tcx>,
1717
to_ty: Ty<'tcx>,
18-
args: &'tcx [Expr<'_>],
18+
mut arg: &'tcx Expr<'_>,
1919
const_context: bool,
2020
) -> bool {
2121
match (&from_ty.kind(), &to_ty.kind()) {
@@ -26,37 +26,36 @@ pub(super) fn check<'tcx>(
2626
e.span,
2727
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
2828
|diag| {
29-
let mut expr = &args[0];
30-
let mut arg = sugg::Sugg::hir(cx, expr, "..");
29+
let mut sugg = sugg::Sugg::hir(cx, arg, "..");
3130

32-
if let ExprKind::Unary(UnOp::Neg, inner_expr) = &expr.kind {
33-
expr = inner_expr;
31+
if let ExprKind::Unary(UnOp::Neg, inner_expr) = &arg.kind {
32+
arg = inner_expr;
3433
}
3534

3635
if_chain! {
3736
// if the expression is a float literal and it is unsuffixed then
3837
// add a suffix so the suggestion is valid and unambiguous
39-
if let ExprKind::Lit(lit) = &expr.kind;
38+
if let ExprKind::Lit(lit) = &arg.kind;
4039
if let ast::LitKind::Float(_, ast::LitFloatType::Unsuffixed) = lit.node;
4140
then {
42-
let op = format!("{}{}", arg, float_ty.name_str()).into();
43-
match arg {
44-
sugg::Sugg::MaybeParen(_) => arg = sugg::Sugg::MaybeParen(op),
45-
_ => arg = sugg::Sugg::NonParen(op)
41+
let op = format!("{}{}", sugg, float_ty.name_str()).into();
42+
match sugg {
43+
sugg::Sugg::MaybeParen(_) => sugg = sugg::Sugg::MaybeParen(op),
44+
_ => sugg = sugg::Sugg::NonParen(op)
4645
}
4746
}
4847
}
4948

50-
arg = sugg::Sugg::NonParen(format!("{}.to_bits()", arg.maybe_par()).into());
49+
sugg = sugg::Sugg::NonParen(format!("{}.to_bits()", sugg.maybe_par()).into());
5150

5251
// cast the result of `to_bits` if `to_ty` is signed
53-
arg = if let ty::Int(int_ty) = to_ty.kind() {
54-
arg.as_ty(int_ty.name_str().to_string())
52+
sugg = if let ty::Int(int_ty) = to_ty.kind() {
53+
sugg.as_ty(int_ty.name_str().to_string())
5554
} else {
56-
arg
55+
sugg
5756
};
5857

59-
diag.span_suggestion(e.span, "consider using", arg.to_string(), Applicability::Unspecified);
58+
diag.span_suggestion(e.span, "consider using", sugg.to_string(), Applicability::Unspecified);
6059
},
6160
);
6261
true

clippy_lints/src/transmute/transmute_int_to_bool.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub(super) fn check<'tcx>(
1515
e: &'tcx Expr<'_>,
1616
from_ty: Ty<'tcx>,
1717
to_ty: Ty<'tcx>,
18-
args: &'tcx [Expr<'_>],
18+
arg: &'tcx Expr<'_>,
1919
) -> bool {
2020
match (&from_ty.kind(), &to_ty.kind()) {
2121
(ty::Int(ty::IntTy::I8) | ty::Uint(ty::UintTy::U8), ty::Bool) => {
@@ -25,7 +25,7 @@ pub(super) fn check<'tcx>(
2525
e.span,
2626
&format!("transmute from a `{}` to a `bool`", from_ty),
2727
|diag| {
28-
let arg = sugg::Sugg::hir(cx, &args[0], "..");
28+
let arg = sugg::Sugg::hir(cx, arg, "..");
2929
let zero = sugg::Sugg::NonParen(Cow::from("0"));
3030
diag.span_suggestion(
3131
e.span,

clippy_lints/src/transmute/transmute_int_to_char.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(super) fn check<'tcx>(
1414
e: &'tcx Expr<'_>,
1515
from_ty: Ty<'tcx>,
1616
to_ty: Ty<'tcx>,
17-
args: &'tcx [Expr<'_>],
17+
arg: &'tcx Expr<'_>,
1818
) -> bool {
1919
match (&from_ty.kind(), &to_ty.kind()) {
2020
(ty::Int(ty::IntTy::I32) | ty::Uint(ty::UintTy::U32), &ty::Char) => {
@@ -24,7 +24,7 @@ pub(super) fn check<'tcx>(
2424
e.span,
2525
&format!("transmute from a `{}` to a `char`", from_ty),
2626
|diag| {
27-
let arg = sugg::Sugg::hir(cx, &args[0], "..");
27+
let arg = sugg::Sugg::hir(cx, arg, "..");
2828
let arg = if let ty::Int(_) = from_ty.kind() {
2929
arg.as_ty(ast::UintTy::U32.name_str())
3030
} else {

clippy_lints/src/transmute/transmute_int_to_float.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(super) fn check<'tcx>(
1313
e: &'tcx Expr<'_>,
1414
from_ty: Ty<'tcx>,
1515
to_ty: Ty<'tcx>,
16-
args: &'tcx [Expr<'_>],
16+
arg: &'tcx Expr<'_>,
1717
const_context: bool,
1818
) -> bool {
1919
match (&from_ty.kind(), &to_ty.kind()) {
@@ -24,7 +24,7 @@ pub(super) fn check<'tcx>(
2424
e.span,
2525
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
2626
|diag| {
27-
let arg = sugg::Sugg::hir(cx, &args[0], "..");
27+
let arg = sugg::Sugg::hir(cx, arg, "..");
2828
let arg = if let ty::Int(int_ty) = from_ty.kind() {
2929
arg.as_ty(format!(
3030
"u{}",

clippy_lints/src/transmute/transmute_num_to_bytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(super) fn check<'tcx>(
1313
e: &'tcx Expr<'_>,
1414
from_ty: Ty<'tcx>,
1515
to_ty: Ty<'tcx>,
16-
args: &'tcx [Expr<'_>],
16+
arg: &'tcx Expr<'_>,
1717
const_context: bool,
1818
) -> bool {
1919
match (&from_ty.kind(), &to_ty.kind()) {
@@ -33,7 +33,7 @@ pub(super) fn check<'tcx>(
3333
e.span,
3434
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
3535
|diag| {
36-
let arg = sugg::Sugg::hir(cx, &args[0], "..");
36+
let arg = sugg::Sugg::hir(cx, arg, "..");
3737
diag.span_suggestion(
3838
e.span,
3939
"consider using `to_ne_bytes()`",

clippy_lints/src/transmute/transmute_ptr_to_ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(super) fn check<'tcx>(
1313
e: &'tcx Expr<'_>,
1414
from_ty: Ty<'tcx>,
1515
to_ty: Ty<'tcx>,
16-
args: &'tcx [Expr<'_>],
16+
arg: &'tcx Expr<'_>,
1717
) -> bool {
1818
match (&from_ty.kind(), &to_ty.kind()) {
1919
(ty::RawPtr(_), ty::RawPtr(to_ty)) => {
@@ -23,7 +23,7 @@ pub(super) fn check<'tcx>(
2323
e.span,
2424
"transmute from a pointer to a pointer",
2525
|diag| {
26-
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
26+
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
2727
let sugg = arg.as_ty(cx.tcx.mk_ptr(*to_ty));
2828
diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
2929
}

clippy_lints/src/transmute/transmute_ptr_to_ref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(super) fn check<'tcx>(
1414
e: &'tcx Expr<'_>,
1515
from_ty: Ty<'tcx>,
1616
to_ty: Ty<'tcx>,
17-
args: &'tcx [Expr<'_>],
17+
arg: &'tcx Expr<'_>,
1818
qpath: &'tcx QPath<'_>,
1919
) -> bool {
2020
match (&from_ty.kind(), &to_ty.kind()) {
@@ -28,7 +28,7 @@ pub(super) fn check<'tcx>(
2828
from_ty, to_ty
2929
),
3030
|diag| {
31-
let arg = sugg::Sugg::hir(cx, &args[0], "..");
31+
let arg = sugg::Sugg::hir(cx, arg, "..");
3232
let (deref, cast) = if *mutbl == Mutability::Mut {
3333
("&mut *", "*mut")
3434
} else {

clippy_lints/src/transmute/transmute_ref_to_ref.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub(super) fn check<'tcx>(
1515
e: &'tcx Expr<'_>,
1616
from_ty: Ty<'tcx>,
1717
to_ty: Ty<'tcx>,
18-
args: &'tcx [Expr<'_>],
18+
arg: &'tcx Expr<'_>,
1919
const_context: bool,
2020
) -> bool {
2121
let mut triggered = false;
@@ -41,7 +41,7 @@ pub(super) fn check<'tcx>(
4141
format!(
4242
"std::str::from_utf8{}({}).unwrap()",
4343
postfix,
44-
snippet(cx, args[0].span, ".."),
44+
snippet(cx, arg.span, ".."),
4545
),
4646
Applicability::Unspecified,
4747
);
@@ -54,7 +54,7 @@ pub(super) fn check<'tcx>(
5454
TRANSMUTE_PTR_TO_PTR,
5555
e.span,
5656
"transmute from a reference to a reference",
57-
|diag| if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
57+
|diag| if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
5858
let ty_from_and_mut = ty::TypeAndMut {
5959
ty: ty_from,
6060
mutbl: *from_mutbl

0 commit comments

Comments
 (0)