-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #81458 - estebank:match-stmt-remove-semi, r=oli-obk
Detect match statement intended to be tail expression CC #24157
- Loading branch information
Showing
6 changed files
with
161 additions
and
10 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
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
30 changes: 30 additions & 0 deletions
30
src/test/ui/suggestions/match-with-different-arm-types-as-stmt-instead-of-expr.rs
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,30 @@ | ||
pub trait Foo {} | ||
|
||
struct Bar; | ||
struct Baz; | ||
|
||
impl Foo for Bar { } | ||
impl Foo for Baz { } | ||
|
||
fn not_all_paths(a: &str) -> u32 { //~ ERROR mismatched types | ||
match a { | ||
"baz" => 0, | ||
_ => 1, | ||
}; | ||
} | ||
|
||
fn right(b: &str) -> Box<dyn Foo> { | ||
match b { | ||
"baz" => Box::new(Baz), | ||
_ => Box::new(Bar), | ||
} | ||
} | ||
|
||
fn wrong(c: &str) -> Box<dyn Foo> { //~ ERROR mismatched types | ||
match c { | ||
"baz" => Box::new(Baz), | ||
_ => Box::new(Bar), //~ ERROR `match` arms have incompatible types | ||
}; | ||
} | ||
|
||
fn main() {} |
51 changes: 51 additions & 0 deletions
51
src/test/ui/suggestions/match-with-different-arm-types-as-stmt-instead-of-expr.stderr
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,51 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/match-with-different-arm-types-as-stmt-instead-of-expr.rs:9:30 | ||
| | ||
LL | fn not_all_paths(a: &str) -> u32 { | ||
| ------------- ^^^ expected `u32`, found `()` | ||
| | | ||
| implicitly returns `()` as its body has no tail or `return` expression | ||
... | ||
LL | }; | ||
| - help: consider removing this semicolon | ||
|
||
error[E0308]: `match` arms have incompatible types | ||
--> $DIR/match-with-different-arm-types-as-stmt-instead-of-expr.rs:26:14 | ||
| | ||
LL | / match c { | ||
LL | | "baz" => Box::new(Baz), | ||
| | ------------- this is found to be of type `Box<Baz>` | ||
LL | | _ => Box::new(Bar), | ||
| | ^^^^^^^^^^^^^ expected struct `Baz`, found struct `Bar` | ||
LL | | }; | ||
| |_____- `match` arms have incompatible types | ||
| | ||
= note: expected type `Box<Baz>` | ||
found struct `Box<Bar>` | ||
note: you might have meant to return the `match` expression | ||
--> $DIR/match-with-different-arm-types-as-stmt-instead-of-expr.rs:27:6 | ||
| | ||
LL | fn wrong(c: &str) -> Box<dyn Foo> { | ||
| ------------ the `match` arms can conform to this return type | ||
LL | / match c { | ||
LL | | "baz" => Box::new(Baz), | ||
LL | | _ => Box::new(Bar), | ||
LL | | }; | ||
| | -^ the `match` is a statement because of this semicolon, consider removing it | ||
| |_____| | ||
| this could be implicitly returned but it is a statement, not a tail expression | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/match-with-different-arm-types-as-stmt-instead-of-expr.rs:23:22 | ||
| | ||
LL | fn wrong(c: &str) -> Box<dyn Foo> { | ||
| ----- ^^^^^^^^^^^^ expected struct `Box`, found `()` | ||
| | | ||
| implicitly returns `()` as its body has no tail or `return` expression | ||
| | ||
= note: expected struct `Box<(dyn Foo + 'static)>` | ||
found unit type `()` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |