Skip to content

transmutes_expressible_as_ptr_casts shows rustc diagnostics for hypotehtical as cast #17063

@hanna-kruppe

Description

@hanna-kruppe

Summary

The clippy lint transmutes_expressible_as_ptr_casts calls into rustc_hir_typeck::cast::check_cast to check if a cast from one type to another would type-check. When the cast is invalid, there shouldn't be any diagnostics from either rustc or clippy. This is mostly ensured by the check_cast helper discards only running the "check" part and not the "report cast error" part, but but there are a few errors and lints that are reported in the "check" phase. Those are shown when the clippy lint considers a transmute that rustc would lint/error on.

Note: I'm reporting this in the clippy issue tracker because it's only user-visible through clippy, but it seems likely that the root cause and fix are in rustc.

Reproducer

Code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=d4303b7bc7f0a6253a61b325976cc185

#[repr(u32)]
enum Foo { A }

impl Drop for Foo {
    fn drop(&mut self) {}
}

pub fn foo() -> u32 {
    unsafe {
        std::mem::transmute::<Foo, u32>(Foo::A)
    }
}

Current output:

    Checking playground v0.0.1 (/playground)
error: cannot cast enum `Foo` into integer `u32` because it implements `Drop`

error: could not compile `playground` (lib) due to 1 previous error

Desired output:

(No diagnostic)

Version


Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions