Skip to content

transmute_ptr_to_ref wrongly suggests to use cast when the destination type is a DST #13357

@antonilol

Description

@antonilol

Summary

title, there is also an inconsistency depending on if type annotations are used or a turbofish (see reproducer).

<*const T>::cast<U> (and the one for *mut T) does not allow an unsized U because the vtables may not be of the same type. can cast be improved to allow unsized values with the same vtable type?

Reproducer

I tried this code:

let ptr = &[1, 2, 3] as *const [i32];
let r1 = unsafe { core::mem::transmute::<*const [i32], &[u32]>(ptr) };
let r2: &[u32] = unsafe { core::mem::transmute(ptr) };

I expected to see this happen:
Suggestion to use as on both.

Instead, this happened:
Clippy suggests to rewrite the first transmute as &*ptr.cast::<[u32]>().

Version

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions