On resolve error of [rest..], suggest [rest @ ..]
When writing a pattern to collect multiple entries of a slice in a
single binding, it is easy to misremember or typo the appropriate syntax
to do so, instead writing the experimental `X..` pattern syntax. When we
encounter a resolve error because `X` isn't available, we suggest
`X @ ..` as an alternative.
```
error[E0425]: cannot find value `rest` in this scope
--> $DIR/range-pattern-meant-to-be-slice-rest-pattern.rs:3:13
|
LL | [1, rest..] => println!("{rest:?}"),
| ^^^^ not found in this scope
|
help: if you meant to collect the rest of the slice in `rest`, use the at operator
|
LL | [1, rest @ ..] => println!("{rest:?}"),
| +
```
Fix #88404.
This commit is contained in:
@@ -603,6 +603,8 @@ struct DiagnosticMetadata<'ast> {
|
||||
/// Only used for better errors on `let <pat>: <expr, not type>;`.
|
||||
current_let_binding: Option<(Span, Option<Span>, Option<Span>)>,
|
||||
|
||||
current_pat: Option<&'ast Pat>,
|
||||
|
||||
/// Used to detect possible `if let` written without `let` and to provide structured suggestion.
|
||||
in_if_condition: Option<&'ast Expr>,
|
||||
|
||||
@@ -703,6 +705,12 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
|
||||
fn visit_expr(&mut self, expr: &'ast Expr) {
|
||||
self.resolve_expr(expr, None);
|
||||
}
|
||||
fn visit_pat(&mut self, p: &'ast Pat) {
|
||||
let prev = self.diagnostic_metadata.current_pat;
|
||||
self.diagnostic_metadata.current_pat = Some(p);
|
||||
visit::walk_pat(self, p);
|
||||
self.diagnostic_metadata.current_pat = prev;
|
||||
}
|
||||
fn visit_local(&mut self, local: &'ast Local) {
|
||||
let local_spans = match local.pat.kind {
|
||||
// We check for this to avoid tuple struct fields.
|
||||
|
||||
Reference in New Issue
Block a user