for init_idx in inits {
let init = &self.move_data.inits[*init_idx];
let span = init.span(&self.body);
- spans.push(span);
+ if !span.is_dummy() {
+ spans.push(span);
+ }
}
let (binding, name, desc) =
Some(name) => (format!("`{name}`"), format!("`{name}`"), format!("`{name}` ")),
None => ("value".to_string(), "the variable".to_string(), String::new()),
};
- let initialized = if let InitializationRequiringAction::PartialAssignment = desired_action {
- // The same error is emitted for bindings that are *sometimes* initialized and the ones
- // that are *partially* initialized by assigning to a field of an uninitialized
- // binding. We differentiate between them for more accurate wording here.
- "fully initialized"
- } else if spans.iter().filter(|i| !i.contains(span)).count() == 0 {
- // We filter above to avoid misleading wording in cases like:
- // ```
- // let x;
- // x += 1;
- // ```
- "initialized"
- } else {
- "initialized in all conditions"
- };
+ let isnt_initialized =
+ if let InitializationRequiringAction::PartialAssignment = desired_action {
+ // The same error is emitted for bindings that are *sometimes* initialized and the ones
+ // that are *partially* initialized by assigning to a field of an uninitialized
+ // binding. We differentiate between them for more accurate wording here.
+ "isn't fully initialized"
+ } else if spans.iter().filter(|i| !i.contains(span)).count() == 0 {
+ // We filter above to avoid misleading wording in cases like the following, where `x`
+ // has an `init`, but it is in the same place we're looking at:
+ // ```
+ // let x;
+ // x += 1;
+ // ```
+ "isn't initialized"
+ } else {
+ "is possibly-uninitialized"
+ };
let used = desired_action.as_general_verb_in_past_tense();
let mut err =
- struct_span_err!(self, span, E0381, "{used} binding {desc}isn't {initialized}");
+ struct_span_err!(self, span, E0381, "{used} binding {desc}{isnt_initialized}");
use_spans.var_span_label_path_only(
&mut err,
format!("{} occurs due to use{}", desired_action.as_noun(), use_spans.describe()),
default value and mutate it, or use `std::mem::MaybeUninit`",
);
}
- err.span_label(span, format!("{binding} {used} here but it isn't {initialized}"));
+ err.span_label(span, format!("{binding} {used} here but it {isnt_initialized}"));
// We use the statements were the binding was initialized, and inspect the HIR to look
// for the branching codepaths that aren't covered, to point at them.
v.visit_expr(body);
if v.1 {
self.errors.push((
- ex.span.to(cond.span),
+ cond.span,
format!(
- "this `if` expression might be missing an `else` arm that initializes \
- {}",
+ "if this `if` condition is `false`, {} is not initialized",
self.name,
),
));
+ self.errors.push((
+ ex.span.shrink_to_hi(),
+ format!("an `else` arm might be missing here, initializing {}", self.name),
+ ));
}
}
hir::ExprKind::If(cond, body, Some(other)) => {
self.errors.push((
cond.span,
format!(
- "{} is uninitialized if this condition isn't met and the \
- `while` loop runs 0 times",
+ "if this condition isn't met and the `while` loop runs 0 \
+ times, {} is not initialized",
self.name
),
));
self.errors.push((
body.span.shrink_to_hi().until(other.span),
format!(
- "{} is uninitialized if this `else` arm is executed",
+ "if the `if` condition is `false` and this `else` arm is \
+ executed, {} is not initialized",
self.name
),
));
(false, true) => {
self.errors.push((
cond.span,
- format!("{} is uninitialized if this condition is met", self.name),
+ format!(
+ "if this condition is `true`, {} is not initialized",
+ self.name
+ ),
));
}
}
self.errors.push((
e.span,
format!(
- "{} is uninitialized if the `for` loop runs 0 times",
+ "if the `for` loop runs 0 times, {} is not initialized ",
self.name
),
));
self.errors.push((
arm.pat.span.to(guard.body().span),
format!(
- "{} is uninitialized if this pattern and condition are \
- matched",
+ "if this pattern and condition are matched, {} is not \
+ initialized",
self.name
),
));
self.errors.push((
arm.pat.span,
format!(
- "{} is uninitialized if this pattern is matched",
+ "if this pattern is matched, {} is not initialized",
self.name
),
));
-error[E0381]: used binding `y` isn't initialized in all conditions
+error[E0381]: used binding `y` is possibly-uninitialized
--> $DIR/no-non-guaranteed-initialization.rs:9:5
|
LL | let y;
| - binding declared here but left uninitialized
LL | if x > 5 {
- | ----- this `if` expression might be missing an `else` arm that initializes `y`
-...
+ | ----- if this `if` condition is `false`, `y` is not initialized
+LL | y = echo(10).await;
+LL | }
+ | - an `else` arm might be missing here, initializing `y`
LL | y
- | ^ `y` used here but it isn't initialized in all conditions
+ | ^ `y` used here but it is possibly-uninitialized
error: aborting due to previous error
-error[E0381]: used binding `i` isn't initialized in all conditions
+error[E0381]: used binding `i` is possibly-uninitialized
--> $DIR/borrowck-and-init.rs:5:20
|
LL | let i: isize;
LL | println!("{}", false && { i = 5; true });
| ----- binding initialized here in some conditions
LL | println!("{}", i);
- | ^ `i` used here but it isn't initialized in all conditions
+ | ^ `i` used here but it is possibly-uninitialized
|
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
-error[E0381]: used binding `x` isn't initialized in all conditions
+error[E0381]: used binding `x` is possibly-uninitialized
--> $DIR/borrowck-if-no-else.rs:5:9
|
LL | let x: isize; if 1 > 2 { x = 10; }
- | - ----- this `if` expression might be missing an `else` arm that initializes `x`
- | |
+ | - ----- - an `else` arm might be missing here, initializing `x`
+ | | |
+ | | if this `if` condition is `false`, `x` is not initialized
| binding declared here but left uninitialized
LL | foo(x);
- | ^ `x` used here but it isn't initialized in all conditions
+ | ^ `x` used here but it is possibly-uninitialized
error: aborting due to previous error
-error[E0381]: used binding `x` isn't initialized in all conditions
+error[E0381]: used binding `x` is possibly-uninitialized
--> $DIR/borrowck-if-with-else.rs:10:9
|
LL | let x: isize;
| - binding declared here but left uninitialized
LL | if 1 > 2 {
- | ----- `x` is uninitialized if this condition is met
+ | ----- if this condition is `true`, `x` is not initialized
...
LL | foo(x);
- | ^ `x` used here but it isn't initialized in all conditions
+ | ^ `x` used here but it is possibly-uninitialized
error: aborting due to previous error
-error[E0381]: used binding `i` isn't initialized in all conditions
+error[E0381]: used binding `i` is possibly-uninitialized
--> $DIR/borrowck-or-init.rs:5:20
|
LL | let i: isize;
LL | println!("{}", false || { i = 5; true });
| ----- binding initialized here in some conditions
LL | println!("{}", i);
- | ^ `i` used here but it isn't initialized in all conditions
+ | ^ `i` used here but it is possibly-uninitialized
|
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
-error[E0381]: used binding `v` isn't initialized in all conditions
+error[E0381]: used binding `v` is possibly-uninitialized
--> $DIR/borrowck-while-break.rs:7:20
|
LL | let v;
| - binding declared here but left uninitialized
LL | while cond {
- | ---- `v` is uninitialized if this condition isn't met and the `while` loop runs 0 times
+ | ---- if this condition isn't met and the `while` loop runs 0 times, `v` is not initialized
...
LL | println!("{}", v);
- | ^ `v` used here but it isn't initialized in all conditions
+ | ^ `v` used here but it is possibly-uninitialized
|
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
-error[E0381]: used binding `x` isn't initialized in all conditions
+error[E0381]: used binding `x` is possibly-uninitialized
--> $DIR/borrowck-while.rs:4:12
|
LL | let mut x: isize;
| ----- binding declared here but left uninitialized
LL | while 1 == 1 { x = 10; }
- | ------ `x` is uninitialized if this condition isn't met and the `while` loop runs 0 times
+ | ------ if this condition isn't met and the `while` loop runs 0 times, `x` is not initialized
LL | return x;
- | ^ `x` used here but it isn't initialized in all conditions
+ | ^ `x` used here but it is possibly-uninitialized
error: aborting due to previous error
-error[E0381]: used binding `x` isn't initialized in all conditions
+error[E0381]: used binding `x` is possibly-uninitialized
--> $DIR/match-cfg-fake-edges.rs:21:13
|
LL | let x;
| - binding declared here but left uninitialized
...
LL | x;
- | ^ `x` used here but it isn't initialized in all conditions
+ | ^ `x` used here but it is possibly-uninitialized
error[E0382]: use of moved value: `x`
--> $DIR/match-cfg-fake-edges.rs:35:13
-error[E0381]: used binding `z` isn't initialized in all conditions
+error[E0381]: used binding `z` is possibly-uninitialized
--> $DIR/chains-without-let.rs:3:34
|
LL | let z;
| - binding declared here but left uninitialized
LL | if true && { z = 3; true} && z == 3 {}
- | ----- ^ `z` used here but it isn't initialized in all conditions
+ | ----- ^ `z` used here but it is possibly-uninitialized
| |
| binding initialized here in some conditions
-error[E0381]: used binding `z` isn't initialized in all conditions
+error[E0381]: used binding `z` is possibly-uninitialized
--> $DIR/chains-without-let.rs:9:31
|
LL | let z;
| - binding declared here but left uninitialized
LL | true && { z = 3; true} && z == 3;
- | ----- ^ `z` used here but it isn't initialized in all conditions
+ | ----- ^ `z` used here but it is possibly-uninitialized
| |
| binding initialized here in some conditions
-error[E0381]: used binding `z` isn't initialized in all conditions
+error[E0381]: used binding `z` is possibly-uninitialized
--> $DIR/chains-without-let.rs:15:36
|
LL | let z;
| - binding declared here but left uninitialized
LL | if false || { z = 3; false} || z == 3 {}
- | ----- ^ `z` used here but it isn't initialized in all conditions
+ | ----- ^ `z` used here but it is possibly-uninitialized
| |
| binding initialized here in some conditions
-error[E0381]: used binding `cfg_res` isn't initialized in all conditions
+error[E0381]: used binding `cfg_res` is possibly-uninitialized
--> $DIR/try-block-opt-init.rs:15:5
|
LL | let cfg_res;
| ----------- binding initialized here in some conditions
...
LL | assert_eq!(cfg_res, 5);
- | ^^^^^^^^^^^^^^^^^^^^^^ `cfg_res` used here but it isn't initialized in all conditions
+ | ^^^^^^^^^^^^^^^^^^^^^^ `cfg_res` used here but it is possibly-uninitialized
|
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)