Closed
Description
Summary
.
Reproducer
I tried this code:
#![allow(dead_code, unused_variables)]
#![allow(clippy::unnecessary_cast)]
/// Should not trigger an ICE in `SpanlessEq` / `consts::constant`
///
/// Issue: https://github.com/rust-lang/rust-clippy/issues/1782
use std::{mem, ptr};
fn spanless_eq_ice() {
let txt = "something";
match txt {
"something" => unsafe {
ptr::write(
ptr::null_mut() as *mut u32,
mem::transmute::<[u8; 4], _>([0, 0, 0, 255]),
)
},
_ => unsafe {
ptr::write(
ptr::null_mut() as *mut u32,
mem::transmute::<[u8; 4], _>([13, 246, 24, 255]),
)
},
}
}
fn main() {}
I expected to see this happen:
Clippy suggests
warning: `as` casting between raw pointers without changing its mutability
--> ./src/tools/clippy/tests/ui/crashes/ice-1782.rs:14:17
|
14 | ptr::null_mut() as *mut u32,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr::null_mut().cast::<u32>()`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr
= note: requested on the command line with `-W clippy::ptr-as-ptr`
Instead, this happened:
This does not build:
#![allow(dead_code, unused_variables)]
#![allow(clippy::unnecessary_cast)]
/// Should not trigger an ICE in `SpanlessEq` / `consts::constant`
///
/// Issue: https://github.com/rust-lang/rust-clippy/issues/1782
use std::{mem, ptr};
fn spanless_eq_ice() {
let txt = "something";
match txt {
"something" => unsafe {
ptr::write(
ptr::null_mut().cast::<u32>(),
mem::transmute::<[u8; 4], _>([0, 0, 0, 255]),
)
},
_ => unsafe {
ptr::write(
ptr::null_mut() as *mut u32,
mem::transmute::<[u8; 4], _>([13, 246, 24, 255]),
)
},
}
}
fn main() {}
warning: type annotations needed
--> ./src/tools/clippy/tests/ui/crashes/ice-1782.rs:14:21
|
14 | ptr::null_mut().cast::<u32>(),
| ^^^^
|
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018!
= note: for more information, see issue #46906 <https://github.com/rust-lang/rust/issues/46906>
= note: `#[warn(tyvar_behind_raw_pointer)]` on by default
error[E0283]: type annotations needed
--> ./src/tools/clippy/tests/ui/crashes/ice-1782.rs:14:5
|
14 | ptr::null_mut().cast::<u32>(),
| ^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `null_mut`
|
= note: cannot satisfy `_: std::ptr::Thin`
note: required by a bound in `std::ptr::null_mut`
--> /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:543:35
|
543 | pub const fn null_mut<T: ?Sized + Thin>() -> *mut T {
| ^^^^ required by this bound in `null_mut`
help: consider specifying the generic argument
|
14 | ptr::null_mut::<T>().cast::<u32>(),
| +++++
error: aborting due to previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0283`.
Version
rustc 1.72.0-nightly (6162f6f12 2023-07-01)
binary: rustc
commit-hash: 6162f6f12339aa81fe16b8a64644ead497e411b2
commit-date: 2023-07-01
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5
Additional Labels
No response
Activity
asquared31415 commentedon Jul 4, 2023
Note for anyone reproducing: add
#![warn(clippy::ptr_as_ptr)]
to the file and use edition 2015.KisaragiEffective commentedon Dec 3, 2023
@rustbot claim
fix: handle `std::ptr::null{_mut}`
std::ptr::null{_mut}
#11913fix: handle `std::ptr::null{_mut}`
Auto merge of #11913 - KisaragiEffective:fix/ptr-as-ptr-with-null, r=…
Auto merge of #11913 - KisaragiEffective:fix/ptr-as-ptr-with-null, r=…
fix: handle `std::ptr::null{_mut}`