Skip to content

transmute_undefined_repr fails to adjust argument types. #8501

Closed
@llogiq

Description

@llogiq

Summary

If I have a mem::transmute<U, _>(v) where the type of v is autoderef-adjusted to U, the lint ignores this type adjustment and lints as if the concrete type of v would be transmuted.

Lint Name

No response

Reproducer

I tried this code:

#![warn(clippy::transmute_undefined_repr)]
struct Foo([u8]);
fn main() {
    let x = vec![0_u8, 1, 2, 3];
    // this makes the lint think it's transmuting `&Vec<u8> to &Foo`
    let _ = unsafe { std::mem::transmute::<&[u8], &Foo>(&x) };
}

I saw this happen:

warning: transmute from `&std::vec::Vec<u8>` which has an undefined layout
  --> src/test_transmute.rs:5:22
   |
55 |             unsafe { mem::transmute::<&[_], _>(&x) }
   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: requested on the command line with `-W clippy::transmute-undefined-repr`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmute_undefined_repr

I expected to see this happen:

(nothing, the lint should not apply here).

Version

rustc 1.61.0-nightly (f0c4da499 2022-03-01)
binary: rustc
commit-hash: f0c4da49983aa699f715caf681e3154b445fb60b
commit-date: 2022-03-01
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions