mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
Fix incorrect suggestion for uninitialize binding in destructuring pattern
This commit is contained in:
parent
b11fbfbf35
commit
75da582987
@ -613,7 +613,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||||||
if self.sugg_span.is_some() {
|
if self.sugg_span.is_some() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if let hir::StmtKind::Local(hir::Local { span, ty, init: None, .. }) = &ex.kind
|
|
||||||
|
// FIXME: We make sure that this is a normal top-level binding,
|
||||||
|
// but we could suggest `todo!()` for all uninitalized bindings in the pattern pattern
|
||||||
|
if let hir::StmtKind::Local(hir::Local { span, ty, init: None, pat, .. }) =
|
||||||
|
&ex.kind
|
||||||
|
&& let hir::PatKind::Binding(..) = pat.kind
|
||||||
&& span.contains(self.decl_span)
|
&& span.contains(self.decl_span)
|
||||||
{
|
{
|
||||||
self.sugg_span = ty.map_or(Some(self.decl_span), |ty| Some(ty.span));
|
self.sugg_span = ty.map_or(Some(self.decl_span), |ty| Some(ty.span));
|
||||||
|
@ -3,4 +3,17 @@ fn foo(x: isize) { println!("{}", x); }
|
|||||||
fn main() {
|
fn main() {
|
||||||
let x: isize;
|
let x: isize;
|
||||||
foo(x); //~ ERROR E0381
|
foo(x); //~ ERROR E0381
|
||||||
|
|
||||||
|
// test for #120634
|
||||||
|
struct A(u8);
|
||||||
|
struct B { d: u8 }
|
||||||
|
let (a, );
|
||||||
|
let [b, ];
|
||||||
|
let A(c);
|
||||||
|
let B { d };
|
||||||
|
let _: (u8, u8, u8, u8) = (a, b, c, d);
|
||||||
|
//~^ ERROR used binding `a`
|
||||||
|
//~| ERROR used binding `b`
|
||||||
|
//~| ERROR used binding `c`
|
||||||
|
//~| ERROR used binding `d`
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,41 @@ help: consider assigning a value
|
|||||||
LL | let x: isize = 0;
|
LL | let x: isize = 0;
|
||||||
| +++
|
| +++
|
||||||
|
|
||||||
error: aborting due to 1 previous error
|
error[E0381]: used binding `a` isn't initialized
|
||||||
|
--> $DIR/borrowck-uninit.rs:14:32
|
||||||
|
|
|
||||||
|
LL | let (a, );
|
||||||
|
| - binding declared here but left uninitialized
|
||||||
|
...
|
||||||
|
LL | let _: (u8, u8, u8, u8) = (a, b, c, d);
|
||||||
|
| ^ `a` used here but it isn't initialized
|
||||||
|
|
||||||
|
error[E0381]: used binding `b` isn't initialized
|
||||||
|
--> $DIR/borrowck-uninit.rs:14:35
|
||||||
|
|
|
||||||
|
LL | let [b, ];
|
||||||
|
| - binding declared here but left uninitialized
|
||||||
|
...
|
||||||
|
LL | let _: (u8, u8, u8, u8) = (a, b, c, d);
|
||||||
|
| ^ `b` used here but it isn't initialized
|
||||||
|
|
||||||
|
error[E0381]: used binding `c` isn't initialized
|
||||||
|
--> $DIR/borrowck-uninit.rs:14:38
|
||||||
|
|
|
||||||
|
LL | let A(c);
|
||||||
|
| - binding declared here but left uninitialized
|
||||||
|
LL | let B { d };
|
||||||
|
LL | let _: (u8, u8, u8, u8) = (a, b, c, d);
|
||||||
|
| ^ `c` used here but it isn't initialized
|
||||||
|
|
||||||
|
error[E0381]: used binding `d` isn't initialized
|
||||||
|
--> $DIR/borrowck-uninit.rs:14:41
|
||||||
|
|
|
||||||
|
LL | let B { d };
|
||||||
|
| - binding declared here but left uninitialized
|
||||||
|
LL | let _: (u8, u8, u8, u8) = (a, b, c, d);
|
||||||
|
| ^ `d` used here but it isn't initialized
|
||||||
|
|
||||||
|
error: aborting due to 5 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0381`.
|
For more information about this error, try `rustc --explain E0381`.
|
||||||
|
Loading…
Reference in New Issue
Block a user