Skip to content

Commit f9b0c77

Browse files
committed
Add must_use exception for infallible results
On lang, we recently decided (in rust-lang/rust#147382) that `Result<(), E>` and `ControlFlow<B, ()>` should not trigger the `unused_must_use` lint when the error or break type is uninhabited. The rationale is that an infallible result carries nothing the caller needs to handle. Let's document this.
1 parent 0775ef3 commit f9b0c77

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/attributes/diagnostics.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,19 @@ struct MustUse();
395395
MustUse(); // ERROR: Unused value that must be used.
396396
```
397397
398+
r[attributes.diagnostics.must_use.type.uninhabited]
399+
As an exception to [attributes.diagnostics.must_use.type], the lint does not fire for `Result<(), E>` when `E` is [uninhabited] or for `ControlFlow<B, ()>` when `B` is [uninhabited]. A `#[non_exhaustive]` type from an external crate is not considered uninhabited for this purpose, because it may gain constructors in the future.
400+
401+
```rust
402+
#![deny(unused_must_use)]
403+
# use core::ops::ControlFlow;
404+
enum Empty {}
405+
fn f1() -> Result<(), Empty> { Ok(()) }
406+
f1(); // OK: `Empty` is uninhabited.
407+
fn f2() -> ControlFlow<Empty, ()> { ControlFlow::Continue(()) }
408+
f2(); // OK: `Empty` is uninhabited.
409+
```
410+
398411
r[attributes.diagnostics.must_use.fn]
399412
If the [expression] of an [expression statement] is a [call expression] or [method call expression] whose function operand is a function to which the attribute is applied, the use triggers the `unused_must_use` lint.
400413

@@ -757,4 +770,5 @@ The first error message includes a somewhat confusing error message about the re
757770
[trait item]: ../items/traits.md
758771
[trait-impl]: ../items/implementations.md#trait-implementations
759772
[traits]: ../items/traits.md
773+
[uninhabited]: glossary.uninhabited
760774
[union]: ../items/unions.md

0 commit comments

Comments
 (0)