mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 20:09:19 +00:00
show diagnostic for } token followed by else in let else statement
This commit is contained in:
parent
31c12ec282
commit
995aacfce8
5 changed files with 101 additions and 8 deletions
|
@ -43,7 +43,7 @@ pub(super) fn meta(p: &mut Parser<'_>) {
|
|||
match p.current() {
|
||||
T![=] => {
|
||||
p.bump(T![=]);
|
||||
if !expressions::expr(p) {
|
||||
if expressions::expr(p).is_none() {
|
||||
p.error("expected expression");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,9 +16,9 @@ pub(super) enum Semicolon {
|
|||
|
||||
const EXPR_FIRST: TokenSet = LHS_FIRST;
|
||||
|
||||
pub(super) fn expr(p: &mut Parser<'_>) -> bool {
|
||||
pub(super) fn expr(p: &mut Parser<'_>) -> Option<CompletedMarker> {
|
||||
let r = Restrictions { forbid_structs: false, prefer_stmt: false };
|
||||
expr_bp(p, None, r, 1).is_some()
|
||||
expr_bp(p, None, r, 1).map(|(m, _)| m)
|
||||
}
|
||||
|
||||
pub(super) fn expr_stmt(
|
||||
|
@ -120,16 +120,32 @@ pub(super) fn stmt(p: &mut Parser<'_>, semicolon: Semicolon) {
|
|||
// fn f() { let x: i32; }
|
||||
types::ascription(p);
|
||||
}
|
||||
|
||||
let mut is_block_like_expr_after_eq = false;
|
||||
if p.eat(T![=]) {
|
||||
// test let_stmt_init
|
||||
// fn f() { let x = 92; }
|
||||
expressions::expr(p);
|
||||
let expr = expressions::expr(p);
|
||||
|
||||
if let Some(expr) = expr {
|
||||
is_block_like_expr_after_eq = match expr.kind() {
|
||||
IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => true,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if p.at(T![else]) {
|
||||
// test_err let_else_right_curly_brace
|
||||
// fn func() { let Some(_) = {Some(1)} else { panic!("h") };}
|
||||
if is_block_like_expr_after_eq {
|
||||
p.error(
|
||||
"right curly brace `}` before `else` in a `let...else` statement not allowed",
|
||||
)
|
||||
}
|
||||
|
||||
// test let_else
|
||||
// fn f() { let Some(x) = opt else { return }; }
|
||||
|
||||
let m = p.start();
|
||||
p.bump(T![else]);
|
||||
block_expr(p);
|
||||
|
@ -578,7 +594,14 @@ fn arg_list(p: &mut Parser<'_>) {
|
|||
// fn main() {
|
||||
// foo(#[attr] 92)
|
||||
// }
|
||||
delimited(p, T!['('], T![')'], T![,], EXPR_FIRST.union(ATTRIBUTE_FIRST), expr);
|
||||
delimited(
|
||||
p,
|
||||
T!['('],
|
||||
T![')'],
|
||||
T![,],
|
||||
EXPR_FIRST.union(ATTRIBUTE_FIRST),
|
||||
|p: &mut Parser<'_>| expr(p).is_some(),
|
||||
);
|
||||
m.complete(p, ARG_LIST);
|
||||
}
|
||||
|
||||
|
|
|
@ -188,7 +188,7 @@ fn tuple_expr(p: &mut Parser<'_>) -> CompletedMarker {
|
|||
|
||||
// test tuple_attrs
|
||||
// const A: (i64, i64) = (1, #[cfg(test)] 2);
|
||||
if !expr(p) {
|
||||
if expr(p).is_none() {
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -221,7 +221,7 @@ fn array_expr(p: &mut Parser<'_>) -> CompletedMarker {
|
|||
|
||||
// test array_attrs
|
||||
// const A: &[i64] = &[1, #[cfg(test)] 2];
|
||||
if !expr(p) {
|
||||
if expr(p).is_none() {
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue