-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Suggest adding a semicolon to a closure without block #97371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bors
merged 2 commits into
rust-lang:master
from
ChayimFriedman2:closure-non-block-add-semicolon
May 31, 2022
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,12 +46,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
blk_id: hir::HirId, | ||
) -> bool { | ||
let expr = expr.peel_drop_temps(); | ||
// If the expression is from an external macro, then do not suggest | ||
// adding a semicolon, because there's nowhere to put it. | ||
// See issue #81943. | ||
if expr.can_have_side_effects() && !in_external_macro(self.tcx.sess, expr.span) { | ||
self.suggest_missing_semicolon(err, expr, expected); | ||
} | ||
self.suggest_missing_semicolon(err, expr, expected, false); | ||
let mut pointing_at_return_type = false; | ||
if let Some((fn_decl, can_suggest)) = self.get_fn_decl(blk_id) { | ||
let fn_id = self.tcx.hir().get_return_block(blk_id).unwrap(); | ||
|
@@ -473,11 +468,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
/// This routine checks if the return expression in a block would make sense on its own as a | ||
/// statement and the return type has been left as default or has been specified as `()`. If so, | ||
/// it suggests adding a semicolon. | ||
fn suggest_missing_semicolon( | ||
/// | ||
/// If the expression is the expression of a closure without block (`|| expr`), a | ||
/// block is needed to be added too (`|| { expr; }`). This is denoted by `needs_block`. | ||
pub fn suggest_missing_semicolon( | ||
&self, | ||
err: &mut Diagnostic, | ||
expression: &'tcx hir::Expr<'tcx>, | ||
expected: Ty<'tcx>, | ||
needs_block: bool, | ||
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. Not a fan of the bool argument, but I can't come up with anything clearer right now 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. Neither am I :) Maybe an enum will do? |
||
) { | ||
if expected.is_unit() { | ||
// `BlockTailExpression` only relevant if the tail expr would be | ||
|
@@ -489,14 +488,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
| ExprKind::If(..) | ||
| ExprKind::Match(..) | ||
| ExprKind::Block(..) | ||
if expression.can_have_side_effects() => | ||
if expression.can_have_side_effects() | ||
// If the expression is from an external macro, then do not suggest | ||
// adding a semicolon, because there's nowhere to put it. | ||
// See issue #81943. | ||
&& !in_external_macro(self.tcx.sess, expression.span) => | ||
{ | ||
err.span_suggestion( | ||
expression.span.shrink_to_hi(), | ||
"consider using a semicolon here", | ||
";".to_string(), | ||
Applicability::MachineApplicable, | ||
); | ||
if needs_block { | ||
err.multipart_suggestion( | ||
"consider using a semicolon here", | ||
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
vec![ | ||
(expression.span.shrink_to_lo(), "{ ".to_owned()), | ||
(expression.span.shrink_to_hi(), "; }".to_owned()), | ||
], | ||
Applicability::MachineApplicable, | ||
); | ||
} else { | ||
err.span_suggestion( | ||
expression.span.shrink_to_hi(), | ||
"consider using a semicolon here", | ||
";".to_string(), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
_ => (), | ||
} | ||
|
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,11 @@ | ||
fn foo(_f: impl Fn()) {} | ||
|
||
fn bar() -> i32 { | ||
1 | ||
} | ||
|
||
fn main() { | ||
foo(|| bar()) | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider using a semicolon here | ||
} |
16 changes: 16 additions & 0 deletions
16
src/test/ui/closures/add_semicolon_non_block_closure.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,16 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/add_semicolon_non_block_closure.rs:8:12 | ||
| | ||
LL | fn main() { | ||
| - expected `()` because of default return type | ||
LL | foo(|| bar()) | ||
| ^^^^^ expected `()`, found `i32` | ||
| | ||
help: consider using a semicolon here | ||
| | ||
LL | foo(|| { bar(); }) | ||
| + +++ | ||
|
||
error: aborting due to previous error | ||
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.