-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
uninhabited_references
is very likely to be a false positive
#11984
Comments
Also a different perspective: since |
T-opsem is debating the validity of references to uninhabited types at length. (I agree that this lint should not be firing on |
I agree that this is a false positive. The lint should be disabled, or at least permit code in the form This pattern is used in a lot of places and as far as I know it is the idiomatic way of telling the compiler "this function cannot be called and all references to it should be optimized away". I do not know of any alternatives to this syntax in safe rust. This pattern is especially useful when you have a lot of generic trait impls such as We have one such example in planus where we are currently considering disabling this lint. |
As another data point, this is the implementation of every trait on |
I'm reopening this issue, as the linked PR didn't actually resolve it but made the lint allow.by-default instead |
This issue's title should be edited to add the |
uninhabited_reference
is very likely to be a false positiveuninhabited_references
is very likely to be a false positive
another case (minimized): use core::convert::Infallible;
#[warn(clippy::uninhabited_references)]
pub fn ignore_infallible(x: &Result<u64, Infallible>) -> u64 {
match x {
Ok(v) => v + 1,
Err(e) => {
match *e {
}
}
}
} the lint does not like deref:
|
I will look into this once I get some time, I think we agreed that (at least) matching on uninhabited types itself is not UB (but creating reference to those is UB), so that entire operation must be problematic before it; implies that match should not trigger lint. |
@rustbot claim |
EDIT: I checked some things in more depth, and it is invalid to create such a reference, sorry for the confusion. |
Summary
The new
uninhabited_reference
lint complains about deferencing a reference to an infallible type, saying this is UB. But it is possible to write such code in safe Rust, which will be provably impossible to call (absent unsafe code which itself is almost certainly incorrect).The lint purpose would be better served by linting the creation of a reference to an uninhabited type. The original issue has some discussion saying that it's maybe not UB, but it's almost certainly wrong. Versus the dereference, which is safe to write and has legitimate usecases.
At the very least, the existing lint shouldn't trigger on trait implementations.
cc #11851
Lint Name
No response
Reproducer
triggers the lint. But this code is clearly implementing an error-related trait for
Infallible
, with the understanding that the trait method is impossible to call.Version
No response
Additional Labels
No response
The text was updated successfully, but these errors were encountered: