Skip to content

Tracking issue for release notes of #147382: unused_must_use: Don't warn on Result<(), Uninhabited> #147383

@rustbot

Description

@rustbot

This issue tracks the release notes text for #147382.

cc @joshtriplett, @fmease -- original issue/PR authors and assignees for drafting text

See the forge.rust-lang.org chapter about release notes for an overview of how the release team makes use of these tracking issues.

Release notes text

This section should be edited to specify the correct category(s) for the change, with succinct description(s) of what changed. Some things worth considering:

  • Does this need an additional compat notes section?
  • Was this a libs stabilization that should have additional headers to list new APIs under Stabilized APIs and Const Stabilized APIs?
# Language
- The `unused_must_use` lint no longer warns on `Result<(), Uninhabited>` (for instance, `Result<(), !>`). This avoids having to check for an error that can never happen. [unused_must_use: Don't warn on `Result<(), Uninhabited>`](https://github.com/rust-lang/rust/pull/147382)

Tip

Use the previous releases for inspiration on how to write the release notes text and which categories to pick.

Release blog section

If this change is notable enough for inclusion in the blog post then this section should be edited to contain a draft for the blog post. Otherwise leave it empty.

### `unused_must_use` no longer warns about `Result<(), Uninhabited>`

Rust's `unused_must_use` lint warns when ignoring the return value of a function, if the function or its return type is annotated with `#[must_use]`. For instance, this warns if ignoring a return type of `Result`, to remind you to use `?`, or something like `.expect("...")`.

However, some functions return `Result`, but the error type they use is not actually "inhabited", meaning it can never exist in real code (e.g. the [`!`](https://doc.rust-lang.org/std/primitive.never.html) or [`Infallible`](https://doc.rust-lang.org/std/convert/enum.Infallible.html) types).

The `unused_must_use` lint now no longer warns on `Result<(), Uninhabited>`. For instance, it will not warn on `Result<(), !>`. This avoids having to check for an error that can never happen.

```rust
fn can_never_fail() -> Result<(), !> {
    // ...
    Ok(())
}

fn main() {
    can_never_fail()
}
```

This is particularly useful with the common pattern of a trait with an associated error type, where the error type may *sometimes* be infallible:

```rust
trait UsesAssocErrorType {
    type Error;
    fn method(&self) -> Result<(), Self::Error>;
}

struct CannotFail;
impl UsesAssocErrorType for CannotFail {
    type Error = !;
    fn method(&self) -> Result<(), Self::Error> {
        Ok(())
    }
}

struct CanFail;
impl UsesAssocErrorType for CanFail {
    type Error = std::io::Error;
    fn method(&self) -> Result<(), Self::Error> {
        Err(std::io::Error::other("something went wrong"))
    }
}

fn main() {
    CannotFail.method(); // No error
    CanFail.method(); // Error: unused `Result` that must be used
}
```

Note

If a blog post section is required the release-blog-post label should be added (@rustbot label +release-blog-post) to this issue as otherwise it may be missed by the release team.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamneeds-triageThis issue may need triage. Remove it if it has been sufficiently triaged.release-blog-postMarks issues tracking what text to put in the release blog post.relnotesMarks issues that should be documented in the release notes of the next release.relnotes-tracking-issueMarks issues tracking what text to put in release notes.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions