Closed
Description
Summary
ptr_as_ptr
suggests expr.cast::<Ty>()
even if expr
is std::ptr::null()
or std::ptr::null_mut()
.
Reproducer
I tried this code:
#![deny(clippy::ptr_as_ptr)]
fn x() {
let a = std::ptr::null() as *const u8;
}
I expected to see this happen:
error: `as` casting between raw pointers without changing its mutability
--> src/lib.rs:3:13
|
3 | let a = std::ptr::null() as *const u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `std::ptr::null::<u8>()`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr
note: the lint level is defined here
--> src/lib.rs:1:9
|
1 | #![deny(clippy::ptr_as_ptr)]
| ^^^^^^^^^^^^^^^^^^
Instead, this happened:
error: `as` casting between raw pointers without changing its mutability
--> src/lib.rs:3:13
|
3 | let a = std::ptr::null() as *const u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `std::ptr::null().cast::<u8>()`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr
note: the lint level is defined here
--> src/lib.rs:1:9
|
1 | #![deny(clippy::ptr_as_ptr)]
| ^^^^^^^^^^^^^^^^^^
Version
rustc 1.74.0 (79e9716c9 2023-11-13)
binary: rustc
commit-hash: 79e9716c980570bfd1f666e3b16ac583f0168962
commit-date: 2023-11-13
host: x86_64-unknown-linux-gnu
release: 1.74.0
LLVM version: 17.0.4
Additional Labels
@rustbot labels +I-suggestion-causes-error
Activity
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}`