Skip to content

FP transmutes_expressible_as_ptr_casts: type fn() cannot be dereferenced #10449

Closed
@matthiaskrgr

Description

@matthiaskrgr

Summary

.

Lint Name

transmutes_expressible_as_ptr_casts

Reproducer

I tried this code:

fn main() {
    fn f() {}

    let _x: u8 = unsafe {
        *std::mem::transmute::<fn(), *const u8>(f)
    };
}

I saw this happen:

warning: transmute from `fn()` to `*const u8` which could be expressed as a pointer cast instead
 --> src/main.rs:5:10
  |
5 |         *std::mem::transmute::<fn(), *const u8>(f)
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `f as *const u8`
  |

Suggestion does not compile:

The following errors were reported:
error[E0614]: type `fn() {main::f}` cannot be dereferenced
 --> src/main.rs:5:9
  |
5 |         *f as *const u8
  |         ^^

error: aborting due to previous error

Version

rustc 1.69.0-nightly (44cfafe2f 2023-03-03)
binary: rustc
commit-hash: 44cfafe2fafe816395d3acc434663a45d5178c41
commit-date: 2023-03-03
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7

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 haveI-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