fix #102087, Suggest Default::default() when binding isn't initialized

This commit is contained in:
yukang 2022-09-23 15:28:48 +08:00
parent 11bb80a92b
commit 672e3f4d77
38 changed files with 514 additions and 0 deletions

View File

@ -369,6 +369,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let mut visitor = ConditionVisitor { spans: &spans, name: &name, errors: vec![] }; let mut visitor = ConditionVisitor { spans: &spans, name: &name, errors: vec![] };
visitor.visit_body(&body); visitor.visit_body(&body);
let mut show_assign_sugg = false;
let isnt_initialized = if let InitializationRequiringAction::PartialAssignment let isnt_initialized = if let InitializationRequiringAction::PartialAssignment
| InitializationRequiringAction::Assignment = desired_action | InitializationRequiringAction::Assignment = desired_action
{ {
@ -396,6 +397,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
.count() .count()
== 0 == 0
{ {
show_assign_sugg = true;
"isn't initialized" "isn't initialized"
} else { } else {
"is possibly-uninitialized" "is possibly-uninitialized"
@ -446,10 +448,78 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
} }
} }
} }
err.span_label(decl_span, "binding declared here but left uninitialized"); err.span_label(decl_span, "binding declared here but left uninitialized");
if show_assign_sugg {
struct LetVisitor {
decl_span: Span,
sugg_span: Option<Span>,
}
impl<'v> Visitor<'v> for LetVisitor {
fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) {
if self.sugg_span.is_some() {
return;
}
if let hir::StmtKind::Local(hir::Local {
span, ty, init: None, ..
}) = &ex.kind && span.contains(self.decl_span) {
self.sugg_span = ty.map_or(Some(self.decl_span), |ty| Some(ty.span));
}
hir::intravisit::walk_stmt(self, ex);
}
}
let mut visitor = LetVisitor { decl_span, sugg_span: None };
visitor.visit_body(&body);
if let Some(span) = visitor.sugg_span {
self.suggest_assign_value(&mut err, moved_place, span);
}
}
err err
} }
fn suggest_assign_value(
&self,
err: &mut Diagnostic,
moved_place: PlaceRef<'tcx>,
sugg_span: Span,
) {
let ty = moved_place.ty(self.body, self.infcx.tcx).ty;
debug!("ty: {:?}, kind: {:?}", ty, ty.kind());
let tcx = self.infcx.tcx;
let implements_default = |ty, param_env| {
let Some(default_trait) = tcx.get_diagnostic_item(sym::Default) else {
return false;
};
tcx.infer_ctxt().enter(|infcx| {
infcx
.type_implements_trait(default_trait, ty, ty::List::empty(), param_env)
.may_apply()
})
};
let assign_value = match ty.kind() {
ty::Bool => "false",
ty::Float(_) => "0.0",
ty::Int(_) | ty::Uint(_) => "0",
ty::Never | ty::Error(_) => "",
ty::Adt(def, _) if Some(def.did()) == tcx.get_diagnostic_item(sym::Vec) => "vec![]",
ty::Adt(_, _) if implements_default(ty, self.param_env) => "Default::default()",
_ => "todo!()",
};
if !assign_value.is_empty() {
err.span_suggestion_verbose(
sugg_span.shrink_to_hi(),
format!("consider assigning a value"),
format!(" = {}", assign_value),
Applicability::MaybeIncorrect,
);
}
}
fn suggest_borrow_fn_like( fn suggest_borrow_fn_like(
&self, &self,
err: &mut Diagnostic, err: &mut Diagnostic,

View File

@ -5,6 +5,11 @@ LL | let x: u64;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | asm!("{}", in(reg) x); LL | asm!("{}", in(reg) x);
| ^ `x` used here but it isn't initialized | ^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: u64 = 0;
| +++
error[E0381]: used binding `y` isn't initialized error[E0381]: used binding `y` isn't initialized
--> $DIR/type-check-2-2.rs:22:9 --> $DIR/type-check-2-2.rs:22:9
@ -13,6 +18,11 @@ LL | let mut y: u64;
| ----- binding declared here but left uninitialized | ----- binding declared here but left uninitialized
LL | asm!("{}", inout(reg) y); LL | asm!("{}", inout(reg) y);
| ^^^^^^^^^^^^^^^^^^^^^^^^ `y` used here but it isn't initialized | ^^^^^^^^^^^^^^^^^^^^^^^^ `y` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let mut y: u64 = 0;
| +++
error[E0596]: cannot borrow `v` as mutable, as it is not declared as mutable error[E0596]: cannot borrow `v` as mutable, as it is not declared as mutable
--> $DIR/type-check-2-2.rs:30:29 --> $DIR/type-check-2-2.rs:30:29

View File

@ -5,6 +5,11 @@ LL | let x: u64;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | asm!("{}", in(reg) x); LL | asm!("{}", in(reg) x);
| ^ `x` used here but it isn't initialized | ^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: u64 = 0;
| +++
error[E0381]: used binding `y` isn't initialized error[E0381]: used binding `y` isn't initialized
--> $DIR/type-check-5.rs:18:9 --> $DIR/type-check-5.rs:18:9
@ -13,6 +18,11 @@ LL | let mut y: u64;
| ----- binding declared here but left uninitialized | ----- binding declared here but left uninitialized
LL | asm!("{}", inout(reg) y); LL | asm!("{}", inout(reg) y);
| ^^^^^^^^^^^^^^^^^^^^^^^^ `y` used here but it isn't initialized | ^^^^^^^^^^^^^^^^^^^^^^^^ `y` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let mut y: u64 = 0;
| +++
error[E0596]: cannot borrow `v` as mutable, as it is not declared as mutable error[E0596]: cannot borrow `v` as mutable, as it is not declared as mutable
--> $DIR/type-check-5.rs:26:29 --> $DIR/type-check-5.rs:26:29

View File

@ -7,6 +7,11 @@ LL | force(|| {
| ^^ `x` used here but it isn't initialized | ^^ `x` used here but it isn't initialized
LL | println!("{}", x); LL | println!("{}", x);
| - borrow occurs due to use in closure | - borrow occurs due to use in closure
|
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error: aborting due to previous error error: aborting due to previous error

View File

@ -8,6 +8,10 @@ LL | println!("{}", x);
| ^ `x` used here but it isn't initialized | ^ `x` used here but it isn't initialized
| |
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error: aborting due to previous error error: aborting due to previous error

View File

@ -8,6 +8,10 @@ LL | println!("{}", x);
| ^ `x` used here but it isn't initialized | ^ `x` used here but it isn't initialized
| |
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error: aborting due to previous error error: aborting due to previous error

View File

@ -5,6 +5,11 @@ LL | let i: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | i LL | i
| ^ `i` used here but it isn't initialized | ^ `i` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let i: isize = 0;
| +++
error: aborting due to previous error error: aborting due to previous error

View File

@ -5,6 +5,11 @@ LL | let i: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | i LL | i
| ^ `i` used here but it isn't initialized | ^ `i` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let i: isize = 0;
| +++
error: aborting due to previous error error: aborting due to previous error

View File

@ -5,6 +5,11 @@ LL | let mut origin: Point;
| ---------- binding declared here but left uninitialized | ---------- binding declared here but left uninitialized
LL | origin = Point { x: 10, ..origin }; LL | origin = Point { x: 10, ..origin };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ `origin.y` used here but it isn't initialized | ^^^^^^^^^^^^^^^^^^^^^^^^^ `origin.y` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let mut origin: Point = todo!();
| +++++++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -5,6 +5,11 @@ LL | let v: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | v += 1; LL | v += 1;
| ^^^^^^ `v` used here but it isn't initialized | ^^^^^^ `v` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let v: isize = 0;
| +++
error: aborting due to previous error error: aborting due to previous error

View File

@ -5,6 +5,11 @@ LL | let mut v: isize;
| ----- binding declared here but left uninitialized | ----- binding declared here but left uninitialized
LL | v = v + 1; LL | v = v + 1;
| ^ `v` used here but it isn't initialized | ^ `v` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let mut v: isize = 0;
| +++
error: aborting due to previous error error: aborting due to previous error

View File

@ -5,6 +5,11 @@ LL | let x: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | return x; LL | return x;
| ^ `x` used here but it isn't initialized | ^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error: aborting due to previous error error: aborting due to previous error

View File

@ -5,6 +5,11 @@ LL | let x: i32;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | let _ = x + 1; LL | let _ = x + 1;
| ^ `x` used here but it isn't initialized | ^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: i32 = 0;
| +++
error: aborting due to previous error error: aborting due to previous error

View File

@ -6,6 +6,11 @@ LL | let bar;
LL | fn baz(_x: isize) { } LL | fn baz(_x: isize) { }
LL | baz(bar); LL | baz(bar);
| ^^^ `bar` used here but it isn't initialized | ^^^ `bar` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let bar = 0;
| +++
error: aborting due to previous error error: aborting due to previous error

View File

@ -5,6 +5,11 @@ LL | let mut a: Point;
| ----- binding declared here but left uninitialized | ----- binding declared here but left uninitialized
LL | let _ = a.x + 1; LL | let _ = a.x + 1;
| ^^^ `a.x` used here but it isn't initialized | ^^^ `a.x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let mut a: Point = Default::default();
| ++++++++++++++++++++
error[E0382]: use of moved value: `line1.origin` error[E0382]: use of moved value: `line1.origin`
--> $DIR/borrowck-uninit-field-access.rs:25:13 --> $DIR/borrowck-uninit-field-access.rs:25:13

View File

@ -5,6 +5,11 @@ LL | let x: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | x += 1; LL | x += 1;
| ^^^^^^ `x` used here but it isn't initialized | ^^^^^^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error[E0381]: used binding `x` isn't initialized error[E0381]: used binding `x` isn't initialized
--> $DIR/borrowck-uninit-in-assignop.rs:9:5 --> $DIR/borrowck-uninit-in-assignop.rs:9:5
@ -13,6 +18,11 @@ LL | let x: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | x -= 1; LL | x -= 1;
| ^^^^^^ `x` used here but it isn't initialized | ^^^^^^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error[E0381]: used binding `x` isn't initialized error[E0381]: used binding `x` isn't initialized
--> $DIR/borrowck-uninit-in-assignop.rs:12:5 --> $DIR/borrowck-uninit-in-assignop.rs:12:5
@ -21,6 +31,11 @@ LL | let x: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | x *= 1; LL | x *= 1;
| ^^^^^^ `x` used here but it isn't initialized | ^^^^^^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error[E0381]: used binding `x` isn't initialized error[E0381]: used binding `x` isn't initialized
--> $DIR/borrowck-uninit-in-assignop.rs:15:5 --> $DIR/borrowck-uninit-in-assignop.rs:15:5
@ -29,6 +44,11 @@ LL | let x: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | x /= 1; LL | x /= 1;
| ^^^^^^ `x` used here but it isn't initialized | ^^^^^^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error[E0381]: used binding `x` isn't initialized error[E0381]: used binding `x` isn't initialized
--> $DIR/borrowck-uninit-in-assignop.rs:18:5 --> $DIR/borrowck-uninit-in-assignop.rs:18:5
@ -37,6 +57,11 @@ LL | let x: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | x %= 1; LL | x %= 1;
| ^^^^^^ `x` used here but it isn't initialized | ^^^^^^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error[E0381]: used binding `x` isn't initialized error[E0381]: used binding `x` isn't initialized
--> $DIR/borrowck-uninit-in-assignop.rs:21:5 --> $DIR/borrowck-uninit-in-assignop.rs:21:5
@ -45,6 +70,11 @@ LL | let x: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | x ^= 1; LL | x ^= 1;
| ^^^^^^ `x` used here but it isn't initialized | ^^^^^^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error[E0381]: used binding `x` isn't initialized error[E0381]: used binding `x` isn't initialized
--> $DIR/borrowck-uninit-in-assignop.rs:24:5 --> $DIR/borrowck-uninit-in-assignop.rs:24:5
@ -53,6 +83,11 @@ LL | let x: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | x &= 1; LL | x &= 1;
| ^^^^^^ `x` used here but it isn't initialized | ^^^^^^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error[E0381]: used binding `x` isn't initialized error[E0381]: used binding `x` isn't initialized
--> $DIR/borrowck-uninit-in-assignop.rs:27:5 --> $DIR/borrowck-uninit-in-assignop.rs:27:5
@ -61,6 +96,11 @@ LL | let x: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | x |= 1; LL | x |= 1;
| ^^^^^^ `x` used here but it isn't initialized | ^^^^^^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error[E0381]: used binding `x` isn't initialized error[E0381]: used binding `x` isn't initialized
--> $DIR/borrowck-uninit-in-assignop.rs:30:5 --> $DIR/borrowck-uninit-in-assignop.rs:30:5
@ -69,6 +109,11 @@ LL | let x: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | x <<= 1; LL | x <<= 1;
| ^^^^^^^ `x` used here but it isn't initialized | ^^^^^^^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error[E0381]: used binding `x` isn't initialized error[E0381]: used binding `x` isn't initialized
--> $DIR/borrowck-uninit-in-assignop.rs:33:5 --> $DIR/borrowck-uninit-in-assignop.rs:33:5
@ -77,6 +122,11 @@ LL | let x: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | x >>= 1; LL | x >>= 1;
| ^^^^^^^ `x` used here but it isn't initialized | ^^^^^^^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error: aborting due to 10 previous errors error: aborting due to 10 previous errors

View File

@ -5,6 +5,11 @@ LL | let x: &&Box<i32>;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | let _y = &**x; LL | let _y = &**x;
| ^^^^ `**x` used here but it isn't initialized | ^^^^ `**x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: &&Box<i32> = todo!();
| +++++++++
error[E0381]: used binding `x` isn't initialized error[E0381]: used binding `x` isn't initialized
--> $DIR/borrowck-uninit-ref-chain.rs:11:14 --> $DIR/borrowck-uninit-ref-chain.rs:11:14
@ -13,6 +18,11 @@ LL | let x: &&S<i32, i32>;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | let _y = &**x; LL | let _y = &**x;
| ^^^^ `**x` used here but it isn't initialized | ^^^^ `**x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: &&S<i32, i32> = todo!();
| +++++++++
error[E0381]: used binding `x` isn't initialized error[E0381]: used binding `x` isn't initialized
--> $DIR/borrowck-uninit-ref-chain.rs:14:14 --> $DIR/borrowck-uninit-ref-chain.rs:14:14
@ -21,6 +31,11 @@ LL | let x: &&i32;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | let _y = &**x; LL | let _y = &**x;
| ^^^^ `**x` used here but it isn't initialized | ^^^^ `**x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: &&i32 = todo!();
| +++++++++
error[E0381]: partially assigned binding `a` isn't fully initialized error[E0381]: partially assigned binding `a` isn't fully initialized
--> $DIR/borrowck-uninit-ref-chain.rs:18:5 --> $DIR/borrowck-uninit-ref-chain.rs:18:5

View File

@ -5,6 +5,11 @@ LL | let x: isize;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | foo(x); LL | foo(x);
| ^ `x` used here but it isn't initialized | ^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: isize = 0;
| +++
error: aborting due to previous error error: aborting due to previous error

View File

@ -5,6 +5,11 @@ LL | let w: &mut [isize];
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | w[5] = 0; LL | w[5] = 0;
| ^^^^ `*w` used here but it isn't initialized | ^^^^ `*w` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let w: &mut [isize] = todo!();
| +++++++++
error[E0381]: used binding `w` isn't initialized error[E0381]: used binding `w` isn't initialized
--> $DIR/borrowck-use-in-index-lvalue.rs:6:5 --> $DIR/borrowck-use-in-index-lvalue.rs:6:5
@ -13,6 +18,11 @@ LL | let mut w: &mut [isize];
| ----- binding declared here but left uninitialized | ----- binding declared here but left uninitialized
LL | w[5] = 0; LL | w[5] = 0;
| ^^^^ `*w` used here but it isn't initialized | ^^^^ `*w` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let mut w: &mut [isize] = todo!();
| +++++++++
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View File

@ -5,6 +5,11 @@ LL | let x: &i32;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | let y = x as *const dyn Foo; LL | let y = x as *const dyn Foo;
| ^ `*x` used here but it isn't initialized | ^ `*x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: &i32 = todo!();
| +++++++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -5,6 +5,11 @@ LL | let x: &i32;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | let y = x as *const i32; LL | let y = x as *const i32;
| ^ `*x` used here but it isn't initialized | ^ `*x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: &i32 = todo!();
| +++++++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -5,6 +5,11 @@ LL | let x: bool;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | while x { } LL | while x { }
| ^ `x` used here but it isn't initialized | ^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: bool = false;
| +++++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -8,6 +8,10 @@ LL | println!("{}", x);
| ^ `x` used here but it isn't initialized | ^ `x` used here but it isn't initialized
| |
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let x: i32 = 0;
| +++
error[E0381]: used binding `x` isn't initialized error[E0381]: used binding `x` isn't initialized
--> $DIR/issue-24267-flow-exit.rs:18:20 --> $DIR/issue-24267-flow-exit.rs:18:20
@ -19,6 +23,10 @@ LL | println!("{}", x);
| ^ `x` used here but it isn't initialized | ^ `x` used here but it isn't initialized
| |
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let x: i32 = 0;
| +++
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View File

@ -5,6 +5,11 @@ LL | let e: i32;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | match e { LL | match e {
| ^ `e` used here but it isn't initialized | ^ `e` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let e: i32 = 0;
| +++
error: aborting due to previous error error: aborting due to previous error

View File

@ -0,0 +1,57 @@
#![allow(dead_code)]
#![feature(never_type)]
#[derive(Debug, Default)]
struct Demo {}
#[derive(Debug)]
struct DemoNoDef {}
fn apple(_: u32) {}
fn banana() {
let chaenomeles;
apple(chaenomeles);
//~^ ERROR used binding `chaenomeles` isn't initialized [E0381]
}
fn main() {
let my_bool: bool = bool::default();
println!("my_bool: {}", my_bool);
let my_float: f32;
println!("my_float: {}", my_float);
//~^ ERROR used binding `my_float` isn't initialized
let demo: Demo;
println!("demo: {:?}", demo);
//~^ ERROR used binding `demo` isn't initialized
let demo_no: DemoNoDef;
println!("demo_no: {:?}", demo_no);
//~^ ERROR used binding `demo_no` isn't initialized
let arr: [i32; 5];
println!("arr: {:?}", arr);
//~^ ERROR used binding `arr` isn't initialized
let foo: Vec<&str>;
println!("foo: {:?}", foo);
//~^ ERROR used binding `foo` isn't initialized
let my_string: String;
println!("my_string: {}", my_string);
//~^ ERROR used binding `my_string` isn't initialized
let my_int: &i32;
println!("my_int: {}", *my_int);
//~^ ERROR used binding `my_int` isn't initialized
let hello: &str;
println!("hello: {}", hello);
//~^ ERROR used binding `hello` isn't initialized
let never: !;
println!("never: {}", never);
//~^ ERROR used binding `never` isn't initialized [E0381]
banana();
}

View File

@ -0,0 +1,138 @@
error[E0381]: used binding `chaenomeles` isn't initialized
--> $DIR/suggest-assign-rvalue.rs:14:11
|
LL | let chaenomeles;
| ----------- binding declared here but left uninitialized
LL | apple(chaenomeles);
| ^^^^^^^^^^^ `chaenomeles` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let chaenomeles = 0;
| +++
error[E0381]: used binding `my_float` isn't initialized
--> $DIR/suggest-assign-rvalue.rs:23:30
|
LL | let my_float: f32;
| -------- binding declared here but left uninitialized
LL | println!("my_float: {}", my_float);
| ^^^^^^^^ `my_float` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let my_float: f32 = 0.0;
| +++++
error[E0381]: used binding `demo` isn't initialized
--> $DIR/suggest-assign-rvalue.rs:26:28
|
LL | let demo: Demo;
| ---- binding declared here but left uninitialized
LL | println!("demo: {:?}", demo);
| ^^^^ `demo` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let demo: Demo = Default::default();
| ++++++++++++++++++++
error[E0381]: used binding `demo_no` isn't initialized
--> $DIR/suggest-assign-rvalue.rs:30:31
|
LL | let demo_no: DemoNoDef;
| ------- binding declared here but left uninitialized
LL | println!("demo_no: {:?}", demo_no);
| ^^^^^^^ `demo_no` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let demo_no: DemoNoDef = todo!();
| +++++++++
error[E0381]: used binding `arr` isn't initialized
--> $DIR/suggest-assign-rvalue.rs:34:27
|
LL | let arr: [i32; 5];
| --- binding declared here but left uninitialized
LL | println!("arr: {:?}", arr);
| ^^^ `arr` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let arr: [i32; 5] = todo!();
| +++++++++
error[E0381]: used binding `foo` isn't initialized
--> $DIR/suggest-assign-rvalue.rs:37:27
|
LL | let foo: Vec<&str>;
| --- binding declared here but left uninitialized
LL | println!("foo: {:?}", foo);
| ^^^ `foo` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let foo: Vec<&str> = vec![];
| ++++++++
error[E0381]: used binding `my_string` isn't initialized
--> $DIR/suggest-assign-rvalue.rs:41:31
|
LL | let my_string: String;
| --------- binding declared here but left uninitialized
LL | println!("my_string: {}", my_string);
| ^^^^^^^^^ `my_string` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let my_string: String = Default::default();
| ++++++++++++++++++++
error[E0381]: used binding `my_int` isn't initialized
--> $DIR/suggest-assign-rvalue.rs:45:28
|
LL | let my_int: &i32;
| ------ binding declared here but left uninitialized
LL | println!("my_int: {}", *my_int);
| ^^^^^^^ `*my_int` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let my_int: &i32 = todo!();
| +++++++++
error[E0381]: used binding `hello` isn't initialized
--> $DIR/suggest-assign-rvalue.rs:49:27
|
LL | let hello: &str;
| ----- binding declared here but left uninitialized
LL | println!("hello: {}", hello);
| ^^^^^ `hello` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let hello: &str = todo!();
| +++++++++
error[E0381]: used binding `never` isn't initialized
--> $DIR/suggest-assign-rvalue.rs:53:27
|
LL | let never: !;
| ----- binding declared here but left uninitialized
LL | println!("never: {}", never);
| ^^^^^ `never` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 10 previous errors
For more information about this error, try `rustc --explain E0381`.

View File

@ -76,6 +76,11 @@ LL | let x: u8;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | let c1 = || match x { }; LL | let c1 = || match x { };
| ^ `x` used here but it isn't initialized | ^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: u8 = 0;
| +++
error: aborting due to 8 previous errors error: aborting due to 8 previous errors

View File

@ -5,6 +5,11 @@ LL | let s: &'static str; s.len()
| - ^^^^^^^ `*s` used here but it isn't initialized | - ^^^^^^^ `*s` used here but it isn't initialized
| | | |
| binding declared here but left uninitialized | binding declared here but left uninitialized
|
help: consider assigning a value
|
LL | let s: &'static str = todo!(); s.len()
| +++++++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -5,6 +5,11 @@ LL | let x;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | &x LL | &x
| ^^ `x` used here but it isn't initialized | ^^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x = 0;
| +++
error: could not evaluate constant pattern error: could not evaluate constant pattern
--> $DIR/issue-78655.rs:7:9 --> $DIR/issue-78655.rs:7:9

View File

@ -24,6 +24,11 @@ LL | let x: u8;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | let _ = [x; 0]; LL | let _ = [x; 0];
| ^ `x` used here but it isn't initialized | ^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x: u8 = 0;
| +++
error: aborting due to 3 previous errors error: aborting due to 3 previous errors

View File

@ -8,6 +8,10 @@ LL | println!("{:?}", x);
| ^ `x` used here but it isn't initialized | ^ `x` used here but it isn't initialized
| |
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let x: i32 = 0;
| +++
error: aborting due to previous error error: aborting due to previous error

View File

@ -24,6 +24,11 @@ LL | let a: [String; 1];
LL | LL |
LL | a[0] = String::new(); LL | a[0] = String::new();
| ^^^^ `a` used here but it isn't initialized | ^^^^ `a` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let a: [String; 1] = todo!();
| +++++++++
error[E0493]: destructors cannot be evaluated at compile-time error[E0493]: destructors cannot be evaluated at compile-time
--> $DIR/drop-elaboration-after-borrowck-error.rs:18:9 --> $DIR/drop-elaboration-after-borrowck-error.rs:18:9

View File

@ -47,6 +47,11 @@ LL | let value: NonCopy;
| ----- binding declared here but left uninitialized | ----- binding declared here but left uninitialized
LL | let _used = value; LL | let _used = value;
| ^^^^^ `value` used here but it isn't initialized | ^^^^^ `value` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let value: NonCopy = todo!();
| +++++++++
error[E0381]: used binding `value` isn't initialized error[E0381]: used binding `value` isn't initialized
--> $DIR/issue-72649-uninit-in-loop.rs:69:21 --> $DIR/issue-72649-uninit-in-loop.rs:69:21
@ -56,6 +61,11 @@ LL | let mut value: NonCopy;
LL | loop { LL | loop {
LL | let _used = value; LL | let _used = value;
| ^^^^^ `value` used here but it isn't initialized | ^^^^^ `value` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let mut value: NonCopy = todo!();
| +++++++++
error: aborting due to 6 previous errors error: aborting due to 6 previous errors

View File

@ -5,6 +5,11 @@ LL | let mut a: [D; 4];
| ----- binding declared here but left uninitialized | ----- binding declared here but left uninitialized
LL | a[i] = d(); LL | a[i] = d();
| ^^^^ `a` used here but it isn't initialized | ^^^^ `a` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let mut a: [D; 4] = todo!();
| +++++++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -7,6 +7,10 @@ LL | std::ptr::addr_of_mut!(x);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ `x` used here but it isn't initialized | ^^^^^^^^^^^^^^^^^^^^^^^^^ `x` used here but it isn't initialized
| |
= note: this error originates in the macro `std::ptr::addr_of_mut` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `std::ptr::addr_of_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let mut x: S = todo!();
| +++++++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -9,6 +9,11 @@ LL | _ if { x = 2; true } => 1,
LL | _ if { LL | _ if {
LL | x; LL | x;
| ^ `x` used here but it isn't initialized | ^ `x` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let x = 0;
| +++
error[E0382]: use of moved value: `x` error[E0382]: use of moved value: `x`
--> $DIR/match-cfg-fake-edges.rs:35:13 --> $DIR/match-cfg-fake-edges.rs:35:13

View File

@ -40,6 +40,11 @@ LL | let n: Never;
| - binding declared here but left uninitialized | - binding declared here but left uninitialized
LL | match n {} LL | match n {}
| ^ `n` used here but it isn't initialized | ^ `n` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let n: Never = todo!();
| +++++++++
error: aborting due to 4 previous errors error: aborting due to 4 previous errors

View File

@ -6,6 +6,11 @@ LL | let y: &mut u32;
... ...
LL | *y = 2; LL | *y = 2;
| ^^^^^^ `y` used here but it isn't initialized | ^^^^^^ `y` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let y: &mut u32 = todo!();
| +++++++++
error: aborting due to previous error error: aborting due to previous error