-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a lint for unused const fn results #50805
Conversation
r? @pnkfelix (rust_highfive has picked a reviewer for you, use r? to override) |
That's not quite true in the sense relevant for this lint. const fns are very restricted, but e.g. handling |
I believe all new lints require RFCs. |
@rkruppe good point. What about changing to let the lint fire in the following case then:
This would make the lint watertight, modulo UB (where unsafe code has interior mutability and does NOT use @steveklabnik I'd argue that this makes the dead code lint smarter so is more of an extension of currently present lints but shrug... |
I agree this is a new lint. It's not one of the usual classical data flow analyses or CFG reachability checks Furthermore, even if we'd agree that this is "just an extension of the dead code lint", it'll generate a lot of completely new warning and thus warrants extra scrutiny. |
I don't think that's really sound either. We don't yet have anything like trait Foo {
fn foo(self);
}
struct Bar<'a>(&'a mut i32);
impl<'a> Foo for Bar<'a> {
fn foo(mut self) {
self.0 = 42;
}
}
const fn foo<T: const Foo>(x: T) {
x.foo(); // lint would presumably fire, but this does something
} |
@rkruppe that type encapsulates something mutable and thus has some kind of mutability itself (no idea how it's called whether interior or exterior or some other kind but it is mutability). |
Sure but there's no way for the lint to know that when analyzing the body of |
@rkruppe I see what you are meaning. Hmmm tough problem. Any ideas to fix the lint, anyone? Leaving this open for further ideas. |
☔ The latest upstream changes (presumably #50763) made this pull request unmergeable. Please resolve the merge conflicts. |
Ping from triage @pnkfelix! This PR needs your review. |
I'm going to close this PR instead. Sadly I have no idea how to salvage it (see above) even though I hope there is a way. |
@rkruppe what about letting the lint only run on invocations where all the generic types are known? This would emit lints for |
@rkruppe SGTM 👍 Traits which only have I think such a lint along the lines outlined by @est31 in their last comment jives well with my modified notion of Ralf's notion. The modified notion being that execution of |
RFC at rust-lang/rfcs#2450 |
I don't see the connection between this strong form of CTFE correctness and the lint, could you elaborate? I don't think such a guarantee is realistic. CTFE will do checks that we can never perform at run-time, such that e.g. Off the top of my head, I cannot think of any way in which calling a |
I'm saying that the lint works with that definition, i.e: there is no problem. I'll reply here for now, but let's continue the conversation about guarantees elsewhere after that. |
Marking as blocked on the RFC. |
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. |
Implements RFC 2450.
This PR adds a lint that checks for invocations of functions which are
const fn
where the result is discarded.This is dead code as
const fn
is free of side effects.Implements my suggestion from #48926 (comment)
Important gotcha: this is not a "unused constant expression" lint but a superset of such a lint: we also lint for
const fn
invocations with non-constant parameters. Invocations in such cases would still be side effect free... well maybe except for drop impls but this is as close as we can get.