Detect unused structs which implement private traits by mu001999 · Pull Request #122382 · rust-lang/rust (original) (raw)

Hey, I stumbled upon this weird edge case after updating to 1.81 nightly (but this also applies to 1.80 beta).
When compiling my hobby OS (https://github.com/Soveu/sovos) I get this warning:

warning: struct `GraphicsOutput` is never constructed
 --> uefi/src/protocols/gop.rs:4:12
  |
4 | pub struct GraphicsOutput {
  |            ^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `uefi` (lib) generated 1 warning

I thought it came from the binary, which would be extremely weird, because I definitely use this structure to initialize the framebuffer (https://github.com/Soveu/sovos/blob/3b0505cb6dbdc0ac1e7983309100b2d2bd240617/uefi_wrapper/src/main.rs#L55), but upon closer inspection it comes from the library itself:

soveu: ~/git/sovos/libs/uefi $ cargo check
warning: struct `GraphicsOutput` is never constructed
 --> uefi/src/protocols/gop.rs:4:12
  |
4 | pub struct GraphicsOutput {
  |            ^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `uefi` (lib) generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
soveu: ~/git/sovos/libs/uefi $

I assume the "is never constructed" comes from the fact, that this structure doesn't have all the fields public, so it can't be constructed from outside library, so it must be constructed somewhere inside. That is true, I don't construct the structure, but I cast a pointer to this type in the binary, so it definitely is not dead code. I definitely could refactor the code, so that locate_protocol could take GraphicsOutput as a template parameter and return a pointer to it, but, well, that is not the case here and now, and it is pretty confusing.

cc @BoxyUwU since we started discussing this PR on discord

EDIT: I just found #126169