rust-lang / rust-lang/rust

E0529 wrong infinite suggestion to add `as_deref`

Open
#130,243 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
use std::io::ErrorKind;
use std::borrow::Cow;

fn f(x: Result<i32, Vec<ErrorKind>>) -> bool {
    matches!(x, Err([ErrorKind::NotFound]))
}
Current output
error[E0529]: expected an array or slice, found `Vec<ErrorKind>`
 --> src/lib.rs:5:21
  |
5 |     matches!(x, Err([ErrorKind::NotFound]))
  |                     ^^^^^^^^^^^^^^^^^^^^^ pattern cannot match with input type `Vec<ErrorKind>`
  |
help: consider using `as_deref` here
  |
5 |     matches!(x.as_deref(), Err([ErrorKind::NotFound]))
  |               +++++++++++
Desired output

no suggestion to use as_deref

Rationale and extra context
  1. as_deref doesn't change the meaning of the code.
  2. When applied, rustc will suggest to apply it again, which is absurd and confusing.
Other cases
matches!(x.as_deref().as_deref().as_deref(), Err([ErrorKind::NotFound]))

will suggest

help: consider using `as_deref` here
  |
5 |     matches!(x.as_deref().as_deref().as_deref().as_deref(), Err([ErrorKind::NotFound]))
  |                                                +++++++++++
Rust Version
rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied Rust reproducer and investigate the rustc E0529 diagnostic path that proposes as_deref; the issue names no source file or test. Done means the compiler no longer emits that suggestion for the shown case or repeated as_deref calls, verified by rerunning the reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.