Skip to content

ptr_as_ptr: suggestion does not take std::ptr::null into account #11911

Closed
@KisaragiEffective

Description

@KisaragiEffective
Contributor

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

added
I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
on Dec 3, 2023
KisaragiEffective

KisaragiEffective commented on Dec 3, 2023

@KisaragiEffective
ContributorAuthor

@rustbot claim

added a commit that references this issue on Dec 3, 2023
2e0afcf
added a commit that references this issue on Dec 3, 2023
8eea8b1
added 2 commits that reference this issue on Dec 3, 2023

Auto merge of #11913 - KisaragiEffective:fix/ptr-as-ptr-with-null, r=…

018cf8b

Auto merge of #11913 - KisaragiEffective:fix/ptr-as-ptr-with-null, r=…

2793e8d
added a commit that references this issue on Dec 12, 2023
0226fad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

C-bugCategory: Clippy is not doing the correct thingI-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

    Participants

    @rustbot@KisaragiEffective

    Issue actions

      ptr_as_ptr: suggestion does not take `std::ptr::null` into account · Issue #11911 · rust-lang/rust-clippy