Skip to content

unreachable_code lint on matching empty enum result of Into::into #116903

Open
@chorman0773

Description

@chorman0773

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.L-false-positiveLint: False positive (should not have fired).L-unreachable_codeLint: unreachable_codeT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions