forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#120479 - estebank:issue-61788, r=wesleywiser
Suggest turning `if let` into irrefutable `let` if appropriate When encountering an `if let` tail expression without an `else` arm for an enum with a single variant, suggest writing an irrefutable `let` binding instead. ``` error[E0317]: `if` may be missing an `else` clause --> $DIR/irrefutable-if-let-without-else.rs:8:5 | LL | fn foo(x: Enum) -> i32 { | --- expected `i32` because of this return type LL | / if let Enum::Variant(value) = x { LL | | value LL | | } | |_____^ expected `i32`, found `()` | = note: `if` expressions without `else` evaluate to `()` = help: consider adding an `else` block that evaluates to the expected type help: consider using an irrefutable `let` binding instead | LL ~ let Enum::Variant(value) = x; LL ~ value | ``` Fix rust-lang#61788.
- Loading branch information
Showing
5 changed files
with
214 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// run-rustfix | ||
enum Enum { | ||
Variant(i32), | ||
} | ||
struct Struct(i32); | ||
|
||
fn foo(x: Enum) -> i32 { | ||
let Enum::Variant(value) = x; | ||
value | ||
} | ||
fn bar(x: Enum) -> i32 { | ||
let Enum::Variant(value) = x; | ||
let x = value + 1; | ||
x | ||
} | ||
fn baz(x: Struct) -> i32 { | ||
let Struct(value) = x; | ||
let x = value + 1; | ||
x | ||
} | ||
fn main() { | ||
let _ = foo(Enum::Variant(42)); | ||
let _ = bar(Enum::Variant(42)); | ||
let _ = baz(Struct(42)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// run-rustfix | ||
enum Enum { | ||
Variant(i32), | ||
} | ||
struct Struct(i32); | ||
|
||
fn foo(x: Enum) -> i32 { | ||
if let Enum::Variant(value) = x { //~ ERROR `if` may be missing an `else` clause | ||
value | ||
} | ||
} | ||
fn bar(x: Enum) -> i32 { | ||
if let Enum::Variant(value) = x { //~ ERROR `if` may be missing an `else` clause | ||
let x = value + 1; | ||
x | ||
} | ||
} | ||
fn baz(x: Struct) -> i32 { | ||
if let Struct(value) = x { //~ ERROR `if` may be missing an `else` clause | ||
let x = value + 1; | ||
x | ||
} | ||
} | ||
fn main() { | ||
let _ = foo(Enum::Variant(42)); | ||
let _ = bar(Enum::Variant(42)); | ||
let _ = baz(Struct(42)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
error[E0317]: `if` may be missing an `else` clause | ||
--> $DIR/irrefutable-if-let-without-else.rs:8:5 | ||
| | ||
LL | fn foo(x: Enum) -> i32 { | ||
| --- expected `i32` because of this return type | ||
LL | / if let Enum::Variant(value) = x { | ||
LL | | value | ||
LL | | } | ||
| |_____^ expected `i32`, found `()` | ||
| | ||
= note: `if` expressions without `else` evaluate to `()` | ||
= help: consider adding an `else` block that evaluates to the expected type | ||
help: consider using an irrefutable `let` binding instead | ||
| | ||
LL ~ let Enum::Variant(value) = x; | ||
LL ~ value | ||
| | ||
|
||
error[E0317]: `if` may be missing an `else` clause | ||
--> $DIR/irrefutable-if-let-without-else.rs:13:5 | ||
| | ||
LL | fn bar(x: Enum) -> i32 { | ||
| --- expected `i32` because of this return type | ||
LL | / if let Enum::Variant(value) = x { | ||
LL | | let x = value + 1; | ||
LL | | x | ||
LL | | } | ||
| |_____^ expected `i32`, found `()` | ||
| | ||
= note: `if` expressions without `else` evaluate to `()` | ||
= help: consider adding an `else` block that evaluates to the expected type | ||
help: consider using an irrefutable `let` binding instead | ||
| | ||
LL ~ let Enum::Variant(value) = x; | ||
LL ~ let x = value + 1; | ||
LL ~ x | ||
| | ||
|
||
error[E0317]: `if` may be missing an `else` clause | ||
--> $DIR/irrefutable-if-let-without-else.rs:19:5 | ||
| | ||
LL | fn baz(x: Struct) -> i32 { | ||
| --- expected `i32` because of this return type | ||
LL | / if let Struct(value) = x { | ||
LL | | let x = value + 1; | ||
LL | | x | ||
LL | | } | ||
| |_____^ expected `i32`, found `()` | ||
| | ||
= note: `if` expressions without `else` evaluate to `()` | ||
= help: consider adding an `else` block that evaluates to the expected type | ||
help: consider using an irrefutable `let` binding instead | ||
| | ||
LL ~ let Struct(value) = x; | ||
LL ~ let x = value + 1; | ||
LL ~ x | ||
| | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0317`. |