Skip to content

extend transmute_ptr_to_ref to detect transmuting newtypes around ptrs #1966

Open
@oli-obk

Description

@oli-obk

We just had something like the following code in IRC. It would be nice if clippy suggested to replace the transmute with &*(self.0 as *const i32)

struct Foo(*const i32);
fn foo(foo: &Foo) -> &i32 {
    unsafe { transmute(foo) }
}

This is an extension of the existing lint that complains about transmute<*const T, &T>(raw_ptr). We just need to check whether the transmuted-from type is a struct which has only a single raw pointer field (or all other fields are ZST).

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesL-styleLint: Belongs in the style lint groupT-middleType: Probably requires verifiying typesgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions