-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Parse expression after else
as a condition if followed by {
#97298
Conversation
r? @davidtwco (rust-highfive has picked a reviewer for you, use r? to override) |
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.
One suggestion, otherwise looks good to me.
0246585
to
9be37b2
Compare
@rustbot ready |
@bors r+ |
📌 Commit 9be37b2 has been approved by |
…, r=davidtwco Parse expression after `else` as a condition if followed by `{` Fixes rust-lang#49361. Two things: 1. This wording needs help. I can never find a natural/intuitive phrasing when I write diagnostics 😅 2. Do we even want to show the "wrap in braces" case? I would assume most of the time the "add an `if`" case is the right one.
…, r=davidtwco Parse expression after `else` as a condition if followed by `{` Fixes rust-lang#49361. Two things: 1. This wording needs help. I can never find a natural/intuitive phrasing when I write diagnostics 😅 2. Do we even want to show the "wrap in braces" case? I would assume most of the time the "add an `if`" case is the right one.
Rollup of 4 pull requests Successful merges: - rust-lang#97288 (Lifetime variance fixes for rustdoc) - rust-lang#97298 (Parse expression after `else` as a condition if followed by `{`) - rust-lang#97308 (Stabilize `cell_filter_map`) - rust-lang#97321 (explain how to turn integers into fn ptrs) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Ok(cond) | ||
// If it's not a free-standing expression, and is followed by a block, | ||
// then it's very likely the condition to an `else if`. | ||
if self.check(&TokenKind::OpenDelim(Delimiter::Brace)) | ||
&& classify::expr_requires_semi_to_be_stmt(&cond) => | ||
{ |
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.
Given all the checks you have here...
).multipart_suggestion( | ||
"... otherwise, place this expression inside of a block if it is not an `if` condition", | ||
vec![ | ||
(cond.span.shrink_to_lo(), "{ ".to_string()), | ||
(cond.span.shrink_to_hi(), " }".to_string()), | ||
], | ||
Applicability::MaybeIncorrect, | ||
) |
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.
...I would have likely skipped this suggestion :)
.span_label(else_span, "expected an `if` or a block after this `else`") | ||
.span_suggestion( | ||
cond.span.shrink_to_lo(), | ||
"add an `if` if this is the condition to an chained `if` statement after the `else`", |
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.
"add an `if` if this is the condition to an chained `if` statement after the `else`", | |
"add an `if` if this is the condition to a chained `if` statement after the `else`", |
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.
…an-DPC Minor improvement on else-no-if diagnostic Don't suggest wrapping in block since it's highly likely to be a missing `if` after `else`. Also rework message a bit (open to further suggestions). cc: rust-lang#97298 (comment) r? `@estebank`
Fixes #49361.
Two things:
if
" case is the right one.