@@ -329,32 +329,36 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
329329 for init_idx in inits {
330330 let init = & self . move_data . inits [ * init_idx] ;
331331 let span = init. span ( & self . body ) ;
332- spans. push ( span) ;
332+ if !span. is_dummy ( ) {
333+ spans. push ( span) ;
334+ }
333335 }
334336
335337 let ( binding, name, desc) =
336338 match self . describe_place_with_options ( used_place, IncludingDowncast ( true ) ) {
337339 Some ( name) => ( format ! ( "`{name}`" ) , format ! ( "`{name}`" ) , format ! ( "`{name}` " ) ) ,
338340 None => ( "value" . to_string ( ) , "the variable" . to_string ( ) , String :: new ( ) ) ,
339341 } ;
340- let initialized = if let InitializationRequiringAction :: PartialAssignment = desired_action {
341- // The same error is emitted for bindings that are *sometimes* initialized and the ones
342- // that are *partially* initialized by assigning to a field of an uninitialized
343- // binding. We differentiate between them for more accurate wording here.
344- "fully initialized"
345- } else if spans. iter ( ) . filter ( |i| !i. contains ( span) ) . count ( ) == 0 {
346- // We filter above to avoid misleading wording in cases like:
347- // ```
348- // let x;
349- // x += 1;
350- // ```
351- "initialized"
352- } else {
353- "initialized in all conditions"
354- } ;
342+ let isnt_initialized =
343+ if let InitializationRequiringAction :: PartialAssignment = desired_action {
344+ // The same error is emitted for bindings that are *sometimes* initialized and the ones
345+ // that are *partially* initialized by assigning to a field of an uninitialized
346+ // binding. We differentiate between them for more accurate wording here.
347+ "isn't fully initialized"
348+ } else if spans. iter ( ) . filter ( |i| !i. contains ( span) ) . count ( ) == 0 {
349+ // We filter above to avoid misleading wording in cases like the following, where `x`
350+ // has an `init`, but it is in the same place we're looking at:
351+ // ```
352+ // let x;
353+ // x += 1;
354+ // ```
355+ "isn't initialized"
356+ } else {
357+ "is possibly-uninitialized"
358+ } ;
355359 let used = desired_action. as_general_verb_in_past_tense ( ) ;
356360 let mut err =
357- struct_span_err ! ( self , span, E0381 , "{used} binding {desc}isn't {initialized }" ) ;
361+ struct_span_err ! ( self , span, E0381 , "{used} binding {desc}{isnt_initialized }" ) ;
358362 use_spans. var_span_label_path_only (
359363 & mut err,
360364 format ! ( "{} occurs due to use{}" , desired_action. as_noun( ) , use_spans. describe( ) ) ,
@@ -366,7 +370,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
366370 default value and mutate it, or use `std::mem::MaybeUninit`",
367371 ) ;
368372 }
369- err. span_label ( span, format ! ( "{binding} {used} here but it isn't {initialized }" ) ) ;
373+ err. span_label ( span, format ! ( "{binding} {used} here but it {isnt_initialized }" ) ) ;
370374
371375 // We use the statements were the binding was initialized, and inspect the HIR to look
372376 // for the branching codepaths that aren't covered, to point at them.
@@ -2561,13 +2565,16 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
25612565 v. visit_expr ( body) ;
25622566 if v. 1 {
25632567 self . errors . push ( (
2564- ex . span . to ( cond. span ) ,
2568+ cond. span ,
25652569 format ! (
2566- "this `if` expression might be missing an `else` arm that initializes \
2567- {}",
2570+ "if this `if` condition is `false`, {} is not initialized" ,
25682571 self . name,
25692572 ) ,
25702573 ) ) ;
2574+ self . errors . push ( (
2575+ ex. span . shrink_to_hi ( ) ,
2576+ format ! ( "an `else` arm might be missing here, initializing {}" , self . name) ,
2577+ ) ) ;
25712578 }
25722579 }
25732580 hir:: ExprKind :: If ( cond, body, Some ( other) ) => {
@@ -2584,16 +2591,17 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
25842591 self . errors . push ( (
25852592 cond. span ,
25862593 format ! (
2587- "{} is uninitialized if this condition isn't met and the \
2588- `while` loop runs 0 times ",
2594+ "if this condition isn't met and the `while` loop runs 0 \
2595+ times, {} is not initialized ",
25892596 self . name
25902597 ) ,
25912598 ) ) ;
25922599 } else {
25932600 self . errors . push ( (
25942601 body. span . shrink_to_hi ( ) . until ( other. span ) ,
25952602 format ! (
2596- "{} is uninitialized if this `else` arm is executed" ,
2603+ "if the `if` condition is `false` and this `else` arm is \
2604+ executed, {} is not initialized",
25972605 self . name
25982606 ) ,
25992607 ) ) ;
@@ -2602,7 +2610,10 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
26022610 ( false , true ) => {
26032611 self . errors . push ( (
26042612 cond. span ,
2605- format ! ( "{} is uninitialized if this condition is met" , self . name) ,
2613+ format ! (
2614+ "if this condition is `true`, {} is not initialized" ,
2615+ self . name
2616+ ) ,
26062617 ) ) ;
26072618 }
26082619 }
@@ -2625,24 +2636,24 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
26252636 self . errors . push ( (
26262637 e. span ,
26272638 format ! (
2628- "{} is uninitialized if the `for` loop runs 0 times" ,
2639+ "if the `for` loop runs 0 times, {} is not initialized " ,
26292640 self . name
26302641 ) ,
26312642 ) ) ;
26322643 } else if let Some ( guard) = & arm. guard {
26332644 self . errors . push ( (
26342645 arm. pat . span . to ( guard. body ( ) . span ) ,
26352646 format ! (
2636- "{} is uninitialized if this pattern and condition are \
2637- matched ",
2647+ "if this pattern and condition are matched, {} is not \
2648+ initialized ",
26382649 self . name
26392650 ) ,
26402651 ) ) ;
26412652 } else {
26422653 self . errors . push ( (
26432654 arm. pat . span ,
26442655 format ! (
2645- "{} is uninitialized if this pattern is matched" ,
2656+ "if this pattern is matched, {} is not initialized " ,
26462657 self . name
26472658 ) ,
26482659 ) ) ;
0 commit comments