-
Notifications
You must be signed in to change notification settings - Fork 14.1k
unused_must_use: Don't warn on Result<(), Uninhabited> or ControlFlow<Uninhabited, ()>
#147382
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
Merged
bors
merged 7 commits into
rust-lang:master
from
joshtriplett:unused-must-use-ignore-result-unit-uninhabited
Oct 20, 2025
+206
−7
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2997070
unused_must_use: Factor out a variable for the parent module DefId
joshtriplett 6f89cec
unused_must_use: Don't warn on `Result<(), Uninhabited>`
joshtriplett efd76c9
unused_must_use: Add test for a method using an associated error type
joshtriplett 39f59bf
unused_must_use: Suppress warnings for `ControlFlow<Uninhabited, ()>`…
joshtriplett 339caa1
unused_must_use: Rename test functions to reflect what they test
joshtriplett 2d95dfd
unused_must_use: Add extra test for types that are still generic
niacdoial 7aa7ecc
Factor out a helper function to check if a type is uninhabited
joshtriplett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
tests/ui/lint/unused/auxiliary/must_use_result_unit_uninhabited_extern_crate.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| pub enum MyUninhabited {} | ||
|
|
||
| #[non_exhaustive] | ||
| pub enum MyUninhabitedNonexhaustive {} |
101 changes: 101 additions & 0 deletions
101
tests/ui/lint/unused/must_use-result-unit-uninhabited.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| //@ edition: 2024 | ||
| //@ aux-crate:dep=must_use_result_unit_uninhabited_extern_crate.rs | ||
|
|
||
| #![deny(unused_must_use)] | ||
| #![feature(never_type)] | ||
|
|
||
| use core::ops::{ControlFlow, ControlFlow::Continue}; | ||
| use dep::{MyUninhabited, MyUninhabitedNonexhaustive}; | ||
|
|
||
| fn result_unit_unit() -> Result<(), ()> { | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn result_unit_infallible() -> Result<(), core::convert::Infallible> { | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn result_unit_never() -> Result<(), !> { | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn result_unit_myuninhabited() -> Result<(), MyUninhabited> { | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn result_unit_myuninhabited_nonexhaustive() -> Result<(), MyUninhabitedNonexhaustive> { | ||
| Ok(()) | ||
| } | ||
|
|
||
| trait AssocType { | ||
| type Error; | ||
| } | ||
|
|
||
| struct S1; | ||
| impl AssocType for S1 { | ||
| type Error = !; | ||
| } | ||
|
|
||
| struct S2; | ||
| impl AssocType for S2 { | ||
| type Error = (); | ||
| } | ||
|
|
||
| fn result_unit_assoctype<AT: AssocType>(_: AT) -> Result<(), AT::Error> { | ||
| Ok(()) | ||
| } | ||
|
|
||
| trait UsesAssocType { | ||
| type Error; | ||
| fn method_use_assoc_type(&self) -> Result<(), Self::Error>; | ||
| } | ||
|
|
||
| impl UsesAssocType for S1 { | ||
| type Error = !; | ||
| fn method_use_assoc_type(&self) -> Result<(), Self::Error> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl UsesAssocType for S2 { | ||
| type Error = (); | ||
| fn method_use_assoc_type(&self) -> Result<(), Self::Error> { | ||
| Err(()) | ||
| } | ||
| } | ||
|
|
||
| fn controlflow_unit() -> ControlFlow<()> { | ||
| Continue(()) | ||
| } | ||
|
|
||
| fn controlflow_infallible_unit() -> ControlFlow<core::convert::Infallible, ()> { | ||
| Continue(()) | ||
| } | ||
|
|
||
| fn controlflow_never() -> ControlFlow<!> { | ||
| Continue(()) | ||
| } | ||
|
|
||
| fn main() { | ||
| result_unit_unit(); //~ ERROR: unused `Result` that must be used | ||
| result_unit_infallible(); | ||
| result_unit_never(); | ||
| result_unit_myuninhabited(); | ||
| result_unit_myuninhabited_nonexhaustive(); //~ ERROR: unused `Result` that must be used | ||
| result_unit_assoctype(S1); | ||
| result_unit_assoctype(S2); //~ ERROR: unused `Result` that must be used | ||
| S1.method_use_assoc_type(); | ||
| S2.method_use_assoc_type(); //~ ERROR: unused `Result` that must be used | ||
|
|
||
| controlflow_unit(); //~ ERROR: unused `ControlFlow` that must be used | ||
| controlflow_infallible_unit(); | ||
| controlflow_never(); | ||
| } | ||
|
|
||
| trait AssocTypeBeforeMonomorphisation { | ||
| type Error; | ||
| fn generate(&self) -> Result<(), Self::Error>; | ||
| fn process(&self) { | ||
| self.generate(); //~ ERROR: unused `Result` that must be used | ||
| } | ||
| } |
78 changes: 78 additions & 0 deletions
78
tests/ui/lint/unused/must_use-result-unit-uninhabited.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| error: unused `Result` that must be used | ||
| --> $DIR/must_use-result-unit-uninhabited.rs:80:5 | ||
| | | ||
| LL | result_unit_unit(); | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `Result` may be an `Err` variant, which should be handled | ||
| note: the lint level is defined here | ||
| --> $DIR/must_use-result-unit-uninhabited.rs:4:9 | ||
| | | ||
| LL | #![deny(unused_must_use)] | ||
| | ^^^^^^^^^^^^^^^ | ||
| help: use `let _ = ...` to ignore the resulting value | ||
| | | ||
| LL | let _ = result_unit_unit(); | ||
| | +++++++ | ||
|
|
||
| error: unused `Result` that must be used | ||
| --> $DIR/must_use-result-unit-uninhabited.rs:84:5 | ||
| | | ||
| LL | result_unit_myuninhabited_nonexhaustive(); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `Result` may be an `Err` variant, which should be handled | ||
| help: use `let _ = ...` to ignore the resulting value | ||
| | | ||
| LL | let _ = result_unit_myuninhabited_nonexhaustive(); | ||
| | +++++++ | ||
|
|
||
| error: unused `Result` that must be used | ||
| --> $DIR/must_use-result-unit-uninhabited.rs:86:5 | ||
| | | ||
| LL | result_unit_assoctype(S2); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `Result` may be an `Err` variant, which should be handled | ||
| help: use `let _ = ...` to ignore the resulting value | ||
| | | ||
| LL | let _ = result_unit_assoctype(S2); | ||
| | +++++++ | ||
|
|
||
| error: unused `Result` that must be used | ||
| --> $DIR/must_use-result-unit-uninhabited.rs:88:5 | ||
| | | ||
| LL | S2.method_use_assoc_type(); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `Result` may be an `Err` variant, which should be handled | ||
| help: use `let _ = ...` to ignore the resulting value | ||
| | | ||
| LL | let _ = S2.method_use_assoc_type(); | ||
| | +++++++ | ||
|
|
||
| error: unused `ControlFlow` that must be used | ||
| --> $DIR/must_use-result-unit-uninhabited.rs:90:5 | ||
| | | ||
| LL | controlflow_unit(); | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: use `let _ = ...` to ignore the resulting value | ||
| | | ||
| LL | let _ = controlflow_unit(); | ||
| | +++++++ | ||
|
|
||
| error: unused `Result` that must be used | ||
| --> $DIR/must_use-result-unit-uninhabited.rs:99:9 | ||
| | | ||
| LL | self.generate(); | ||
| | ^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this `Result` may be an `Err` variant, which should be handled | ||
| help: use `let _ = ...` to ignore the resulting value | ||
| | | ||
| LL | let _ = self.generate(); | ||
| | +++++++ | ||
|
|
||
| error: aborting due to 6 previous errors | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why this checks for
T = ()?Result<T, !>is basicallyT, so I'd expect this to not check for(). In #147854, which I opened because I haven't seen this PR, I just returnis_ty_must_use(cx, args.type_at(0), expr, span)here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@WaffleLapkin I can see the argument for that, but it'd be less clear-cut.
Result<(), !>has only one possible value, so there's zero value in checking it at all, and it seems safe to unconditionally make it ignoremust_use. Something likeResult<u32, !>, on the other hand, would currently be flagged by themust_useonResult, and it would be a potential surprise to people to stop doing that; people might be expecting to have to capture the return value withlet Ok(value) = ..., for instance.If we were going to potentially suppress warnings on
Result<T, !>, I think that'd need a separate FCP. We might also want to let people know that they may want to mark the function#[must_use]if they were previously counting on theResultto do that.Separately from that, if we're checking for
Tbeing#[must_use], it'd be nice to have a design where a function returningResult<T, E>produces anunused_must_usewarning if you apply a?and then ignore theT. But that doesn't have to happen right away, and I'm all for incremental results.One way or another, though, I think that would be an additional change requiring an additional FCP.