rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.43k stars 1.54k forks source link

unnecessary_cast: does not handle `std::ptr::null{_mut}` #11912

Open KisaragiEffective opened 11 months ago

KisaragiEffective commented 11 months ago

Summary

unnecessary_cast does not insert actual type to std::ptr::null{_mut}'s turbofish.

Reproducer

I tried this code:

#![deny(clippy::unnecessary_cast)]
fn x() {
    let a = std::ptr::null() as *const u8;
    let b = std::ptr::null_mut() as *mut u8;
}

I expected to see this happen:

error: casting raw pointers to the same type and constness is unnecessary (`*const u8` -> `*const u8`)
 --> src/lib.rs:3:13
  |
3 |     let a = std::ptr::null() as *const u8;
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::null::<u8>()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![deny(clippy::unnecessary_cast)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^

error: casting raw pointers to the same type and constness is unnecessary (`*mut u8` -> `*mut u8`)
 --> src/lib.rs:4:13
  |
4 |     let b = std::ptr::null_mut() as *mut u8;
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::null_mut::<u8>()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast

Instead, this happened:

error: casting raw pointers to the same type and constness is unnecessary (`*const u8` -> `*const u8`)
 --> src/lib.rs:3:13
  |
3 |     let a = std::ptr::null() as *const u8;
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::null()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![deny(clippy::unnecessary_cast)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^

error: casting raw pointers to the same type and constness is unnecessary (`*mut u8` -> `*mut u8`)
 --> src/lib.rs:4:13
  |
4 |     let b = std::ptr::null_mut() as *mut u8;
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::null_mut()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast

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

cocodery commented 11 months ago

@rustbot claim

cocodery commented 10 months ago

It's a generic case more than std::ptr::null{_mut}, but is there any convenient way to get callee's fn_decl and check whether callee is a generic function? Or check whether callee is instanced in mir

cocodery commented 10 months ago

@rustbot label +E-help-wanted