Skip to content

Clippy fix error w/ ptr_as_ptr (type annotations needed for std::mem::MaybeUninit<_>) #16229

@0vercl0k

Description

@0vercl0k

Summary

repro_clippy>cargo clippy --fix --bin "repro_clippy" -p repro_clippy --allow-dirty
    Checking repro_clippy v0.1.0 (repro_clippy)
warning: failed to automatically apply fixes suggested by rustc to crate `repro_clippy`

after fixes were automatically applied the compiler reported errors within these files:

  * src\main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0282]: type annotations needed for `std::mem::MaybeUninit<_>`
  --> src\main.rs:11:9
   |
11 |     let mut t = MaybeUninit::uninit();
   |         ^^^^^
12 |     let size_of_t = size_of_val(&t);
13 |     let slice_over_t = unsafe { slice::from_raw_parts_mut(t.as_mut_ptr().cast::<u8>(), size_of_t) };
   |                                                                          ---- cannot call a method on a raw pointer with an unknown pointee type
   |
help: consider giving `t` an explicit type, where the placeholders `_` are specified
   |
11 |     let mut t: std::mem::MaybeUninit<T> = MaybeUninit::uninit();
   |              ++++++++++++++++++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0282`.
Original diagnostics will follow.

warning: `as` casting between raw pointers without changing their constness
  --> src\main.rs:13:59
   |
13 |     let slice_over_t = unsafe { slice::from_raw_parts_mut(t.as_mut_ptr() as *mut u8, size_of_t) };
   |                                                           ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `t.as_mut_ptr().cast::<u8>()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.92.0/index.html#ptr_as_ptr
   = note: `-W clippy::ptr-as-ptr` implied by `-W clippy::pedantic`
   = help: to override `-W clippy::pedantic` add `#[allow(clippy::ptr_as_ptr)]`

warning: `repro_clippy` (bin "repro_clippy") generated 1 warning (run `cargo clippy --fix --bin "repro_clippy" -p repro_clippy` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.33s

Reproducer

Code:

use std::{
    io::{self, Read},
    mem::MaybeUninit,
    slice,
};

fn x<S>(reader: &mut impl Read) -> io::Result<S>
where
    S: Copy,
{
    let mut t = MaybeUninit::uninit();
    let size_of_t = size_of_val(&t);
    let slice_over_t = unsafe { slice::from_raw_parts_mut(t.as_mut_ptr() as *mut u8, size_of_t) };

    reader.read_exact(slice_over_t)?;

    Ok(unsafe { t.assume_init() })
}

fn main() {
    let mut slice: &[u8] = &[0xef, 0xbe, 0xad, 0xde];
    println!("Hello, {:#x}", x::<u32>(&mut slice).unwrap());
}

cargo.toml:

[package]
name = "repro_clippy"
version = "0.1.0"
edition = "2024"

[lints.clippy]
pedantic = "warn"

Version

rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: x86_64-pc-windows-msvc
release: 1.92.0
LLVM version: 21.1.3

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions