diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 69ad7f6627500..66ee25a357b4c 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -16,6 +16,7 @@ use rustc_middle::mir::{ FakeReadCause, LocalDecl, LocalInfo, LocalKind, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind, VarBindingForm, }; +//use rustc_middle::ty::subst::InternalSubsts; use rustc_middle::ty::{self, suggest_constraining_type_params, PredicateKind, Ty}; use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex}; use rustc_span::def_id::LocalDefId; @@ -336,6 +337,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let inits = &self.move_data.init_path_map[mpi]; let move_path = &self.move_data.move_paths[mpi]; let decl_span = self.body.local_decls[move_path.place.local].source_info.span; + let mut spans = vec![]; for init_idx in inits { let init = &self.move_data.inits[*init_idx]; @@ -369,6 +371,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let mut visitor = ConditionVisitor { spans: &spans, name: &name, errors: vec![] }; visitor.visit_body(&body); + let mut show_assign_sugg = false; let isnt_initialized = if let InitializationRequiringAction::PartialAssignment | InitializationRequiringAction::Assignment = desired_action { @@ -396,6 +399,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { .count() == 0 { + show_assign_sugg = true; "isn't initialized" } else { "is possibly-uninitialized" @@ -446,10 +450,69 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { } } } + err.span_label(decl_span, "binding declared here but left uninitialized"); + if show_assign_sugg { + self.suggest_assign_rvalue(&mut err, moved_place, &name, decl_span); + } err } + fn suggest_assign_rvalue( + &self, + err: &mut Diagnostic, + moved_place: PlaceRef<'tcx>, + name: &str, + decl_span: Span, + ) { + let ty = moved_place.ty(self.body, self.infcx.tcx).ty; + debug!("ty: {:?}, kind: {:?}", ty, ty.kind()); + + let initilize_msg = match ty.kind() { + ty::Array(_, n) => format!("[val; {}]", n), + ty::Int(_) | ty::Uint(_) => format!("0"), + ty::Float(_) => format!("0.0"), + ty::Bool => format!("false"), + ty::Never | ty::Error(_) => "".to_string(), + ty::Adt(def, _substs) => { + if format!("{:?}", def).starts_with("std::vec::Vec") { + format!("vec![]") + } else if let Some(default_trait) = self.infcx.tcx.get_diagnostic_item(sym::Default) && + self.infcx.tcx.infer_ctxt().enter(|infcx| { + infcx.type_implements_trait(default_trait, ty, ty::List::empty(), self.param_env).may_apply() + }) { + format!("Default::default()") + } else { + format!("something") + } + }, + _ => format!("something"), + }; + + if initilize_msg.is_empty() { + return; + } + + let sugg_span = self + .infcx + .tcx + .sess + .source_map() + .span_extend_while(decl_span, |c| c != '\n') + .unwrap_or(decl_span); + let mut prefix = self.infcx.tcx.sess.source_map().span_to_snippet(sugg_span).unwrap(); + // remove last char if eq ';' + if prefix.ends_with(';') { + prefix.pop(); + } + err.span_suggestion_verbose( + sugg_span, + format!("use `=` to assign some value to {}", name), + format!("{} = {};", prefix, initilize_msg), + Applicability::MaybeIncorrect, + ); + } + fn suggest_borrow_fn_like( &self, err: &mut Diagnostic, diff --git a/src/test/ui/asm/x86_64/type-check-5.stderr b/src/test/ui/asm/x86_64/type-check-5.stderr index e9c93fea561a8..768f22f10f0ca 100644 --- a/src/test/ui/asm/x86_64/type-check-5.stderr +++ b/src/test/ui/asm/x86_64/type-check-5.stderr @@ -5,6 +5,11 @@ LL | let x: u64; | - binding declared here but left uninitialized LL | asm!("{}", in(reg) x); | ^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: u64 = 0; + | ~~~~~~~~~~~ error[E0381]: used binding `y` isn't initialized --> $DIR/type-check-5.rs:18:9 @@ -13,6 +18,11 @@ LL | let mut y: u64; | ----- binding declared here but left uninitialized LL | asm!("{}", inout(reg) y); | ^^^^^^^^^^^^^^^^^^^^^^^^ `y` used here but it isn't initialized + | +help: use `=` to assign some value to `y` + | +LL | let mut y: u64 = 0; + | ~~~~~~~~~~~~~~~ error[E0596]: cannot borrow `v` as mutable, as it is not declared as mutable --> $DIR/type-check-5.rs:26:29 diff --git a/src/test/ui/borrowck/borrowck-block-unint.stderr b/src/test/ui/borrowck/borrowck-block-unint.stderr index e720db1c6961b..e77ec994081bb 100644 --- a/src/test/ui/borrowck/borrowck-block-unint.stderr +++ b/src/test/ui/borrowck/borrowck-block-unint.stderr @@ -7,6 +7,11 @@ LL | force(|| { | ^^ `x` used here but it isn't initialized LL | println!("{}", x); | - borrow occurs due to use in closure + | +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-break-uninit-2.stderr b/src/test/ui/borrowck/borrowck-break-uninit-2.stderr index 91038b3adca9d..ce02c966d23a3 100644 --- a/src/test/ui/borrowck/borrowck-break-uninit-2.stderr +++ b/src/test/ui/borrowck/borrowck-break-uninit-2.stderr @@ -8,6 +8,10 @@ LL | println!("{}", x); | ^ `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) +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-break-uninit.stderr b/src/test/ui/borrowck/borrowck-break-uninit.stderr index 8d0c9582fda92..84c0df39a1421 100644 --- a/src/test/ui/borrowck/borrowck-break-uninit.stderr +++ b/src/test/ui/borrowck/borrowck-break-uninit.stderr @@ -8,6 +8,10 @@ LL | println!("{}", x); | ^ `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) +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-init-in-called-fn-expr.stderr b/src/test/ui/borrowck/borrowck-init-in-called-fn-expr.stderr index e8a2fbc91ea64..438924c65886a 100644 --- a/src/test/ui/borrowck/borrowck-init-in-called-fn-expr.stderr +++ b/src/test/ui/borrowck/borrowck-init-in-called-fn-expr.stderr @@ -5,6 +5,11 @@ LL | let i: isize; | - binding declared here but left uninitialized LL | i | ^ `i` used here but it isn't initialized + | +help: use `=` to assign some value to `i` + | +LL | let i: isize = 0; + | ~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-init-in-fn-expr.stderr b/src/test/ui/borrowck/borrowck-init-in-fn-expr.stderr index 1e950d6a20def..aa8d1442521cc 100644 --- a/src/test/ui/borrowck/borrowck-init-in-fn-expr.stderr +++ b/src/test/ui/borrowck/borrowck-init-in-fn-expr.stderr @@ -5,6 +5,11 @@ LL | let i: isize; | - binding declared here but left uninitialized LL | i | ^ `i` used here but it isn't initialized + | +help: use `=` to assign some value to `i` + | +LL | let i: isize = 0; + | ~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-init-in-fru.stderr b/src/test/ui/borrowck/borrowck-init-in-fru.stderr index 83a3e3e0e3ae0..9515c46df6567 100644 --- a/src/test/ui/borrowck/borrowck-init-in-fru.stderr +++ b/src/test/ui/borrowck/borrowck-init-in-fru.stderr @@ -5,6 +5,11 @@ LL | let mut origin: Point; | ---------- binding declared here but left uninitialized LL | origin = Point { x: 10, ..origin }; | ^^^^^^^^^^^^^^^^^^^^^^^^^ `origin.y` used here but it isn't initialized + | +help: use `=` to assign some value to `origin` + | +LL | let mut origin: Point = something; + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-init-op-equal.stderr b/src/test/ui/borrowck/borrowck-init-op-equal.stderr index 74704b2abfee8..61b4a3831348f 100644 --- a/src/test/ui/borrowck/borrowck-init-op-equal.stderr +++ b/src/test/ui/borrowck/borrowck-init-op-equal.stderr @@ -5,6 +5,11 @@ LL | let v: isize; | - binding declared here but left uninitialized LL | v += 1; | ^^^^^^ `v` used here but it isn't initialized + | +help: use `=` to assign some value to `v` + | +LL | let v: isize = 0; + | ~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-init-plus-equal.stderr b/src/test/ui/borrowck/borrowck-init-plus-equal.stderr index 7542576d636be..709db53c12390 100644 --- a/src/test/ui/borrowck/borrowck-init-plus-equal.stderr +++ b/src/test/ui/borrowck/borrowck-init-plus-equal.stderr @@ -5,6 +5,11 @@ LL | let mut v: isize; | ----- binding declared here but left uninitialized LL | v = v + 1; | ^ `v` used here but it isn't initialized + | +help: use `=` to assign some value to `v` + | +LL | let mut v: isize = 0; + | ~~~~~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-return.stderr b/src/test/ui/borrowck/borrowck-return.stderr index 1c916e223175c..9f64fde939615 100644 --- a/src/test/ui/borrowck/borrowck-return.stderr +++ b/src/test/ui/borrowck/borrowck-return.stderr @@ -5,6 +5,11 @@ LL | let x: isize; | - binding declared here but left uninitialized LL | return x; | ^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-storage-dead.stderr b/src/test/ui/borrowck/borrowck-storage-dead.stderr index 2cea4392d6adb..2cd6da96f4469 100644 --- a/src/test/ui/borrowck/borrowck-storage-dead.stderr +++ b/src/test/ui/borrowck/borrowck-storage-dead.stderr @@ -5,6 +5,11 @@ LL | let x: i32; | - binding declared here but left uninitialized LL | let _ = x + 1; | ^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: i32 = 0; + | ~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-uninit-after-item.stderr b/src/test/ui/borrowck/borrowck-uninit-after-item.stderr index 588b1b0c9729c..cae2bd9e69a95 100644 --- a/src/test/ui/borrowck/borrowck-uninit-after-item.stderr +++ b/src/test/ui/borrowck/borrowck-uninit-after-item.stderr @@ -6,6 +6,11 @@ LL | let bar; LL | fn baz(_x: isize) { } LL | baz(bar); | ^^^ `bar` used here but it isn't initialized + | +help: use `=` to assign some value to `bar` + | +LL | let bar = 0; + | ~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-uninit-field-access.stderr b/src/test/ui/borrowck/borrowck-uninit-field-access.stderr index 6a38a79891970..ccfeb40834afc 100644 --- a/src/test/ui/borrowck/borrowck-uninit-field-access.stderr +++ b/src/test/ui/borrowck/borrowck-uninit-field-access.stderr @@ -5,6 +5,11 @@ LL | let mut a: Point; | ----- binding declared here but left uninitialized LL | let _ = a.x + 1; | ^^^ `a.x` used here but it isn't initialized + | +help: use `=` to assign some value to `a` + | +LL | let mut a: Point = Default::default(); + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0382]: use of moved value: `line1.origin` --> $DIR/borrowck-uninit-field-access.rs:25:13 diff --git a/src/test/ui/borrowck/borrowck-uninit-in-assignop.stderr b/src/test/ui/borrowck/borrowck-uninit-in-assignop.stderr index 744cb14e662b3..65228593775c1 100644 --- a/src/test/ui/borrowck/borrowck-uninit-in-assignop.stderr +++ b/src/test/ui/borrowck/borrowck-uninit-in-assignop.stderr @@ -5,6 +5,11 @@ LL | let x: isize; | - binding declared here but left uninitialized LL | x += 1; | ^^^^^^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error[E0381]: used binding `x` isn't initialized --> $DIR/borrowck-uninit-in-assignop.rs:9:5 @@ -13,6 +18,11 @@ LL | let x: isize; | - binding declared here but left uninitialized LL | x -= 1; | ^^^^^^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error[E0381]: used binding `x` isn't initialized --> $DIR/borrowck-uninit-in-assignop.rs:12:5 @@ -21,6 +31,11 @@ LL | let x: isize; | - binding declared here but left uninitialized LL | x *= 1; | ^^^^^^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error[E0381]: used binding `x` isn't initialized --> $DIR/borrowck-uninit-in-assignop.rs:15:5 @@ -29,6 +44,11 @@ LL | let x: isize; | - binding declared here but left uninitialized LL | x /= 1; | ^^^^^^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error[E0381]: used binding `x` isn't initialized --> $DIR/borrowck-uninit-in-assignop.rs:18:5 @@ -37,6 +57,11 @@ LL | let x: isize; | - binding declared here but left uninitialized LL | x %= 1; | ^^^^^^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error[E0381]: used binding `x` isn't initialized --> $DIR/borrowck-uninit-in-assignop.rs:21:5 @@ -45,6 +70,11 @@ LL | let x: isize; | - binding declared here but left uninitialized LL | x ^= 1; | ^^^^^^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error[E0381]: used binding `x` isn't initialized --> $DIR/borrowck-uninit-in-assignop.rs:24:5 @@ -53,6 +83,11 @@ LL | let x: isize; | - binding declared here but left uninitialized LL | x &= 1; | ^^^^^^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error[E0381]: used binding `x` isn't initialized --> $DIR/borrowck-uninit-in-assignop.rs:27:5 @@ -61,6 +96,11 @@ LL | let x: isize; | - binding declared here but left uninitialized LL | x |= 1; | ^^^^^^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error[E0381]: used binding `x` isn't initialized --> $DIR/borrowck-uninit-in-assignop.rs:30:5 @@ -69,6 +109,11 @@ LL | let x: isize; | - binding declared here but left uninitialized LL | x <<= 1; | ^^^^^^^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error[E0381]: used binding `x` isn't initialized --> $DIR/borrowck-uninit-in-assignop.rs:33:5 @@ -77,6 +122,11 @@ LL | let x: isize; | - binding declared here but left uninitialized LL | x >>= 1; | ^^^^^^^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error: aborting due to 10 previous errors diff --git a/src/test/ui/borrowck/borrowck-uninit-ref-chain.stderr b/src/test/ui/borrowck/borrowck-uninit-ref-chain.stderr index c486cb6dd0cd3..4084454e36b97 100644 --- a/src/test/ui/borrowck/borrowck-uninit-ref-chain.stderr +++ b/src/test/ui/borrowck/borrowck-uninit-ref-chain.stderr @@ -5,6 +5,11 @@ LL | let x: &&Box; | - binding declared here but left uninitialized LL | let _y = &**x; | ^^^^ `**x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: &&Box = something; + | ~~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0381]: used binding `x` isn't initialized --> $DIR/borrowck-uninit-ref-chain.rs:11:14 @@ -13,6 +18,11 @@ LL | let x: &&S; | - binding declared here but left uninitialized LL | let _y = &**x; | ^^^^ `**x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: &&S = something; + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0381]: used binding `x` isn't initialized --> $DIR/borrowck-uninit-ref-chain.rs:14:14 @@ -21,6 +31,11 @@ LL | let x: &&i32; | - binding declared here but left uninitialized LL | let _y = &**x; | ^^^^ `**x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: &&i32 = something; + | ~~~~~~~~~~~~~~~~~~~~~ error[E0381]: partially assigned binding `a` isn't fully initialized --> $DIR/borrowck-uninit-ref-chain.rs:18:5 diff --git a/src/test/ui/borrowck/borrowck-uninit.stderr b/src/test/ui/borrowck/borrowck-uninit.stderr index d5566691a8200..e16bdbc14e07c 100644 --- a/src/test/ui/borrowck/borrowck-uninit.stderr +++ b/src/test/ui/borrowck/borrowck-uninit.stderr @@ -5,6 +5,11 @@ LL | let x: isize; | - binding declared here but left uninitialized LL | foo(x); | ^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: isize = 0; + | ~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-use-in-index-lvalue.stderr b/src/test/ui/borrowck/borrowck-use-in-index-lvalue.stderr index 459cf1398b750..a8f797c163911 100644 --- a/src/test/ui/borrowck/borrowck-use-in-index-lvalue.stderr +++ b/src/test/ui/borrowck/borrowck-use-in-index-lvalue.stderr @@ -5,6 +5,11 @@ LL | let w: &mut [isize]; | - binding declared here but left uninitialized LL | w[5] = 0; | ^^^^ `*w` used here but it isn't initialized + | +help: use `=` to assign some value to `w` + | +LL | let w: &mut [isize] = something; + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0381]: used binding `w` isn't initialized --> $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 LL | w[5] = 0; | ^^^^ `*w` used here but it isn't initialized + | +help: use `=` to assign some value to `w` + | +LL | let mut w: &mut [isize] = something; + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to 2 previous errors diff --git a/src/test/ui/borrowck/borrowck-use-uninitialized-in-cast-trait.stderr b/src/test/ui/borrowck/borrowck-use-uninitialized-in-cast-trait.stderr index 942ed4fc6cabf..92ac4c1b46f27 100644 --- a/src/test/ui/borrowck/borrowck-use-uninitialized-in-cast-trait.stderr +++ b/src/test/ui/borrowck/borrowck-use-uninitialized-in-cast-trait.stderr @@ -5,6 +5,11 @@ LL | let x: &i32; | - binding declared here but left uninitialized LL | let y = x as *const dyn Foo; | ^ `*x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: &i32 = something; + | ~~~~~~~~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-use-uninitialized-in-cast.stderr b/src/test/ui/borrowck/borrowck-use-uninitialized-in-cast.stderr index f3289e239818a..18821c2008315 100644 --- a/src/test/ui/borrowck/borrowck-use-uninitialized-in-cast.stderr +++ b/src/test/ui/borrowck/borrowck-use-uninitialized-in-cast.stderr @@ -5,6 +5,11 @@ LL | let x: &i32; | - binding declared here but left uninitialized LL | let y = x as *const i32; | ^ `*x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: &i32 = something; + | ~~~~~~~~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-while-cond.stderr b/src/test/ui/borrowck/borrowck-while-cond.stderr index e41c1c55e6024..447a5687c3217 100644 --- a/src/test/ui/borrowck/borrowck-while-cond.stderr +++ b/src/test/ui/borrowck/borrowck-while-cond.stderr @@ -5,6 +5,11 @@ LL | let x: bool; | - binding declared here but left uninitialized LL | while x { } | ^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: bool = false; + | ~~~~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/issue-24267-flow-exit.stderr b/src/test/ui/borrowck/issue-24267-flow-exit.stderr index b85e8f216e5df..2a17a111d8d3d 100644 --- a/src/test/ui/borrowck/issue-24267-flow-exit.stderr +++ b/src/test/ui/borrowck/issue-24267-flow-exit.stderr @@ -8,6 +8,10 @@ LL | println!("{}", x); | ^ `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) +help: use `=` to assign some value to `x` + | +LL | let x: i32 = 0; + | ~~~~~~~~~~~ error[E0381]: used binding `x` isn't initialized --> $DIR/issue-24267-flow-exit.rs:18:20 @@ -19,6 +23,10 @@ LL | println!("{}", x); | ^ `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) +help: use `=` to assign some value to `x` + | +LL | let x: i32 = 0; + | ~~~~~~~~~~~ error: aborting due to 2 previous errors diff --git a/src/test/ui/borrowck/issue-62107-match-arm-scopes.stderr b/src/test/ui/borrowck/issue-62107-match-arm-scopes.stderr index f5d2eecfa91a3..4a27c0a06f7ff 100644 --- a/src/test/ui/borrowck/issue-62107-match-arm-scopes.stderr +++ b/src/test/ui/borrowck/issue-62107-match-arm-scopes.stderr @@ -5,6 +5,11 @@ LL | let e: i32; | - binding declared here but left uninitialized LL | match e { | ^ `e` used here but it isn't initialized + | +help: use `=` to assign some value to `e` + | +LL | let e: i32 = 0; + | ~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/borrowck/suggest-assign-rvalue.rs b/src/test/ui/borrowck/suggest-assign-rvalue.rs new file mode 100644 index 0000000000000..1db3cecdd8b13 --- /dev/null +++ b/src/test/ui/borrowck/suggest-assign-rvalue.rs @@ -0,0 +1,47 @@ +#![allow(dead_code)] +#![feature(never_type)] + +#[derive(Debug, Default)] +struct Demo {} + +#[derive(Debug)] +struct DemoNoDef {} + +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] +} diff --git a/src/test/ui/borrowck/suggest-assign-rvalue.stderr b/src/test/ui/borrowck/suggest-assign-rvalue.stderr new file mode 100644 index 0000000000000..585967e086dd8 --- /dev/null +++ b/src/test/ui/borrowck/suggest-assign-rvalue.stderr @@ -0,0 +1,125 @@ +error[E0381]: used binding `my_float` isn't initialized + --> $DIR/suggest-assign-rvalue.rs:15: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: use `=` to assign some value to `my_float` + | +LL | let my_float: f32 = 0.0; + | ~~~~~~~~~~~~~~~~~~~~ + +error[E0381]: used binding `demo` isn't initialized + --> $DIR/suggest-assign-rvalue.rs:18: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: use `=` to assign some value to `demo` + | +LL | let demo: Demo = Default::default(); + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +error[E0381]: used binding `demo_no` isn't initialized + --> $DIR/suggest-assign-rvalue.rs:22: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: use `=` to assign some value to `demo_no` + | +LL | let demo_no: DemoNoDef = something; + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +error[E0381]: used binding `arr` isn't initialized + --> $DIR/suggest-assign-rvalue.rs:26: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: use `=` to assign some value to `arr` + | +LL | let arr: [i32; 5] = [val; 5]; + | ~~~~~~~~~~~~~~~~~~~~~~~~~ + +error[E0381]: used binding `foo` isn't initialized + --> $DIR/suggest-assign-rvalue.rs:29: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: use `=` to assign some value to `foo` + | +LL | let foo: Vec<&str> = vec![]; + | ~~~~~~~~~~~~~~~~~~~~~~~~ + +error[E0381]: used binding `my_string` isn't initialized + --> $DIR/suggest-assign-rvalue.rs:33: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: use `=` to assign some value to `my_string` + | +LL | let my_string: String = Default::default(); + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +error[E0381]: used binding `my_int` isn't initialized + --> $DIR/suggest-assign-rvalue.rs:37: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: use `=` to assign some value to `my_int` + | +LL | let my_int: &i32 = something; + | ~~~~~~~~~~~~~~~~~~~~~~~~~ + +error[E0381]: used binding `hello` isn't initialized + --> $DIR/suggest-assign-rvalue.rs:41: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: use `=` to assign some value to `hello` + | +LL | let hello: &str = something; + | ~~~~~~~~~~~~~~~~~~~~~~~~ + +error[E0381]: used binding `never` isn't initialized + --> $DIR/suggest-assign-rvalue.rs:45: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 9 previous errors + +For more information about this error, try `rustc --explain E0381`. diff --git a/src/test/ui/closures/2229_closure_analysis/match/pattern-matching-should-fail.stderr b/src/test/ui/closures/2229_closure_analysis/match/pattern-matching-should-fail.stderr index fea5441ec673d..80ff0f761e3ae 100644 --- a/src/test/ui/closures/2229_closure_analysis/match/pattern-matching-should-fail.stderr +++ b/src/test/ui/closures/2229_closure_analysis/match/pattern-matching-should-fail.stderr @@ -76,6 +76,11 @@ LL | let x: u8; | - binding declared here but left uninitialized LL | let c1 = || match x { }; | ^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: u8 = 0; + | ~~~~~~~~~~ error: aborting due to 8 previous errors diff --git a/src/test/ui/const-generics/const-generic-default-wont-borrowck.stderr b/src/test/ui/const-generics/const-generic-default-wont-borrowck.stderr index c62f1d1d23061..4207058e858a6 100644 --- a/src/test/ui/const-generics/const-generic-default-wont-borrowck.stderr +++ b/src/test/ui/const-generics/const-generic-default-wont-borrowck.stderr @@ -5,6 +5,11 @@ LL | let s: &'static str; s.len() | - ^^^^^^^ `*s` used here but it isn't initialized | | | binding declared here but left uninitialized + | +help: use `=` to assign some value to `s` + | +LL | let s: &'static str; s.len() + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/consts/issue-78655.stderr b/src/test/ui/consts/issue-78655.stderr index f5b1123e7f343..256920cfaebc0 100644 --- a/src/test/ui/consts/issue-78655.stderr +++ b/src/test/ui/consts/issue-78655.stderr @@ -5,6 +5,11 @@ LL | let x; | - binding declared here but left uninitialized LL | &x | ^^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x = 0; + | ~~~~~~ error: could not evaluate constant pattern --> $DIR/issue-78655.rs:7:9 diff --git a/src/test/ui/drop/repeat-drop-2.stderr b/src/test/ui/drop/repeat-drop-2.stderr index 48fa2bfa975c0..dd55b1a860e23 100644 --- a/src/test/ui/drop/repeat-drop-2.stderr +++ b/src/test/ui/drop/repeat-drop-2.stderr @@ -24,6 +24,11 @@ LL | let x: u8; | - binding declared here but left uninitialized LL | let _ = [x; 0]; | ^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x: u8 = 0; + | ~~~~~~~~~~ error: aborting due to 3 previous errors diff --git a/src/test/ui/loops/loop-proper-liveness.stderr b/src/test/ui/loops/loop-proper-liveness.stderr index 14e86aee059b2..801b5e62db19b 100644 --- a/src/test/ui/loops/loop-proper-liveness.stderr +++ b/src/test/ui/loops/loop-proper-liveness.stderr @@ -8,6 +8,10 @@ LL | println!("{:?}", x); | ^ `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) +help: use `=` to assign some value to `x` + | +LL | let x: i32 = 0; + | ~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/mir/drop-elaboration-after-borrowck-error.stderr b/src/test/ui/mir/drop-elaboration-after-borrowck-error.stderr index d8154f8d2cbc4..764182484da4f 100644 --- a/src/test/ui/mir/drop-elaboration-after-borrowck-error.stderr +++ b/src/test/ui/mir/drop-elaboration-after-borrowck-error.stderr @@ -24,6 +24,11 @@ LL | let a: [String; 1]; LL | LL | a[0] = String::new(); | ^^^^ `a` used here but it isn't initialized + | +help: use `=` to assign some value to `a` + | +LL | let a: [String; 1] = [val; 1]; + | ~~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0493]: destructors cannot be evaluated at compile-time --> $DIR/drop-elaboration-after-borrowck-error.rs:18:9 diff --git a/src/test/ui/moves/issue-72649-uninit-in-loop.stderr b/src/test/ui/moves/issue-72649-uninit-in-loop.stderr index c7373b5be9d8d..305ff7c093c54 100644 --- a/src/test/ui/moves/issue-72649-uninit-in-loop.stderr +++ b/src/test/ui/moves/issue-72649-uninit-in-loop.stderr @@ -47,6 +47,11 @@ LL | let value: NonCopy; | ----- binding declared here but left uninitialized LL | let _used = value; | ^^^^^ `value` used here but it isn't initialized + | +help: use `=` to assign some value to `value` + | +LL | let value: NonCopy; + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0381]: used binding `value` isn't initialized --> $DIR/issue-72649-uninit-in-loop.rs:69:21 @@ -56,6 +61,11 @@ LL | let mut value: NonCopy; LL | loop { LL | let _used = value; | ^^^^^ `value` used here but it isn't initialized + | +help: use `=` to assign some value to `value` + | +LL | let mut value: NonCopy; + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to 6 previous errors diff --git a/src/test/ui/moves/move-into-dead-array-1.stderr b/src/test/ui/moves/move-into-dead-array-1.stderr index 344a6bbf0c92c..756276caa09e8 100644 --- a/src/test/ui/moves/move-into-dead-array-1.stderr +++ b/src/test/ui/moves/move-into-dead-array-1.stderr @@ -5,6 +5,11 @@ LL | let mut a: [D; 4]; | ----- binding declared here but left uninitialized LL | a[i] = d(); | ^^^^ `a` used here but it isn't initialized + | +help: use `=` to assign some value to `a` + | +LL | let mut a: [D; 4] = [val; 4]; + | ~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/moves/move-of-addr-of-mut.stderr b/src/test/ui/moves/move-of-addr-of-mut.stderr index e75f2b1c0894c..f2325fb3e576b 100644 --- a/src/test/ui/moves/move-of-addr-of-mut.stderr +++ b/src/test/ui/moves/move-of-addr-of-mut.stderr @@ -7,6 +7,10 @@ LL | std::ptr::addr_of_mut!(x); | ^^^^^^^^^^^^^^^^^^^^^^^^^ `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) +help: use `=` to assign some value to `x` + | +LL | let mut x: S = something; + | ~~~~~~~~~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/nll/match-cfg-fake-edges.stderr b/src/test/ui/nll/match-cfg-fake-edges.stderr index 250aa482e5c67..73eb5137f3705 100644 --- a/src/test/ui/nll/match-cfg-fake-edges.stderr +++ b/src/test/ui/nll/match-cfg-fake-edges.stderr @@ -9,6 +9,11 @@ LL | _ if { x = 2; true } => 1, LL | _ if { LL | x; | ^ `x` used here but it isn't initialized + | +help: use `=` to assign some value to `x` + | +LL | let x = 0; + | ~~~~~~ error[E0382]: use of moved value: `x` --> $DIR/match-cfg-fake-edges.rs:35:13 diff --git a/src/test/ui/nll/match-on-borrowed.stderr b/src/test/ui/nll/match-on-borrowed.stderr index 664f36f695cf3..40c3d5141c6d9 100644 --- a/src/test/ui/nll/match-on-borrowed.stderr +++ b/src/test/ui/nll/match-on-borrowed.stderr @@ -40,6 +40,11 @@ LL | let n: Never; | - binding declared here but left uninitialized LL | match n {} | ^ `n` used here but it isn't initialized + | +help: use `=` to assign some value to `n` + | +LL | let n: Never = something; + | ~~~~~~~~~~~~~~~~~~~~~ error: aborting due to 4 previous errors diff --git a/src/test/ui/uninhabited/privately-uninhabited-mir-call.stderr b/src/test/ui/uninhabited/privately-uninhabited-mir-call.stderr index 95c209f47c92a..68504f3c03987 100644 --- a/src/test/ui/uninhabited/privately-uninhabited-mir-call.stderr +++ b/src/test/ui/uninhabited/privately-uninhabited-mir-call.stderr @@ -6,6 +6,11 @@ LL | let y: &mut u32; ... LL | *y = 2; | ^^^^^^ `y` used here but it isn't initialized + | +help: use `=` to assign some value to `y` + | +LL | let y: &mut u32 = something; + | ~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to previous error