rust-lang / rust-lang/rust-clippy

Incorrect `unnecessary_cast` on a raw pointer with an unknown pointee type

Open
#11,634 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

The lint should not warn in this case

Lint Name

unnecessary_cast

Reproducer

I tried this code:

fn src<A>() -> *mut *mut A {
    unimplemented!()
}

unsafe fn error_repo<A>() -> bool {
    let val = src();
    (*val as *mut A).is_null()
}

I saw this happen:

warning: casting raw pointers to the same type and constness is unnecessary (`*mut A` -> `*mut A`)
   --> src/functions.rs:622:5
    |
622 |     (*val as *mut A).is_null()
    |     ^^^^^^^^^^^^^^^^ help: try: `*val`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
    = note: `#[warn(clippy::unnecessary_cast)]` on by default

I expected not to see this error, because removing as *mut A fails to compile with this error:

error[E0699]: cannot call a method on a raw pointer with an unknown pointee type
   --> src/functions.rs:622:12
    |
622 |     (*val).is_null()
    |            ^^^^^^^
Version
rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the unnecessary_cast warning with the Rust example and the expression at src/functions.rs:622. Read the unnecessary_cast lint entry point and its existing tests, then verify that the raw-pointer cast is not warned about when removing it produces the unknown-pointee-type error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.