Skip to content

Incorrect "consider slicing" suggestion with Vec inside a struct #120605

Closed
@AaronC81

Description

@AaronC81

Code

pub struct Struct {
    a: Vec<Struct>,
}

impl Struct {
    pub fn test(&self) {
        if let [Struct { a: [] }] = &self.a[..] {
            println!("matches!")   
        }
    }
}

Current output

error[E0529]: expected an array or slice, found `Vec<Struct>`
  --> src/main.rs:47:29
   |
47 |         if let [Struct { a: [] }] = &self.a[..] {
   |                             ^^      ----------- help: consider slicing here: `&self.a[..][..]`
   |                             |
   |                             pattern cannot match with input type `Vec<Struct>`

Desired output

error[E0529]: expected an array or slice, found `Vec<Struct>`
  --> src/main.rs:47:29
   |
47 |         if let [Struct { a: [] }] = &self.a[..] {
   |                             ^^
   |                             |
   |                             pattern cannot match with input type `Vec<Struct>`

Rationale and extra context

The suggestion to try &self.a[..][..] does not work, and should not be included. If you do try to follow this suggestion, the error then suggests &self.a[..][..][..], then &self.a[..][..][..][..], and so on.

Other cases

No response

Rust Version

rustc 1.77.0-nightly (88189a71e 2024-01-19)
binary: rustc
commit-hash: 88189a71e4e4376eea82ac61db6a539612eb200a
commit-date: 2024-01-19
host: aarch64-apple-darwin
release: 1.77.0-nightly
LLVM version: 17.0.6

(Also repros on latest nightly using the Playground)

Anything else?

No response

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions