-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Unused variable warning for used variable. #85071
Comments
The reason seems to be that When I add a variant to the enum the warning disappears. |
Yea, since an empty enum cannot be created, I presume the compiler assumes all of the code after the call to A little smaller repro: #[derive(Debug)]
enum Foo {}
fn f() -> Foo {todo!()}
let x = f();
println!("{:?}", x); |
Yes, this behavior seems to come from this part of the liveness analysis: rust/compiler/rustc_passes/src/liveness.rs Lines 979 to 1007 in ff34b91
i.e. the compiler realizes that a call to a method returning an uninhabited type can never return (it has to either panic or be non-terminating), and so it discards everything after the call for the purposes of liveness analysis. I agree that the warnings about unused variables, while technically correct, are somewhat confusing, and that there should probably be a warning about unreachable statements after the call. |
Makes sense. Yes, an unreachable statement warning for the remainder of the code would be very helpful. 👌 |
@rustbot claim Let me try to add a warning. |
Good luck, though I tried to look into this yesterday and concluded that there is no easy way to do this. Checking for unreachable expressions is integrated into the type checker, as divergence can influence type inference, e.g. here: rust/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs Lines 654 to 660 in bba8710
Unfortunately, this means that full type information is not yet available at the point where the warnings for unreachable statements are generated. The easy solution would have been to add a call to tcx.is_ty_uninhabited_from() here:rust/compiler/rustc_typeck/src/check/expr.rs Lines 202 to 205 in bba8710
Predictably, though, this doesn't work, because type information hasn't been stored in tcx yet (see here). It doesn't even crash, but leads to a broken (non-bootstrappable) compiler.
One possibility would be to move warnings about unreachable code into a separate pass, but then there would be code duplication between the type checker's divergence checking and this separate pass; maybe that is worth it if there is some other benefit, but functions returning uninhabited types seems like too much of an edge case to be worth it. I don't mean to discourage you, though — maybe you can find a better solution! |
I can't fix it. So unassign myself. |
When trying to compile the following code I get an unused variable warning despite the variable is being used:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=66167b96f0e14d53616abbd0e6b3d12c
The current output is:
Ideally the output should not produce such a warning.
cargo fix
produces the following output:The text was updated successfully, but these errors were encountered: