Skip to content

Remove unhelpful diagnostic when we suggesting for if-let #103909

Closed
@chenyukang

Description

@chenyukang

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

#![allow(unused_variables)]
use std::fs::File;

fn main() {
    if Err(err) = File::open("hello.txt") {
        
    }
}

The current output is:

error[E0425]: cannot find value `err` in this scope
 --> src/main.rs:5:12
  |
5 |     if Err(err) = File::open("hello.txt") {
  |            ^^^
  |
help: you might have meant to use pattern matching
  |
5 |     if let Err(err) = File::open("hello.txt") {
  |        +++
help: a tuple variant with a similar name exists
  |
5 |     if Err(Err) = File::open("hello.txt") {
  |            ~~~
help: consider importing one of these items
  |
2 | use futures::future::err;
  |
2 | use futures_0_1_31::future::err;
  |
2 | use futures_util::future::err;
  |

error[E0308]: mismatched types
 --> src/main.rs:5:8
  |
5 |     if Err(err) = File::open("hello.txt") {
  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`

Ideally the output should look like:

error[E0425]: cannot find value `err` in this scope
 --> src/main.rs:5:12
  |
5 |     if Err(err) = File::open("hello.txt") {
  |            ^^^
  |
help: you might have meant to use pattern matching
  |
5 |     if let Err(err) = File::open("hello.txt") {
  |        +++

How do we remove other diagnostics in this scenario?

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-verboseDiagnostics: Too much output caused by a single piece of 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