Closed
Description
The following code (playground)
enum Enum{
Variant(i32),
}
fn stuff(x: Enum) -> i32{
if let Enum::Variant(value) = x {
value
}
}
gives the following error:
Compiling playground v0.0.1 (/playground)
error[E0317]: if may be missing an else clause
--> src/lib.rs:6:5
|
6 | / if let Enum::Variant(value) = x {
7 | | value
8 | | }
| |_____^ expected (), found i32
|
= note: expected type `()`
found type `i32`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0317`.
error: Could not compile `playground`.
which is weird, since the else
clause would never be visited even if it was present.
The equivalent one-arm match
enum Enum{
Variant(i32),
}
fn stuff(x: Enum) -> i32{
match x {
Enum::Variant(value) => value,
}
}
compiles just fine (expectedly).
Adding an else { unreachable!() }
branch to the if let
is, of course, possible, but unelegant, and really shouldn't be needed, as it can be inferred at compile time.
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Suggestions generated by the compiler applied by `cargo fix`Category: An issue proposing an enhancement or a PR with one.Low priorityRelevant to the compiler team, which will review and decide on the PR/issue.