Skip to content

Commit

Permalink
New lint: unsound_collection_transmute
Browse files Browse the repository at this point in the history
  • Loading branch information
llogiq committed Sep 28, 2019
1 parent 5f058d8 commit e59d8a0
Show file tree
Hide file tree
Showing 8 changed files with 503 additions and 234 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,7 @@ Released 2018-09-13
[`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
[`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_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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.

[There are 317 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
[There are 318 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:

Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
transmute::TRANSMUTE_INT_TO_FLOAT,
transmute::TRANSMUTE_PTR_TO_PTR,
transmute::TRANSMUTE_PTR_TO_REF,
transmute::UNSOUND_COLLECTION_TRANSMUTE,
transmute::USELESS_TRANSMUTE,
transmute::WRONG_TRANSMUTE,
transmuting_null::TRANSMUTING_NULL,
Expand Down Expand Up @@ -1143,6 +1144,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL,
suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL,
swap::ALMOST_SWAPPED,
transmute::UNSOUND_COLLECTION_TRANSMUTE,
transmute::WRONG_TRANSMUTE,
transmuting_null::TRANSMUTING_NULL,
types::ABSURD_EXTREME_COMPARISONS,
Expand Down
530 changes: 299 additions & 231 deletions clippy_lints/src/transmute.rs

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use lint::Lint;
pub use lint::LINT_LEVELS;

// begin lint list, do not remove this comment, it’s used in `update_lints`
pub const ALL_LINTS: [Lint; 317] = [
pub const ALL_LINTS: [Lint; 318] = [
Lint {
name: "absurd_extreme_comparisons",
group: "correctness",
Expand Down Expand Up @@ -2023,6 +2023,13 @@ pub const ALL_LINTS: [Lint; 317] = [
deprecation: None,
module: "misc_early",
},
Lint {
name: "unsound_collection_transmute",
group: "correctness",
desc: "transmute between collections of layout-incompatible types",
deprecation: None,
module: "transmute",
},
Lint {
name: "unused_io_amount",
group: "correctness",
Expand Down
34 changes: 33 additions & 1 deletion tests/ui/transmute.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,38 @@ error: transmute from a type (`std::vec::Vec<i32>`) to itself
LL | let _: Vec<i32> = my_transmute(my_vec());
| ^^^^^^^^^^^^^^^^^^^^^^

error: transmute from `std::vec::Vec<i32>` to `std::vec::Vec<u32>` with mismatched layout is unsound
--> $DIR/transmute.rs:83:27
|
LL | let _: Vec<u32> = core::intrinsics::transmute(my_vec());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[deny(clippy::unsound_collection_transmute)]` on by default

error: transmute from `std::vec::Vec<i32>` to `std::vec::Vec<u32>` with mismatched layout is unsound
--> $DIR/transmute.rs:84:27
|
LL | let _: Vec<u32> = core::mem::transmute(my_vec());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: transmute from `std::vec::Vec<i32>` to `std::vec::Vec<u32>` with mismatched layout is unsound
--> $DIR/transmute.rs:85:27
|
LL | let _: Vec<u32> = std::intrinsics::transmute(my_vec());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: transmute from `std::vec::Vec<i32>` to `std::vec::Vec<u32>` with mismatched layout is unsound
--> $DIR/transmute.rs:86:27
|
LL | let _: Vec<u32> = std::mem::transmute(my_vec());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: transmute from `std::vec::Vec<i32>` to `std::vec::Vec<u32>` with mismatched layout is unsound
--> $DIR/transmute.rs:87:27
|
LL | let _: Vec<u32> = my_transmute(my_vec());
| ^^^^^^^^^^^^^^^^^^^^^^

error: transmute from an integer to a pointer
--> $DIR/transmute.rs:89:31
|
Expand Down Expand Up @@ -242,5 +274,5 @@ error: transmute from a reference to a reference
LL | let _: &GenericParam<f32> = std::mem::transmute(&GenericParam { t: 1u32 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>)`

error: aborting due to 38 previous errors
error: aborting due to 43 previous errors

47 changes: 47 additions & 0 deletions tests/ui/transmute_collection.rs
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());
}
}
112 changes: 112 additions & 0 deletions tests/ui/transmute_collection.stderr
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

0 comments on commit e59d8a0

Please sign in to comment.