Skip to content

Commit

Permalink
Suppress erroneous suggestion
Browse files Browse the repository at this point in the history
The suggestion to use `let else` with an uninitialized refutable `let`
statement was erroneous: `let else` cannot be used with deferred
initialization.
  • Loading branch information
eggyal committed Apr 12, 2024
1 parent ab71ee7 commit ddcfb94
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 48 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
if let Some(span) = sp
&& self.tcx.sess.source_map().is_span_accessible(span)
&& interpreted_as_const.is_none()
&& scrut.is_some()
{
let mut bindings = vec![];
pat.each_binding(|name, _, _, _| bindings.push(name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ LL | mac!(0);
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `i32`
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might want to use `if let` to ignore the variant that isn't matched
|
LL | if let ...$e; { todo!() }
| ++ +++++++++++

error: aborting due to 6 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ LL | mac!(0);
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `i32`
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might want to use `if let` to ignore the variant that isn't matched
|
LL | if let $e...; { todo!() }
| ++ +++++++++++

error[E0005]: refutable pattern in local binding
--> $DIR/half-open-range-pats-inclusive-no-end.rs:20:17
Expand All @@ -85,10 +81,6 @@ LL | mac!(0);
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `i32`
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might want to use `if let` to ignore the variant that isn't matched
|
LL | if let $e..=; { todo!() }
| ++ +++++++++++

error: aborting due to 8 previous errors

Expand Down
8 changes: 8 additions & 0 deletions tests/ui/let-else/uninitialized-refutable-let-issue-123844.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// https://github.com/rust-lang/rust/issues/123844
// An uninitialized refutable let should not suggest `let else`, as it can't be used with deferred
// initialization.

fn main() {
let Some(x); //~ ERROR refutable pattern in local binding
x = 1;
}
13 changes: 13 additions & 0 deletions tests/ui/let-else/uninitialized-refutable-let-issue-123844.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0005]: refutable pattern in local binding
--> $DIR/uninitialized-refutable-let-issue-123844.rs:6:9
|
LL | let Some(x);
| ^^^^^^^ pattern `None` not covered
|
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `Option<i32>`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0005`.
36 changes: 0 additions & 36 deletions tests/ui/parser/recover/recover-range-pats.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,6 @@ LL | mac2!(0, 1);
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `i32`
= note: this error originates in the macro `mac2` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might want to use `if let` to ignore the variants that aren't matched
|
LL | if let $e1..$e2; { todo!() }
| ++ +++++++++++

error[E0005]: refutable pattern in local binding
--> $DIR/recover-range-pats.rs:138:17
Expand All @@ -509,10 +505,6 @@ LL | mac2!(0, 1);
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `i32`
= note: this error originates in the macro `mac2` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might want to use `if let` to ignore the variants that aren't matched
|
LL | if let $e1...$e2; { todo!() }
| ++ +++++++++++

error[E0005]: refutable pattern in local binding
--> $DIR/recover-range-pats.rs:142:17
Expand All @@ -527,10 +519,6 @@ LL | mac2!(0, 1);
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `i32`
= note: this error originates in the macro `mac2` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might want to use `if let` to ignore the variants that aren't matched
|
LL | if let $e1..=$e2; { todo!() }
| ++ +++++++++++

error[E0005]: refutable pattern in local binding
--> $DIR/recover-range-pats.rs:151:17
Expand All @@ -545,10 +533,6 @@ LL | mac!(0);
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `i32`
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might want to use `if let` to ignore the variant that isn't matched
|
LL | if let ..$e; { todo!() }
| ++ +++++++++++

error[E0005]: refutable pattern in local binding
--> $DIR/recover-range-pats.rs:153:17
Expand All @@ -563,10 +547,6 @@ LL | mac!(0);
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `i32`
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might want to use `if let` to ignore the variant that isn't matched
|
LL | if let ...$e; { todo!() }
| ++ +++++++++++

error[E0005]: refutable pattern in local binding
--> $DIR/recover-range-pats.rs:156:17
Expand All @@ -581,10 +561,6 @@ LL | mac!(0);
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `i32`
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might want to use `if let` to ignore the variant that isn't matched
|
LL | if let ..=$e; { todo!() }
| ++ +++++++++++

error[E0005]: refutable pattern in local binding
--> $DIR/recover-range-pats.rs:158:17
Expand All @@ -599,10 +575,6 @@ LL | mac!(0);
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `i32`
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might want to use `if let` to ignore the variant that isn't matched
|
LL | if let $e..; { todo!() }
| ++ +++++++++++

error[E0005]: refutable pattern in local binding
--> $DIR/recover-range-pats.rs:160:17
Expand All @@ -617,10 +589,6 @@ LL | mac!(0);
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `i32`
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might want to use `if let` to ignore the variant that isn't matched
|
LL | if let $e...; { todo!() }
| ++ +++++++++++

error[E0005]: refutable pattern in local binding
--> $DIR/recover-range-pats.rs:162:17
Expand All @@ -635,10 +603,6 @@ LL | mac!(0);
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `i32`
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might want to use `if let` to ignore the variant that isn't matched
|
LL | if let $e..=; { todo!() }
| ++ +++++++++++

error: aborting due to 69 previous errors

Expand Down

0 comments on commit ddcfb94

Please sign in to comment.