Unnamed repository; edit this file 'description' to name the repository.
Merge #10343
10343: internal: parser cleanups r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
9 files changed, 142 insertions, 173 deletions
diff --git a/crates/ide_assists/src/utils/gen_trait_fn_body.rs b/crates/ide_assists/src/utils/gen_trait_fn_body.rs index b9c7da71b5..eb4a23a8da 100644 --- a/crates/ide_assists/src/utils/gen_trait_fn_body.rs +++ b/crates/ide_assists/src/utils/gen_trait_fn_body.rs @@ -439,10 +439,10 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { let eq_check = make::expr_bin_op(lhs, BinaryOp::CmpOp(CmpOp::Eq { negated: false }), rhs); - let mut case_count = 0; + let mut n_cases = 0; let mut arms = vec![]; for variant in enum_.variant_list()?.variants() { - case_count += 1; + n_cases += 1; match variant.field_list() { // => (Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin, Some(ast::FieldList::RecordFieldList(list)) => { @@ -517,7 +517,7 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { let expr = match arms.len() { 0 => eq_check, _ => { - if case_count > arms.len() { + if n_cases > arms.len() { let lhs = make::wildcard_pat().into(); arms.push(make::match_arm(Some(lhs), None, eq_check)); } diff --git a/crates/parser/src/grammar/expressions.rs b/crates/parser/src/grammar/expressions.rs index 29310b71bd..9f3e11819f 100644 --- a/crates/parser/src/grammar/expressions.rs +++ b/crates/parser/src/grammar/expressions.rs @@ -1,8 +1,9 @@ mod atom; +use super::*; + pub(crate) use self::atom::{block_expr, match_arm_list}; pub(super) use self::atom::{literal, LITERAL_FIRST}; -use super::*; pub(super) enum StmtWithSemi { Yes, @@ -47,11 +48,6 @@ fn expr_no_struct(p: &mut Parser) { expr_bp(p, r, 1); } -fn is_expr_stmt_attr_allowed(kind: SyntaxKind) -> bool { - let forbid = matches!(kind, BIN_EXPR | RANGE_EXPR); - !forbid -} - pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi, prefer_expr: bool) { let m = p.start(); // test attr_on_expr_stmt @@ -79,13 +75,15 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi, prefer_expr: bool) { let (cm, blocklike) = expr_stmt(p); let kind = cm.as_ref().map(|cm| cm.kind()).unwrap_or(ERROR); - if has_attrs && !is_expr_stmt_attr_allowed(kind) { - // test_err attr_on_expr_not_allowed - // fn foo() { - // #[A] 1 + 2; - // #[B] if true {}; - // } - p.error(format!("attributes are not allowed on {:?}", kind)); + if has_attrs { + if matches!(kind, BIN_EXPR | RANGE_EXPR) { + // test_err attr_on_expr_not_allowed + // fn foo() { + // #[A] 1 + 2; + // #[B] if true {}; + // } + p.error(format!("attributes are not allowed on {:?}", kind)); + } } if p.at(T!['}']) || (prefer_expr && p.at(EOF)) { @@ -117,6 +115,10 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi, prefer_expr: bool) { // } match with_semi { + StmtWithSemi::No => (), + StmtWithSemi::Optional => { + p.eat(T![;]); + } StmtWithSemi::Yes => { if blocklike.is_block() { p.eat(T![;]); @@ -124,48 +126,35 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi, prefer_expr: bool) { p.expect(T![;]); } } - StmtWithSemi::No => {} - StmtWithSemi::Optional => { - if p.at(T![;]) { - p.eat(T![;]); - } - } } m.complete(p, EXPR_STMT); } // test let_stmt - // fn foo() { - // let a; - // let b: i32; - // let c = 92; - // let d: i32 = 92; - // let e: !; - // let _: ! = {}; - // let f = #[attr]||{}; - // } + // fn f() { let x: i32 = 92; } fn let_stmt(p: &mut Parser, m: Marker, with_semi: StmtWithSemi) { - assert!(p.at(T![let])); p.bump(T![let]); patterns::pattern(p); if p.at(T![:]) { + // test let_stmt_ascription + // fn f() { let x: i32; } types::ascription(p); } if p.eat(T![=]) { + // test let_stmt_init + // fn f() { let x = 92; } expressions::expr_with_attrs(p); } match with_semi { + StmtWithSemi::No => (), + StmtWithSemi::Optional => { + p.eat(T![;]); + } StmtWithSemi::Yes => { p.expect(T![;]); } - StmtWithSemi::No => {} - StmtWithSemi::Optional => { - if p.at(T![;]) { - p.eat(T![;]); - } - } } m.complete(p, LET_STMT); } diff --git a/crates/syntax/test_data/parser/inline/ok/0130_let_stmt.rast b/crates/syntax/test_data/parser/inline/ok/0130_let_stmt.rast index c3a79836aa..af3b11376b 100644 --- a/crates/syntax/test_data/parser/inline/ok/0130_let_stmt.rast +++ b/crates/syntax/test_data/parser/inline/ok/0130_let_stmt.rast @@ -1,127 +1,35 @@ [email protected] "fn" - [email protected] "foo" - [email protected] "(" - [email protected] ")" - [email protected] " " - [email protected] "{" - [email protected] "\n " - [email protected] "let" - [email protected] " " - [email protected] "a" - [email protected] ";" - [email protected] "\n " - [email protected] "let" - [email protected] " " - [email protected] "b" - [email protected] ":" - [email protected] " " - [email protected] "i32" - [email protected] ";" - [email protected] "\n " - [email protected] "let" - [email protected] " " - [email protected] "c" - [email protected] " " - [email protected] "=" - [email protected] " " - [email protected] "92" - [email protected] ";" - [email protected] "\n " - [email protected] "let" - [email protected] " " - [email protected] "d" - [email protected] ":" - [email protected] " " - [email protected] "i32" - [email protected] " " - [email protected] "=" - [email protected] " " - [email protected] "92" - [email protected] ";" - [email protected] "\n " - [email protected] "let" - [email protected] " " - [email protected] "e" - [email protected] ":" - [email protected] " " - [email protected] "!" - [email protected] ";" - [email protected] "\n " - [email protected] "let" - [email protected] " " - [email protected] "_" - [email protected] ":" - [email protected] " " - [email protected] "!" - [email protected] " " - [email protected] "=" - [email protected] " " - [email protected] "{" - [email protected] "}" - [email protected] ";" - [email protected] "\n " - [email protected] "let" - [email protected] " " - [email protected] "f" - [email protected] " " - [email protected] "=" - [email protected] " " - [email protected] "#" - [email protected] "[" - [email protected] "attr" - [email protected] "]" - [email protected] "|" - [email protected] "|" - [email protected] "{" - [email protected] "}" - [email protected] ";" - [email protected] "\n" - [email protected] "}" - [email protected] "\n" + [email protected] "f" + [email protected] "(" + [email protected] ")" + [email protected] " " + [email protected] "{" + [email protected] " " + [email protected] "let" + [email protected] " " + [email protected] "x" + [email protected] ":" + [email protected] " " + [email protected] "i32" + [email protected] " " + [email protected] "=" + [email protected] " " + [email protected] "92" + [email protected] ";" + [email protected] " " + [email protected] "}" + [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0130_let_stmt.rs b/crates/syntax/test_data/parser/inline/ok/0130_let_stmt.rs index fa8ee49a23..8003999fd0 100644 --- a/crates/syntax/test_data/parser/inline/ok/0130_let_stmt.rs +++ b/crates/syntax/test_data/parser/inline/ok/0130_let_stmt.rs @@ -1,9 +1 @@ -fn foo() { - let a; - let b: i32; - let c = 92; - let d: i32 = 92; - let e: !; - let _: ! = {}; - let f = #[attr]||{}; -} +fn f() { let x: i32 = 92; } diff --git a/crates/syntax/test_data/parser/inline/ok/0193_let_stmt_init.rast b/crates/syntax/test_data/parser/inline/ok/0193_let_stmt_init.rast new file mode 100644 index 0000000000..cc5d72ff74 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0193_let_stmt_init.rast @@ -0,0 +1,28 @@ + [email protected] "fn" + [email protected] " " + [email protected] "f" + [email protected] "(" + [email protected] ")" + [email protected] " " + [email protected] "{" + [email protected] " " + [email protected] "let" + [email protected] " " + [email protected] "x" + [email protected] " " + [email protected] "=" + [email protected] " " + [email protected] "92" + [email protected] ";" + [email protected] " " + [email protected] "}" + [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0193_let_stmt_init.rs b/crates/syntax/test_data/parser/inline/ok/0193_let_stmt_init.rs new file mode 100644 index 0000000000..232c0db411 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0193_let_stmt_init.rs @@ -0,0 +1 @@ +fn f() { let x = 92; } diff --git a/crates/syntax/test_data/parser/inline/ok/0194_let_stmt_ascription.rast b/crates/syntax/test_data/parser/inline/ok/0194_let_stmt_ascription.rast new file mode 100644 index 0000000000..41acb0dd98 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0194_let_stmt_ascription.rast @@ -0,0 +1,30 @@ + [email protected] "fn" + [email protected] " " + [email protected] "f" + [email protected] "(" + [email protected] ")" + [email protected] " " + [email protected] "{" + [email protected] " " + [email protected] "let" + [email protected] " " + [email protected] "x" + [email protected] ":" + [email protected] " " + [email protected] "i32" + [email protected] ";" + [email protected] " " + [email protected] "}" + [email protected] "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0194_let_stmt_ascription.rs b/crates/syntax/test_data/parser/inline/ok/0194_let_stmt_ascription.rs new file mode 100644 index 0000000000..a94161dffa --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0194_let_stmt_ascription.rs @@ -0,0 +1 @@ +fn f() { let x: i32; } diff --git a/docs/dev/style.md b/docs/dev/style.md index 92e79508b6..e11005c560 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -849,7 +849,7 @@ Default names: * `res` -- "result of the function" local variable * `it` -- I don't really care about the name -* `n_foo` -- number of foos +* `n_foos` -- number of foos (prefer this to `foo_count`) * `foo_idx` -- index of `foo` Many names in rust-analyzer conflict with keywords. @@ -969,6 +969,26 @@ Don't use the `ref` keyword. Today, it is redundant. Between `ref` and mach ergonomics, the latter is more ergonomic in most cases, and is simpler (does not require a keyword). +## Empty Match Arms + +Ues `=> (),` when a match arm is intentionally empty: + +```rust +// GOOD +match result { + Ok(_) => (), + Err(err) => error!("{}", err), +} + +// BAD +match result { + Ok(_) => {} + Err(err) => error!("{}", err), +} +``` + +**Rationale:** consistency. + ## Functional Combinators Use high order monadic combinators like `map`, `then` when they are a natural choice; don't bend the code to fit into some combinator. |