1
Fork 0

Auto merge of #4592 - rust-lang:transmute-collection, r=flip1995

New lint: `unsound_collection_transmute`

changelog: Add `unsound_collection_transmute` lint

This fixes #4515
This commit is contained in:
bors 2019-10-08 05:51:07 +00:00
commit e2393b09ac
9 changed files with 491 additions and 258 deletions

View file

@ -1225,6 +1225,7 @@ Released 2018-09-13
[`unsafe_removed_from_name`]: https://rust-lang.github.io/rust-clippy/master/index.html#unsafe_removed_from_name [`unsafe_removed_from_name`]: https://rust-lang.github.io/rust-clippy/master/index.html#unsafe_removed_from_name
[`unsafe_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#unsafe_vector_initialization [`unsafe_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#unsafe_vector_initialization
[`unseparated_literal_suffix`]: https://rust-lang.github.io/rust-clippy/master/index.html#unseparated_literal_suffix [`unseparated_literal_suffix`]: https://rust-lang.github.io/rust-clippy/master/index.html#unseparated_literal_suffix
[`unsound_collection_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#unsound_collection_transmute
[`unstable_as_mut_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#unstable_as_mut_slice [`unstable_as_mut_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#unstable_as_mut_slice
[`unstable_as_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#unstable_as_slice [`unstable_as_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#unstable_as_slice
[`unused_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_collect [`unused_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_collect

View file

@ -6,7 +6,7 @@
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code. A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
[There are 318 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) [There are 319 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you: We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:

View file

@ -893,6 +893,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
transmute::TRANSMUTE_INT_TO_FLOAT, transmute::TRANSMUTE_INT_TO_FLOAT,
transmute::TRANSMUTE_PTR_TO_PTR, transmute::TRANSMUTE_PTR_TO_PTR,
transmute::TRANSMUTE_PTR_TO_REF, transmute::TRANSMUTE_PTR_TO_REF,
transmute::UNSOUND_COLLECTION_TRANSMUTE,
transmute::USELESS_TRANSMUTE, transmute::USELESS_TRANSMUTE,
transmute::WRONG_TRANSMUTE, transmute::WRONG_TRANSMUTE,
transmuting_null::TRANSMUTING_NULL, transmuting_null::TRANSMUTING_NULL,
@ -1148,6 +1149,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL, suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL,
suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL, suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL,
swap::ALMOST_SWAPPED, swap::ALMOST_SWAPPED,
transmute::UNSOUND_COLLECTION_TRANSMUTE,
transmute::WRONG_TRANSMUTE, transmute::WRONG_TRANSMUTE,
transmuting_null::TRANSMUTING_NULL, transmuting_null::TRANSMUTING_NULL,
types::ABSURD_EXTREME_COMPARISONS, types::ABSURD_EXTREME_COMPARISONS,

View file

@ -216,6 +216,33 @@ declare_clippy_lint! {
"transmutes from a pointer to a pointer / a reference to a reference" "transmutes from a pointer to a pointer / a reference to a reference"
} }
declare_clippy_lint! {
/// **What it does:** Checks for transmutes between collections whose
/// types have different ABI, size or alignment.
///
/// **Why is this bad?** This is undefined behavior.
///
/// **Known problems:** Currently, we cannot know whether a type is a
/// collection, so we just lint the ones that come with `std`.
///
/// **Example:**
/// ```rust
/// // different size, therefore likely out-of-bounds memory access
/// // You absolutely do not want this in your code!
/// unsafe {
/// std::mem::transmute::<_, Vec<u32>>(vec![2_u16])
/// };
/// ```
///
/// You must always iterate, map and collect the values:
///
/// ```rust
/// vec![2_u16].into_iter().map(u32::from).collect::<Vec<_>>();
/// ```
pub UNSOUND_COLLECTION_TRANSMUTE,
correctness,
"transmute between collections of layout-incompatible types"
}
declare_lint_pass!(Transmute => [ declare_lint_pass!(Transmute => [
CROSSPOINTER_TRANSMUTE, CROSSPOINTER_TRANSMUTE,
TRANSMUTE_PTR_TO_REF, TRANSMUTE_PTR_TO_REF,
@ -226,15 +253,28 @@ declare_lint_pass!(Transmute => [
TRANSMUTE_BYTES_TO_STR, TRANSMUTE_BYTES_TO_STR,
TRANSMUTE_INT_TO_BOOL, TRANSMUTE_INT_TO_BOOL,
TRANSMUTE_INT_TO_FLOAT, TRANSMUTE_INT_TO_FLOAT,
UNSOUND_COLLECTION_TRANSMUTE,
]); ]);
// used to check for UNSOUND_COLLECTION_TRANSMUTE
static COLLECTIONS: &[&[&str]] = &[
&paths::VEC,
&paths::VEC_DEQUE,
&paths::BINARY_HEAP,
&paths::BTREESET,
&paths::BTREEMAP,
&paths::HASHSET,
&paths::HASHMAP,
];
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
#[allow(clippy::similar_names, clippy::too_many_lines)] #[allow(clippy::similar_names, clippy::too_many_lines)]
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
if let ExprKind::Call(ref path_expr, ref args) = e.kind { if_chain! {
if let ExprKind::Path(ref qpath) = path_expr.kind { if let ExprKind::Call(ref path_expr, ref args) = e.kind;
if let Some(def_id) = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id() { if let ExprKind::Path(ref qpath) = path_expr.kind;
if match_def_path(cx, def_id, &paths::TRANSMUTE) { if let Some(def_id) = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id();
if match_def_path(cx, def_id, &paths::TRANSMUTE);
then {
let from_ty = cx.tables.expr_ty(&args[0]); let from_ty = cx.tables.expr_ty(&args[0]);
let to_ty = cx.tables.expr_ty(e); let to_ty = cx.tables.expr_ty(e);
@ -479,9 +519,26 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
); );
}, },
), ),
_ => return, (&ty::Adt(ref from_adt, ref from_substs), &ty::Adt(ref to_adt, ref to_substs)) => {
}; if from_adt.did != to_adt.did ||
!COLLECTIONS.iter().any(|path| match_def_path(cx, to_adt.did, path)) {
return;
} }
if from_substs.types().zip(to_substs.types())
.any(|(from_ty, to_ty)| is_layout_incompatible(cx, from_ty, to_ty)) {
span_lint(
cx,
UNSOUND_COLLECTION_TRANSMUTE,
e.span,
&format!(
"transmute from `{}` to `{}` with mismatched layout is unsound",
from_ty,
to_ty
)
);
}
},
_ => return,
} }
} }
} }
@ -510,3 +567,16 @@ fn get_type_snippet(cx: &LateContext<'_, '_>, path: &QPath, to_ref_ty: Ty<'_>) -
to_ref_ty.to_string() to_ref_ty.to_string()
} }
// check if the component types of the transmuted collection and the result have different ABI,
// size or alignment
fn is_layout_incompatible<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, from: Ty<'tcx>, to: Ty<'tcx>) -> bool {
let from_ty_layout = cx.tcx.layout_of(ty::ParamEnv::empty().and(from));
let to_ty_layout = cx.tcx.layout_of(ty::ParamEnv::empty().and(to));
if let (Ok(from_layout), Ok(to_layout)) = (from_ty_layout, to_ty_layout) {
from_layout.size != to_layout.size || from_layout.align != to_layout.align || from_layout.abi != to_layout.abi
} else {
// no idea about layout, so don't lint
false
}
}

View file

@ -6,7 +6,7 @@ pub use lint::Lint;
pub use lint::LINT_LEVELS; pub use lint::LINT_LEVELS;
// begin lint list, do not remove this comment, its used in `update_lints` // begin lint list, do not remove this comment, its used in `update_lints`
pub const ALL_LINTS: [Lint; 318] = [ pub const ALL_LINTS: [Lint; 319] = [
Lint { Lint {
name: "absurd_extreme_comparisons", name: "absurd_extreme_comparisons",
group: "correctness", group: "correctness",
@ -2030,6 +2030,13 @@ pub const ALL_LINTS: [Lint; 318] = [
deprecation: None, deprecation: None,
module: "misc_early", module: "misc_early",
}, },
Lint {
name: "unsound_collection_transmute",
group: "correctness",
desc: "transmute between collections of layout-incompatible types",
deprecation: None,
module: "transmute",
},
Lint { Lint {
name: "unused_io_amount", name: "unused_io_amount",
group: "correctness", group: "correctness",

View file

@ -80,12 +80,6 @@ fn useless() {
let _: Vec<i32> = my_transmute(my_vec()); let _: Vec<i32> = my_transmute(my_vec());
let _: Vec<u32> = core::intrinsics::transmute(my_vec());
let _: Vec<u32> = core::mem::transmute(my_vec());
let _: Vec<u32> = std::intrinsics::transmute(my_vec());
let _: Vec<u32> = std::mem::transmute(my_vec());
let _: Vec<u32> = my_transmute(my_vec());
let _: *const usize = std::mem::transmute(5_isize); let _: *const usize = std::mem::transmute(5_isize);
let _ = 5_isize as *const usize; let _ = 5_isize as *const usize;

View file

@ -117,19 +117,19 @@ LL | let _: Vec<i32> = my_transmute(my_vec());
| ^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^
error: transmute from an integer to a pointer error: transmute from an integer to a pointer
--> $DIR/transmute.rs:89:31 --> $DIR/transmute.rs:83:31
| |
LL | let _: *const usize = std::mem::transmute(5_isize); LL | let _: *const usize = std::mem::transmute(5_isize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `5_isize as *const usize` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `5_isize as *const usize`
error: transmute from an integer to a pointer error: transmute from an integer to a pointer
--> $DIR/transmute.rs:93:31 --> $DIR/transmute.rs:87:31
| |
LL | let _: *const usize = std::mem::transmute(1 + 1usize); LL | let _: *const usize = std::mem::transmute(1 + 1usize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(1 + 1usize) as *const usize` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(1 + 1usize) as *const usize`
error: transmute from a type (`*const Usize`) to the type that it points to (`Usize`) error: transmute from a type (`*const Usize`) to the type that it points to (`Usize`)
--> $DIR/transmute.rs:108:24 --> $DIR/transmute.rs:102:24
| |
LL | let _: Usize = core::intrinsics::transmute(int_const_ptr); LL | let _: Usize = core::intrinsics::transmute(int_const_ptr);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -137,25 +137,25 @@ LL | let _: Usize = core::intrinsics::transmute(int_const_ptr);
= note: `-D clippy::crosspointer-transmute` implied by `-D warnings` = note: `-D clippy::crosspointer-transmute` implied by `-D warnings`
error: transmute from a type (`*mut Usize`) to the type that it points to (`Usize`) error: transmute from a type (`*mut Usize`) to the type that it points to (`Usize`)
--> $DIR/transmute.rs:110:24 --> $DIR/transmute.rs:104:24
| |
LL | let _: Usize = core::intrinsics::transmute(int_mut_ptr); LL | let _: Usize = core::intrinsics::transmute(int_mut_ptr);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from a type (`Usize`) to a pointer to that type (`*const Usize`) error: transmute from a type (`Usize`) to a pointer to that type (`*const Usize`)
--> $DIR/transmute.rs:112:31 --> $DIR/transmute.rs:106:31
| |
LL | let _: *const Usize = core::intrinsics::transmute(my_int()); LL | let _: *const Usize = core::intrinsics::transmute(my_int());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from a type (`Usize`) to a pointer to that type (`*mut Usize`) error: transmute from a type (`Usize`) to a pointer to that type (`*mut Usize`)
--> $DIR/transmute.rs:114:29 --> $DIR/transmute.rs:108:29
| |
LL | let _: *mut Usize = core::intrinsics::transmute(my_int()); LL | let _: *mut Usize = core::intrinsics::transmute(my_int());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from a `u32` to a `char` error: transmute from a `u32` to a `char`
--> $DIR/transmute.rs:120:28 --> $DIR/transmute.rs:114:28
| |
LL | let _: char = unsafe { std::mem::transmute(0_u32) }; LL | let _: char = unsafe { std::mem::transmute(0_u32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::char::from_u32(0_u32).unwrap()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::char::from_u32(0_u32).unwrap()`
@ -163,13 +163,13 @@ LL | let _: char = unsafe { std::mem::transmute(0_u32) };
= note: `-D clippy::transmute-int-to-char` implied by `-D warnings` = note: `-D clippy::transmute-int-to-char` implied by `-D warnings`
error: transmute from a `i32` to a `char` error: transmute from a `i32` to a `char`
--> $DIR/transmute.rs:121:28 --> $DIR/transmute.rs:115:28
| |
LL | let _: char = unsafe { std::mem::transmute(0_i32) }; LL | let _: char = unsafe { std::mem::transmute(0_i32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::char::from_u32(0_i32 as u32).unwrap()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::char::from_u32(0_i32 as u32).unwrap()`
error: transmute from a `u8` to a `bool` error: transmute from a `u8` to a `bool`
--> $DIR/transmute.rs:126:28 --> $DIR/transmute.rs:120:28
| |
LL | let _: bool = unsafe { std::mem::transmute(0_u8) }; LL | let _: bool = unsafe { std::mem::transmute(0_u8) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `0_u8 != 0` | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `0_u8 != 0`
@ -177,7 +177,7 @@ LL | let _: bool = unsafe { std::mem::transmute(0_u8) };
= note: `-D clippy::transmute-int-to-bool` implied by `-D warnings` = note: `-D clippy::transmute-int-to-bool` implied by `-D warnings`
error: transmute from a `u32` to a `f32` error: transmute from a `u32` to a `f32`
--> $DIR/transmute.rs:131:27 --> $DIR/transmute.rs:125:27
| |
LL | let _: f32 = unsafe { std::mem::transmute(0_u32) }; LL | let _: f32 = unsafe { std::mem::transmute(0_u32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f32::from_bits(0_u32)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f32::from_bits(0_u32)`
@ -185,13 +185,13 @@ LL | let _: f32 = unsafe { std::mem::transmute(0_u32) };
= note: `-D clippy::transmute-int-to-float` implied by `-D warnings` = note: `-D clippy::transmute-int-to-float` implied by `-D warnings`
error: transmute from a `i32` to a `f32` error: transmute from a `i32` to a `f32`
--> $DIR/transmute.rs:132:27 --> $DIR/transmute.rs:126:27
| |
LL | let _: f32 = unsafe { std::mem::transmute(0_i32) }; LL | let _: f32 = unsafe { std::mem::transmute(0_i32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f32::from_bits(0_i32 as u32)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f32::from_bits(0_i32 as u32)`
error: transmute from a `&[u8]` to a `&str` error: transmute from a `&[u8]` to a `&str`
--> $DIR/transmute.rs:136:28 --> $DIR/transmute.rs:130:28
| |
LL | let _: &str = unsafe { std::mem::transmute(b) }; LL | let _: &str = unsafe { std::mem::transmute(b) };
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8(b).unwrap()` | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8(b).unwrap()`
@ -199,13 +199,13 @@ LL | let _: &str = unsafe { std::mem::transmute(b) };
= note: `-D clippy::transmute-bytes-to-str` implied by `-D warnings` = note: `-D clippy::transmute-bytes-to-str` implied by `-D warnings`
error: transmute from a `&mut [u8]` to a `&mut str` error: transmute from a `&mut [u8]` to a `&mut str`
--> $DIR/transmute.rs:137:32 --> $DIR/transmute.rs:131:32
| |
LL | let _: &mut str = unsafe { std::mem::transmute(mb) }; LL | let _: &mut str = unsafe { std::mem::transmute(mb) };
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()` | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()`
error: transmute from a pointer to a pointer error: transmute from a pointer to a pointer
--> $DIR/transmute.rs:169:29 --> $DIR/transmute.rs:163:29
| |
LL | let _: *const f32 = std::mem::transmute(ptr); LL | let _: *const f32 = std::mem::transmute(ptr);
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr as *const f32` | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr as *const f32`
@ -213,31 +213,31 @@ LL | let _: *const f32 = std::mem::transmute(ptr);
= note: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings` = note: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings`
error: transmute from a pointer to a pointer error: transmute from a pointer to a pointer
--> $DIR/transmute.rs:170:27 --> $DIR/transmute.rs:164:27
| |
LL | let _: *mut f32 = std::mem::transmute(mut_ptr); LL | let _: *mut f32 = std::mem::transmute(mut_ptr);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `mut_ptr as *mut f32` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `mut_ptr as *mut f32`
error: transmute from a reference to a reference error: transmute from a reference to a reference
--> $DIR/transmute.rs:172:23 --> $DIR/transmute.rs:166:23
| |
LL | let _: &f32 = std::mem::transmute(&1u32); LL | let _: &f32 = std::mem::transmute(&1u32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&1u32 as *const u32 as *const f32)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&1u32 as *const u32 as *const f32)`
error: transmute from a reference to a reference error: transmute from a reference to a reference
--> $DIR/transmute.rs:173:23 --> $DIR/transmute.rs:167:23
| |
LL | let _: &f64 = std::mem::transmute(&1f32); LL | let _: &f64 = std::mem::transmute(&1f32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&1f32 as *const f32 as *const f64)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&1f32 as *const f32 as *const f64)`
error: transmute from a reference to a reference error: transmute from a reference to a reference
--> $DIR/transmute.rs:176:27 --> $DIR/transmute.rs:170:27
| |
LL | let _: &mut f32 = std::mem::transmute(&mut 1u32); LL | let _: &mut f32 = std::mem::transmute(&mut 1u32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(&mut 1u32 as *mut u32 as *mut f32)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(&mut 1u32 as *mut u32 as *mut f32)`
error: transmute from a reference to a reference error: transmute from a reference to a reference
--> $DIR/transmute.rs:177:37 --> $DIR/transmute.rs:171:37
| |
LL | let _: &GenericParam<f32> = std::mem::transmute(&GenericParam { t: 1u32 }); LL | let _: &GenericParam<f32> = std::mem::transmute(&GenericParam { t: 1u32 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>)`

View file

@ -0,0 +1,47 @@
#![warn(clippy::unsound_collection_transmute)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
use std::mem::transmute;
fn main() {
unsafe {
// wrong size
let _ = transmute::<_, Vec<u32>>(vec![0u8]);
// wrong layout
let _ = transmute::<_, Vec<[u8; 4]>>(vec![1234u32]);
// wrong size
let _ = transmute::<_, VecDeque<u32>>(VecDeque::<u8>::new());
// wrong layout
let _ = transmute::<_, VecDeque<u32>>(VecDeque::<[u8; 4]>::new());
// wrong size
let _ = transmute::<_, BinaryHeap<u32>>(BinaryHeap::<u8>::new());
// wrong layout
let _ = transmute::<_, BinaryHeap<u32>>(BinaryHeap::<[u8; 4]>::new());
// wrong size
let _ = transmute::<_, BTreeSet<u32>>(BTreeSet::<u8>::new());
// wrong layout
let _ = transmute::<_, BTreeSet<u32>>(BTreeSet::<[u8; 4]>::new());
// wrong size
let _ = transmute::<_, HashSet<u32>>(HashSet::<u8>::new());
// wrong layout
let _ = transmute::<_, HashSet<u32>>(HashSet::<[u8; 4]>::new());
// wrong size
let _ = transmute::<_, BTreeMap<u8, u32>>(BTreeMap::<u8, u8>::new());
let _ = transmute::<_, BTreeMap<u8, u32>>(BTreeMap::<u32, u32>::new());
// wrong layout
let _ = transmute::<_, BTreeMap<u8, u32>>(BTreeMap::<u8, [u8; 4]>::new());
let _ = transmute::<_, BTreeMap<u32, u32>>(BTreeMap::<[u8; 4], u32>::new());
// wrong size
let _ = transmute::<_, HashMap<u8, u32>>(HashMap::<u8, u8>::new());
let _ = transmute::<_, HashMap<u8, u32>>(HashMap::<u32, u32>::new());
// wrong layout
let _ = transmute::<_, HashMap<u8, u32>>(HashMap::<u8, [u8; 4]>::new());
let _ = transmute::<_, HashMap<u32, u32>>(HashMap::<[u8; 4], u32>::new());
}
}

View file

@ -0,0 +1,112 @@
error: transmute from `std::vec::Vec<u8>` to `std::vec::Vec<u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:9:17
|
LL | let _ = transmute::<_, Vec<u32>>(vec![0u8]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::unsound-collection-transmute` implied by `-D warnings`
error: transmute from `std::vec::Vec<u32>` to `std::vec::Vec<[u8; 4]>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:11:17
|
LL | let _ = transmute::<_, Vec<[u8; 4]>>(vec![1234u32]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::VecDeque<u8>` to `std::collections::VecDeque<u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:14:17
|
LL | let _ = transmute::<_, VecDeque<u32>>(VecDeque::<u8>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::VecDeque<[u8; 4]>` to `std::collections::VecDeque<u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:16:17
|
LL | let _ = transmute::<_, VecDeque<u32>>(VecDeque::<[u8; 4]>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::BinaryHeap<u8>` to `std::collections::BinaryHeap<u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:19:17
|
LL | let _ = transmute::<_, BinaryHeap<u32>>(BinaryHeap::<u8>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::BinaryHeap<[u8; 4]>` to `std::collections::BinaryHeap<u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:21:17
|
LL | let _ = transmute::<_, BinaryHeap<u32>>(BinaryHeap::<[u8; 4]>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::BTreeSet<u8>` to `std::collections::BTreeSet<u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:24:17
|
LL | let _ = transmute::<_, BTreeSet<u32>>(BTreeSet::<u8>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::BTreeSet<[u8; 4]>` to `std::collections::BTreeSet<u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:26:17
|
LL | let _ = transmute::<_, BTreeSet<u32>>(BTreeSet::<[u8; 4]>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::HashSet<u8>` to `std::collections::HashSet<u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:29:17
|
LL | let _ = transmute::<_, HashSet<u32>>(HashSet::<u8>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::HashSet<[u8; 4]>` to `std::collections::HashSet<u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:31:17
|
LL | let _ = transmute::<_, HashSet<u32>>(HashSet::<[u8; 4]>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::BTreeMap<u8, u8>` to `std::collections::BTreeMap<u8, u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:34:17
|
LL | let _ = transmute::<_, BTreeMap<u8, u32>>(BTreeMap::<u8, u8>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::BTreeMap<u32, u32>` to `std::collections::BTreeMap<u8, u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:35:17
|
LL | let _ = transmute::<_, BTreeMap<u8, u32>>(BTreeMap::<u32, u32>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::BTreeMap<u8, [u8; 4]>` to `std::collections::BTreeMap<u8, u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:37:17
|
LL | let _ = transmute::<_, BTreeMap<u8, u32>>(BTreeMap::<u8, [u8; 4]>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::BTreeMap<[u8; 4], u32>` to `std::collections::BTreeMap<u32, u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:38:17
|
LL | let _ = transmute::<_, BTreeMap<u32, u32>>(BTreeMap::<[u8; 4], u32>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::HashMap<u8, u8>` to `std::collections::HashMap<u8, u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:41:17
|
LL | let _ = transmute::<_, HashMap<u8, u32>>(HashMap::<u8, u8>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::HashMap<u32, u32>` to `std::collections::HashMap<u8, u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:42:17
|
LL | let _ = transmute::<_, HashMap<u8, u32>>(HashMap::<u32, u32>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::HashMap<u8, [u8; 4]>` to `std::collections::HashMap<u8, u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:44:17
|
LL | let _ = transmute::<_, HashMap<u8, u32>>(HashMap::<u8, [u8; 4]>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `std::collections::HashMap<[u8; 4], u32>` to `std::collections::HashMap<u32, u32>` with mismatched layout is unsound
--> $DIR/transmute_collection.rs:45:17
|
LL | let _ = transmute::<_, HashMap<u32, u32>>(HashMap::<[u8; 4], u32>::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 18 previous errors