Compiling playground v0.0.1 (/playground)
warning: constant `A` is never used
--> src/main.rs:1:7
|
1 | const A: i32 = 323i32;
| ^
|
= note: `#[warn(dead_code)]` on by default
warning: function `a_ref` is never used
--> src/main.rs:2:10
|
2 | const fn a_ref() -> & 'static i32 { &A }
| ^^^^^
warning: static `B` is never used
--> src/main.rs:3:8
|
3 | static B: &'static i32 = a_ref();
| ^
warning: `playground` (bin "playground") generated 3 warnings
Finished dev [unoptimized + debuginfo] target(s) in 0.88s
Running `target/debug/playground`
I understand that B is dead code because I don't use it anywhere. But why aren't a_ref and A used?
In the Rust Reference, it says that " The static initializer is a constant expression evaluated at compile time." I would have thought that applied here.
Fair point, you did use it to initialize B. But const fn aren't allowed to have side effects (so far and as far as I know that's the plan going forward), so it's still valid to optimize the call out (and consider the function unused) if B is never used.
It's dead code from the POV of the compiled program, even if it happened to be ran by the compiler in the course of compilation. [1]
Try putting pub in front of each of const A, const fn as_ref, and static B and you can see how reachability changes what code is considered dead.
I'm not actually sure if it's smart enough to just not do that too, honestly. âŠī¸
Not sure why you initially asked, but as a side note, I do sometimes run
RUSTFLAGS='-A unused' cargo check ...
to silence the lint when in the middle of development or refactoring, as the dead code warnings can be quite noisy. (But I never add it to a committed file and pay attention when at a good waypoint, because the dead code checks have also caught numerous privacy-whoopses and logic errors for me.)