From 317acfda9784b6fd92f282136cfc17ebd3b883b3 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 29 Apr 2022 19:57:14 +0800 Subject: [PATCH] checker: check struct field name using uppercase letters (#14220) --- .../v/checker/tests/struct_field_name_err.out | 27 ++++++++++++------- vlib/v/checker/tests/struct_field_name_err.vv | 5 ++-- vlib/v/parser/struct.v | 1 + 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/vlib/v/checker/tests/struct_field_name_err.out b/vlib/v/checker/tests/struct_field_name_err.out index 08a4eeb823..50853c00c5 100644 --- a/vlib/v/checker/tests/struct_field_name_err.out +++ b/vlib/v/checker/tests/struct_field_name_err.out @@ -1,13 +1,20 @@ -vlib/v/checker/tests/struct_field_name_err.vv:2:2: error: field name `Architecture` cannot contain uppercase letters, use snake_case instead +vlib/v/checker/tests/struct_field_name_err.vv:2:2: error: field name `Foo` cannot contain uppercase letters, use snake_case instead 1 | struct Release { - 2 | Architecture []string - | ~~~~~~~~~~~~~~~~~~~~~ - 3 | Components []string - 4 | } -vlib/v/checker/tests/struct_field_name_err.vv:3:2: error: field name `Components` cannot contain uppercase letters, use snake_case instead + 2 | Foo string + | ~~~~~~~~~~~~~~~~~~~ + 3 | Bar &int + 4 | Architecture []string +vlib/v/checker/tests/struct_field_name_err.vv:3:2: error: field name `Bar` cannot contain uppercase letters, use snake_case instead 1 | struct Release { - 2 | Architecture []string - 3 | Components []string + 2 | Foo string + 3 | Bar &int + | ~~~~~~~~~~~~~~~~~ + 4 | Architecture []string + 5 | } +vlib/v/checker/tests/struct_field_name_err.vv:4:2: error: field name `Architecture` cannot contain uppercase letters, use snake_case instead + 2 | Foo string + 3 | Bar &int + 4 | Architecture []string | ~~~~~~~~~~~~~~~~~~~~~ - 4 | } - 5 | + 5 | } + 6 | diff --git a/vlib/v/checker/tests/struct_field_name_err.vv b/vlib/v/checker/tests/struct_field_name_err.vv index b5b33cabc1..db62b0e10d 100644 --- a/vlib/v/checker/tests/struct_field_name_err.vv +++ b/vlib/v/checker/tests/struct_field_name_err.vv @@ -1,9 +1,8 @@ struct Release { + Foo string + Bar &int Architecture []string - Components []string } fn main() { - r := Release{} - println(r) } diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 6a7b0af035..e6b81cebe0 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -185,6 +185,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl { is_field_volatile = true } is_embed := ((p.tok.lit.len > 1 && p.tok.lit[0].is_capital() + && (p.peek_tok.line_nr != p.tok.line_nr || p.peek_tok.kind !in [.name, .amp]) && (p.peek_tok.kind != .lsbr || p.peek_token(2).kind != .rsbr)) || p.peek_tok.kind == .dot) && language == .v && p.peek_tok.kind != .key_fn is_on_top := ast_fields.len == 0 && !(is_field_mut || is_field_global)