Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect slicing suggestion for pattern matching against a Vec inside a Result or Option #91328

Closed
edmorley opened this issue Nov 28, 2021 · 0 comments · Fixed by #91343
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@edmorley
Copy link
Contributor

edmorley commented Nov 28, 2021

When trying to pattern match against a Vec inside a Result or Option, an incorrect suggestion is given, that says to append [..] to the variable to slice it, which won't work since the variable is of type Result / Option rather than being a Vec itself.

Instead one option is to use .as_deref() on the Result:
https://doc.rust-lang.org/std/result/enum.Result.html#method.as_deref

Given the following code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1e834c61673228c36f08e8c294efc68d

fn main() {
    let s = "1:2";
    let result: Result<Vec<u64>, _> = s.split(':').map(str::parse).collect();

    match result {
        Ok([first, second]) => {
            println!("first: {}, second: {}", first, second);
        }
        _ => {}
    }
}

The current output is:

error[E0529]: expected an array or slice, found `Vec<u64>`
 --> src/main.rs:6:12
  |
5 |     match result {
  |           ------ help: consider slicing here: `result[..]`
6 |         Ok([first, second]) => {
  |            ^^^^^^^^^^^^^^^ pattern cannot match with input type `Vec<u64>`

Ideally the output should look like:

error[E0529]: expected an array or slice, found `Vec<u64>`
 --> src/main.rs:6:12
  |
5 |     match result {
  |           ------ help: consider converting the contents of the Result to a slice using: `result.as_deref()`
6 |         Ok([first, second]) => {
  |            ^^^^^^^^^^^^^^^ pattern cannot match with input type `Vec<u64>`
@edmorley edmorley added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 28, 2021
@inquisitivecrystal inquisitivecrystal added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Nov 29, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 31, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
…=jackh726

Fix suggestion to slice if scrutinee is a `Result` or `Option`

Fixes rust-lang#91328.
@bors bors closed this as completed in 5159c01 Feb 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants