Skip to content

Vec<MaybeUninit<u8>> transmute to Vec<u8> #7706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kuviman opened this issue Sep 23, 2021 · 0 comments · Fixed by #8648
Closed

Vec<MaybeUninit<u8>> transmute to Vec<u8> #7706

kuviman opened this issue Sep 23, 2021 · 0 comments · Fixed by #8648
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@kuviman
Copy link

kuviman commented Sep 23, 2021

Lint name: unsound_collection_transmute

I tried this code:

unsafe fn foo(v: Vec<std::mem::MaybeUninit<u8>>) -> Vec<u8> {
    std::mem::transmute(v)
}

I expected to see this happen: assuming MaybeUninit has been initialized, this should be safe, since MaybeUninit should have same layout as inner type

Instead, this happened:

error: transmute from `std::vec::Vec<std::mem::MaybeUninit<u8>>` to `std::vec::Vec<u8>` with mismatched layout is unsound
 --> src/lib.rs:2:5
  |
2 |     std::mem::transmute(v)
  |     ^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[deny(clippy::unsound_collection_transmute)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unsound_collection_transmute

Btw, it seems strange, but if I instead make the function generic, everything is ok:

unsafe fn foo_generic<T>(v: Vec<std::mem::MaybeUninit<T>>) -> Vec<T> {
    std::mem::transmute(v)
}

unsafe fn foo(v: Vec<std::mem::MaybeUninit<u8>>) -> Vec<u8> {
    foo_generic(v)
}

Meta

Ran on playground

@kuviman kuviman added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Sep 23, 2021
@bors bors closed this as completed in b029a86 Apr 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant