Join GitHub today
RFC for unused const fn results #2450
I feel positive in general for this. Several points below:
[I've put off commenting here long enough, it's becoming clear I'll never get the time and energy to write something that's as careful and nuanced as this topic deserves. So I'll just give it my best. Apologies if that winds up being curt or heavy-handed.]
Like @scottmcm I am quite excited about the prospect of side-stepping the must_use-ification and getting better lints for free. However, after some reflection I am not sure whether that actually works, since const fns can diverge and that divergence can be desirable and a sufficient reason for calling. Divergence is discussed in the RFC, but hand-waved away by focusing on the not-obviously-useful case of infinite or long-running loops. Much more important, and harder to explain away, are panics, especially conditional ones (which can't be visible in the function signature).
For example, consider an
One can argue whether all of those are good coding style, but even if not, it makes clear that the proposed lint has serious false positives. Furthermore, these false positives are inherent in the approach and can't easily be fixed. Panics are an important tool in Rust, and as const fns encompass more and more of Rust, treating const fns without considering panics is as hopeless as approaching general Rust code without considering panics.
Now, to preempt some objections:
The RFC OP states that the goal is to "lint on side effect free const fn invocations that are not used by the program". The motivation then clarifies this as
Which is true right now, but as @rkruppe correctly asserted, won't be so in the future. Panicking is a side-effect, but still fine for
In rust-lang/rust#51570 I am writing an analysis that figures out whether a call to a const fn should be
Thus, I suggest we use the same analysis to decide which function calls to lint.
The period of my contributions to Rust upstream has reached an end. Thus I'm unable to continue my work on this. I still think something like this is a great addition. I urge anyone interested in this change to adopt and continue it from here on. Thanks.