dead_code lint wrongly warns about "never used" functions that are, in fact used · Issue #126289 · rust-lang/rust (original) (raw)

@glandium

Code

mod ffi { use super::*;

extern "C" {
    pub fn DomPromise_AddRef(promise: *const Promise);
    pub fn DomPromise_Release(promise: *const Promise);
}

}

#[repr(C)] #[allow(unused)] pub struct Promise { private: [u8; 0], __nosync: ::std:📑:PhantomData<::std::rc::Rc>, }

pub unsafe trait RefCounted { unsafe fn addref(&self); unsafe fn release(&self); }

unsafe impl RefCounted for Promise { unsafe fn addref(&self) { ffi::DomPromise_AddRef(self) } unsafe fn release(&self) { ffi::DomPromise_Release(self) } }

Output with 1.79.0:

nothing

Output with 1.80.0:

warning: function `DomPromise_AddRef` is never used
 --> src/lib.rs:5:16
  |
5 |         pub fn DomPromise_AddRef(promise: *const Promise);
  |                ^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: function `DomPromise_Release` is never used
 --> src/lib.rs:6:16
  |
6 |         pub fn DomPromise_Release(promise: *const Promise);
  |                ^^^^^^^^^^^^^^^^^^

warning: `playground` (lib) generated 2 warnings

The original code didn't have the #[allow(unused)], which was added because of the "never constructed" dead_code lint, which I guess would be #126169. Adding a constructor does make the function never used errors go away, so it seems #[allow(unused)] doesn't have enough power.