-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Detect match statement intended to be tail expression #81458
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -539,7 +539,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
self.overwrite_local_ty_if_err(local, ty, pat_ty); | ||
} | ||
|
||
pub fn check_stmt(&self, stmt: &'tcx hir::Stmt<'tcx>) { | ||
pub fn check_stmt(&self, stmt: &'tcx hir::Stmt<'tcx>, is_last: bool) { | ||
// Don't do all the complex logic below for `DeclItem`. | ||
match stmt.kind { | ||
hir::StmtKind::Item(..) => return, | ||
|
@@ -567,7 +567,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
}); | ||
} | ||
hir::StmtKind::Semi(ref expr) => { | ||
self.check_expr(&expr); | ||
// All of this is equivalent to calling `check_expr`, but it is inlined out here | ||
// in order to capture the fact that this `match` is the last statement in its | ||
// function. This is done for better suggestions to remove the `;`. | ||
let expectation = match expr.kind { | ||
hir::ExprKind::Match(..) if is_last => IsLast(stmt.span), | ||
_ => NoExpectation, | ||
}; | ||
Comment on lines
+573
to
+576
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the other piece of code isn't at fault, let's try reverting to the logic you had before (bubbling the information down in the function arguments |
||
self.check_expr_with_expectation(expr, expectation); | ||
} | ||
} | ||
|
||
|
@@ -626,8 +633,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
let ctxt = BreakableCtxt { coerce: Some(coerce), may_break: false }; | ||
|
||
let (ctxt, ()) = self.with_breakable_ctxt(blk.hir_id, ctxt, || { | ||
for s in blk.stmts { | ||
self.check_stmt(s); | ||
for (pos, s) in blk.stmts.iter().enumerate() { | ||
self.check_stmt(s, blk.stmts.len() - 1 == pos); | ||
} | ||
|
||
// check the tail expression **without** holding the | ||
|
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 hidden or 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 hidden or 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 | ||
Comment on lines
+25
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the new part. |
||
|
||
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`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing this on every tail expression could potentially be the cause of the regression
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened #82738 to check this.