You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mark functions with uninhabited parameters and match arms on uninhabited patterns as dead code.
Advantage
Uninhabited types can never be constructed and thus the function can never be called - If the function/arm body is non-trivial, this hints at a mistake.
This can come up as a result of implementing a generic trait Trait<Bar: OtherTrait> where the parameter type comes from the OtherTrait. Users may populate the function without noticing Bar's impl of OtherTrait has uninhabited associated types.
Drawbacks
Detecting what function bodies are allowable may be difficult. When the return type is (), an empty block suffices, however any other type may require the match uninhabited {} trick and thus such a body should be allowable without triggering the lint.
Example
fnmake_int(_: !) -> i32{my_important_fn();0}fnmy_match(val:Option<!>) -> i32{match val {None => none_important_fn(),// okSome(_) => some_important_fn(),// dead code!}}
Could be written as:
fnmake_int(val: !) -> i32{
val
}fnmy_match(val:Option<!>) -> i32{match val {None => none_important_fn(),Some(val) => val,}}
Edit: change examples to !
The text was updated successfully, but these errors were encountered:
These functions are useful for being generic or for using traits, #11984 has some more context about when this pattern is useful. I suppose that taking an uninhabited type by value is less useful, but it could still be useful for use in a generic API.
I am aware the existence of trait functions with uninhabited parameters have uses, this suggestion is about accidentally writing a non-trivial body for these implementations, as the implementation code is actually unreachable. I encountered this when working with the wayland_client crate, where an event handler trait must be implemented. Some of these event handlers take an uninhabited enum to describe the event, and I made the mistake of writing important logic in such a function and it was silently accepted!
Uh oh!
There was an error while loading. Please reload this page.
What it does
Mark functions with uninhabited parameters and match arms on uninhabited patterns as dead code.
Advantage
Uninhabited types can never be constructed and thus the function can never be called - If the function/arm body is non-trivial, this hints at a mistake.
This can come up as a result of implementing a generic trait
Trait<Bar: OtherTrait>
where the parameter type comes from theOtherTrait
. Users may populate the function without noticing Bar's impl ofOtherTrait
has uninhabited associated types.Drawbacks
Detecting what function bodies are allowable may be difficult. When the return type is
()
, an empty block suffices, however any other type may require thematch uninhabited {}
trick and thus such a body should be allowable without triggering the lint.Example
Could be written as:
Edit: change examples to
!
The text was updated successfully, but these errors were encountered: