microsoft / windows-rs

Rust for Windows
https://kennykerr.ca/rust-getting-started/
Apache License 2.0
10.13k stars 473 forks source link

Rust nightly warning about dead code #3096

Closed kennykerr closed 4 weeks ago

kennykerr commented 1 month ago

The latest Rust nightly compiler raises a new warning about dead code.

D:\git\windows-rs> cargo check -p windows --features Foundation      
warning: struct `IReference` is never constructed
    --> crates\libs\windows\src\Windows\Foundation\mod.rs:1024:12
     |
1024 | pub struct IReference<T>(windows_core::IUnknown, core::marker::PhantomData<T>)
     |            ^^^^^^^^^^
     |
     = note: `#[warn(dead_code)]` on by default

warning: struct `IReferenceArray` is never constructed
    --> crates\libs\windows\src\Windows\Foundation\mod.rs:1281:12
     |
1281 | pub struct IReferenceArray<T>(windows_core::IUnknown, core::marker::PhantomData<T>)
     |            ^^^^^^^^^^^^^^^

warning: `windows` (lib) generated 2 warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.14s

This seems like a Rust nightly bug. I don't see what's wrong with this code.

Originally noticed here: https://github.com/microsoft/windows-rs/actions/runs/9501067497/job/26185702630

kennykerr commented 1 month ago

You can for example use IReference as follows (from another crate):

use windows::{core::*, Foundation::*};

fn main() -> Result<()> {
    let r: IReference<i32> = PropertyValue::CreateInt32(123)?.cast()?;
    assert_eq!(r.Value()?, 123);
    Ok(())
}

I may just allow dead_code to unblock other work until this is resolved in the Rust compiler.

kennykerr commented 1 month ago

This seems to be similar to https://github.com/rust-lang/rust/issues/126169