Open
Description
Code
trait ResultExt<T> { fn into_ok(self) -> T; }
impl<T, E> ResultExt<T> for Result<T, E>
where
E: Into<core::convert::Infallible>,
{
/// Infallibly unwraps `self` into it's value
fn into_ok(self) -> T {
match self {
Ok(val) => val,
Err(e) => match Into::into(e) {},
}
}
}
Current output
warning: unreachable expression
--> src/main.rs:11:23
|
11 | Err(e) => match Into::into(e) {},
| ^^^^^^-------------^^^
| | |
| | any code following this expression is unreachable
| unreachable expression
|
note: this expression has type `std::convert::Infallible`, which is uninhabited
--> src/main.rs:11:29
|
11 | Err(e) => match Into::into(e) {},
| ^^^^^^^^^^^^^
= note: `#[warn(unreachable_code)]` on by default
warning: `playground` (bin "playground") generated 1 warning
Desired output
No diagnostic.
Rationale and extra context
The match
in that code, which is linted against, as well as the Into
is added for type checking. Without either, the above code fails to compile, as can be demonstrated here:
trait ResultExt<T> { fn into_ok(self) -> T; }
impl<T, E> ResultExt<T> for Result<T, E>
where
E: Into<core::convert::Infallible>,
{
/// Infallibly unwraps `self` into it's value
fn into_ok(self) -> T {
match self {
Ok(val) => val,
Err(e) => Into::into(e),
}
}
}
which yields
error[E0308]: `match` arms have incompatible types
--> src/main.rs:11:23
|
3 | impl<T, E> ResultExt<T> for Result<T, E>
| - this type parameter
...
9 | / match self {
10 | | Ok(val) => val,
| | --- this is found to be of type `T`
11 | | Err(e) => Into::into(e),
| | ^^^^^^^^^^^^^ expected type parameter `T`, found `Infallible`
12 | | }
| |_________- `match` arms have incompatible types
|
= note: expected type parameter `T`
found enum `std::convert::Infallible`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (bin "playground") due to previous error
(cases without the Into::into(e)
are trivial, and left as exercise to reader)
Other cases
No response
Anything else?
Actual code that found this lint is https://github.com/lccc-project/lccc/blob/main/xlang/xlang_abi/src/result.rs#L182-L194