-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add suggestion to remove if in let..else block #107213
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,15 @@ use crate::errors::{ | |
ComparisonOrShiftInterpretedAsGenericSugg, DoCatchSyntaxRemoved, DotDotDot, EqFieldInit, | ||
ExpectedElseBlock, ExpectedEqForLetExpr, ExpectedExpressionFoundLet, | ||
FieldExpressionWithGeneric, FloatLiteralRequiresIntegerPart, FoundExprWouldBeStmt, | ||
IfExpressionMissingCondition, IfExpressionMissingThenBlock, IfExpressionMissingThenBlockSub, | ||
InvalidBlockMacroSegment, InvalidComparisonOperator, InvalidComparisonOperatorSub, | ||
InvalidInterpolatedExpression, InvalidLiteralSuffixOnTupleIndex, InvalidLogicalOperator, | ||
InvalidLogicalOperatorSub, LabeledLoopInBreak, LeadingPlusNotSupported, LeftArrowOperator, | ||
LifetimeInBorrowExpression, MacroInvocationWithQualifiedPath, MalformedLoopLabel, | ||
MatchArmBodyWithoutBraces, MatchArmBodyWithoutBracesSugg, MissingCommaAfterMatchArm, | ||
MissingDotDot, MissingInInForLoop, MissingInInForLoopSub, MissingSemicolonBeforeArray, | ||
NoFieldsForFnCall, NotAsNegationOperator, NotAsNegationOperatorSub, | ||
OuterAttributeNotAllowedOnIfElse, ParenthesesWithStructFields, | ||
IfExpressionLetSomeSub, IfExpressionMissingCondition, IfExpressionMissingThenBlock, | ||
IfExpressionMissingThenBlockSub, InvalidBlockMacroSegment, InvalidComparisonOperator, | ||
InvalidComparisonOperatorSub, InvalidInterpolatedExpression, InvalidLiteralSuffixOnTupleIndex, | ||
InvalidLogicalOperator, InvalidLogicalOperatorSub, LabeledLoopInBreak, LeadingPlusNotSupported, | ||
LeftArrowOperator, LifetimeInBorrowExpression, MacroInvocationWithQualifiedPath, | ||
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 horrible noise. Can't one instead use 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. Alternatively, let's replace this with a glob import 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. Note: this is nothing that this particular PR has caused. The pattern of having a giant use at the top just creates this noise in PRs like this. 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. Let's discuss it on zulip. |
||
MalformedLoopLabel, MatchArmBodyWithoutBraces, MatchArmBodyWithoutBracesSugg, | ||
MissingCommaAfterMatchArm, MissingDotDot, MissingInInForLoop, MissingInInForLoopSub, | ||
MissingSemicolonBeforeArray, NoFieldsForFnCall, NotAsNegationOperator, | ||
NotAsNegationOperatorSub, OuterAttributeNotAllowedOnIfElse, ParenthesesWithStructFields, | ||
RequireColonAfterLabeledExpression, ShiftInterpretedAsGeneric, StructLiteralNotAllowedHere, | ||
StructLiteralNotAllowedHereSugg, TildeAsUnaryOperator, UnexpectedIfWithIf, | ||
UnexpectedTokenAfterLabel, UnexpectedTokenAfterLabelSugg, WrapExpressionInParentheses, | ||
|
@@ -2251,9 +2251,10 @@ impl<'a> Parser<'a> { | |
if let ExprKind::Block(_, None) = right.kind => { | ||
self.sess.emit_err(IfExpressionMissingThenBlock { | ||
if_span: lo, | ||
sub: IfExpressionMissingThenBlockSub::UnfinishedCondition( | ||
cond_span.shrink_to_lo().to(*binop_span) | ||
), | ||
missing_then_block_sub: | ||
IfExpressionMissingThenBlockSub::UnfinishedCondition(cond_span.shrink_to_lo().to(*binop_span)), | ||
let_else_sub: None, | ||
|
||
}); | ||
std::mem::replace(right, this.mk_expr_err(binop_span.shrink_to_hi())) | ||
}, | ||
|
@@ -2279,9 +2280,15 @@ impl<'a> Parser<'a> { | |
if let Some(block) = recover_block_from_condition(self) { | ||
block | ||
} else { | ||
let let_else_sub = matches!(cond.kind, ExprKind::Let(..)) | ||
.then(|| IfExpressionLetSomeSub { if_span: lo }); | ||
|
||
self.sess.emit_err(IfExpressionMissingThenBlock { | ||
if_span: lo, | ||
sub: IfExpressionMissingThenBlockSub::AddThenBlock(cond_span.shrink_to_hi()), | ||
missing_then_block_sub: IfExpressionMissingThenBlockSub::AddThenBlock( | ||
cond_span.shrink_to_hi(), | ||
), | ||
let_else_sub, | ||
}); | ||
self.mk_block_err(cond_span.shrink_to_hi()) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
fn main() { | ||
let x = Some(123); | ||
if let Some(y) = x else { //~ ERROR this `if` expression is missing a block | ||
return; | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error: this `if` expression is missing a block after the condition | ||
--> $DIR/accidental-if.rs:3:5 | ||
| | ||
LL | if let Some(y) = x else { | ||
| ^^ | ||
| | ||
help: add a block here | ||
--> $DIR/accidental-if.rs:3:23 | ||
| | ||
LL | if let Some(y) = x else { | ||
| ^ | ||
help: remove the `if` if you meant to write a `let...else` statement | ||
--> $DIR/accidental-if.rs:3:5 | ||
| | ||
LL | if let Some(y) = x else { | ||
| ^^ | ||
|
||
error: aborting due to previous error | ||
|
Uh oh!
There was an error while loading. Please reload this page.