Closed
Description
openedon Jun 10, 2022
Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=d0d96c280bcde85e4d524178fefca741
let pkg;
if true {
pkg = 1;
} else {
// I forgot to initialize pkg!
}
drop(pkg);
The current output is:
error[[E0381]](https://doc.rust-lang.org/stable/error-index.html#E0381): use of possibly-uninitialized variable: `pkg`
--> src/main.rs:8:10
|
8 | drop(pkg);
| ^^^ use of possibly-uninitialized `pkg`
Ideally the output should look like:
error[[E0381]](https://doc.rust-lang.org/stable/error-index.html#E0381): use of possibly-uninitialized variable: `pkg`
--> src/main.rs:8:10
|
8 | drop(pkg);
| ^^^ use of possibly-uninitialized `pkg`
|
| hint: the value is not assigned in the branch starting on src/main.rs:5:
|
5 | } else {
|
Because it can be difficult to figure out exactly which branch is failing to initialize a value, especially if it's not as simple as one if
as above. For example, there may be a whole tree of conditionals between the let
and the use, where only one sub-branch fails to initialize the variable, but spotting that amidst all the code isn't easy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment