Skip to content

Diagnostic on missing "?" suggests to ignore the error entirely, which can be problematic #130262

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

Open
RalfJung opened this issue Sep 12, 2024 · 2 comments
Assignees
Labels
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.

Comments

@RalfJung
Copy link
Member

RalfJung commented Sep 12, 2024

Code

fn fallible() -> Result<i32, ()> {
    Ok(42)
}

pub fn caller() -> Result<(), ()> {
    fallible();
    Ok(())
}

Current output

warning: unused `Result` that must be used
 --> src/lib.rs:6:5
  |
6 |     fallible();
  |     ^^^^^^^^^^
  |
  = note: this `Result` may be an `Err` variant, which should be handled
  = note: `#[warn(unused_must_use)]` on by default
help: use `let _ = ...` to ignore the resulting value
  |
6 |     let _ = fallible();
  |     +++++++

Desired output

Rust should suggest to add the missing `?`, not to ignore the error entirely.

Rationale and extra context

See rust-lang/miri#3855 for an example where ignoring the error with let _ = can lead to critical bugs.

Other cases

No response

Rust Version

1.81.0

Anything else?

No response

@RalfJung RalfJung 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 Sep 12, 2024
@roife
Copy link
Member

roife commented Sep 13, 2024

@rustbot claim

@roife
Copy link
Member

roife commented Sep 14, 2024

In functions that return Result<>, handling it this way feels quite natural. However, doing so in functions that don't return Result<> seems a bit weird.

I'm wondering how we handle the latter case: should we keep using let _ =, or recommend users to handle with a match { Ok(_) => {} Err(_) => {} } clause?

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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants