-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
fixed: differentiation between uninitialized and possibly uninitialized #157507
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
Merged
rust-bors
merged 1 commit into
rust-lang:main
from
mazam-97:fix-uninit-vs-possibly-uninitialized
Jun 15, 2026
+203
−9
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| fn foo(b: bool) { | ||
| let x; | ||
| if b { | ||
| x = 1; | ||
| } else { | ||
| println!("{x}"); //~ ERROR E0381 | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| foo(true); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| error[E0381]: used binding `x` isn't initialized on any path leading to this point | ||
| --> $DIR/borrowck-if-else-uninitialized.rs:6:20 | ||
| | | ||
| LL | let x; | ||
| | - binding declared here but left uninitialized | ||
| ... | ||
| LL | } else { | ||
| | ------ if the `if` condition is `false` and this `else` arm is executed, `x` is not initialized | ||
| LL | println!("{x}"); | ||
| | ^ `x` used here but it isn't initialized on any path leading to this point | ||
| | | ||
| = note: when checking initialization, the compiler describes possible control-flow paths without evaluating whether branch conditions can actually have the values shown | ||
| help: consider assigning a value | ||
| | | ||
| LL | let x = 42; | ||
| | ++++ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0381`. |
16 changes: 16 additions & 0 deletions
16
tests/ui/borrowck/borrowck-if-elseif-else-uninitialezed.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| fn main() { | ||
| let internal_code: u32; | ||
| let is_admin = true; | ||
| let access_level = 2; | ||
| if is_admin { | ||
| if access_level == 1 { | ||
| internal_code = 101; | ||
| } else if access_level == 2 { | ||
| println!("Admin access pending for code: {internal_code}"); //~ ERROR E0381 | ||
| } else { | ||
| internal_code = 103; | ||
| } | ||
| } else { | ||
| internal_code = 404; | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
tests/ui/borrowck/borrowck-if-elseif-else-uninitialezed.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| error[E0381]: used binding `internal_code` isn't initialized on any path leading to this point | ||
| --> $DIR/borrowck-if-elseif-else-uninitialezed.rs:9:55 | ||
| | | ||
| LL | let internal_code: u32; | ||
| | ------------- binding declared here but left uninitialized | ||
| ... | ||
| LL | } else if access_level == 2 { | ||
| | ----------------- if this condition is `true`, `internal_code` is not initialized | ||
| LL | println!("Admin access pending for code: {internal_code}"); | ||
| | ^^^^^^^^^^^^^ `internal_code` used here but it isn't initialized on any path leading to this point | ||
| | | ||
| = note: when checking initialization, the compiler describes possible control-flow paths without evaluating whether branch conditions can actually have the values shown | ||
| help: consider assigning a value | ||
| | | ||
| LL | let internal_code: u32 = 42; | ||
| | ++++ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0381`. |
20 changes: 20 additions & 0 deletions
20
tests/ui/borrowck/borrowck-match-multiple-uninitialized.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| enum Status { | ||
| Active, | ||
| Inactive, | ||
| Pending, | ||
| } | ||
| fn main() { | ||
| let message: &str; // Declared but not initialized | ||
| let current_status = Status::Pending; | ||
| match current_status { | ||
| Status::Active => { | ||
| message = "System is live."; | ||
| } | ||
| Status::Inactive => { | ||
| message = "System is down."; | ||
| } | ||
| Status::Pending => { | ||
| println!("{message}"); //~ ERROR E0381 | ||
| } | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
tests/ui/borrowck/borrowck-match-multiple-uninitialized.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| error[E0381]: used binding `message` isn't initialized on any path leading to this point | ||
| --> $DIR/borrowck-match-multiple-uninitialized.rs:17:24 | ||
| | | ||
| LL | let message: &str; // Declared but not initialized | ||
| | ------- binding declared here but left uninitialized | ||
| ... | ||
| LL | Status::Pending => { | ||
| | --------------- if this pattern is matched, `message` is not initialized | ||
| LL | println!("{message}"); | ||
| | ^^^^^^^ `message` used here but it isn't initialized on any path leading to this point | ||
| | | ||
| help: consider assigning a value | ||
| | | ||
| LL | let message: &str = ""; // Declared but not initialized | ||
| | ++++ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0381`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| fn main() { | ||
| let x; | ||
|
|
||
| match true { | ||
| true => x = 42, | ||
| false => println!("{x}") //~ ERROR E0381 | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be something like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed. |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| error[E0381]: used binding `x` isn't initialized on any path leading to this point | ||
| --> $DIR/borrowck-match-uninitialized.rs:6:29 | ||
| | | ||
| LL | let x; | ||
| | - binding declared here but left uninitialized | ||
| ... | ||
| LL | false => println!("{x}") | ||
| | ^ `x` used here but it isn't initialized on any path leading to this point | ||
| | | ||
| help: consider assigning a value | ||
| | | ||
| LL | let x = 42; | ||
| | ++++ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0381`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.