-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #4592 - rust-lang:transmute-collection, r=flip1995
New lint: `unsound_collection_transmute` changelog: Add `unsound_collection_transmute` lint This fixes #4515
- Loading branch information
Showing
9 changed files
with
490 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|