vet, parser: move dynamic const array check from parser into vet (#21423)

This commit is contained in:
Turiiya 2024-05-05 11:55:27 +02:00 committed by GitHub
parent 387af74302
commit 8e7a6e3640
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 7 deletions

View File

@ -1 +1 @@
cmd/tools/vvet/tests/const_dynamic_array_notice.vv:1: notice: use a fixed array, instead of a dynamic one
cmd/tools/vvet/tests/const_dynamic_array_notice.vv:1: notice: Use a fixed array instead of a dynamic one

View File

@ -1 +1 @@
cmd/tools/vvet/tests/prog_without_main_fn.vv:1: notice: use a fixed array, instead of a dynamic one
cmd/tools/vvet/tests/prog_without_main_fn.vv:1: notice: Use a fixed array instead of a dynamic one

View File

@ -262,7 +262,7 @@ fn (mut vt Vet) stmts(stmts []ast.Stmt) {
fn (mut vt Vet) stmt(stmt ast.Stmt) {
match stmt {
ast.ConstDecl { vt.exprs(stmt.fields.map(it.expr)) }
ast.ConstDecl { vt.const_decl(stmt) }
ast.ExprStmt { vt.expr(stmt.expr) }
ast.Return { vt.exprs(stmt.exprs) }
ast.AssertStmt { vt.expr(stmt.expr) }
@ -310,6 +310,16 @@ fn (mut vt Vet) expr(expr ast.Expr) {
}
}
fn (mut vt Vet) const_decl(stmt ast.ConstDecl) {
for field in stmt.fields {
if field.expr is ast.ArrayInit && !field.expr.is_fixed {
vt.notice('Use a fixed array instead of a dynamic one', field.expr.pos.line_nr,
.unknown)
}
vt.expr(field.expr)
}
}
fn (vt &Vet) vprintln(s string) {
if !vt.opt.is_verbose {
return

View File

@ -3891,10 +3891,6 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
return ast.ConstDecl{}
}
expr := p.expr(0)
if expr is ast.ArrayInit && !expr.is_fixed && p.pref.is_vet {
p.vet_notice('use a fixed array, instead of a dynamic one', pos.line_nr, vet.FixKind.unknown,
.default)
}
if is_block {
end_comments << p.eat_comments(same_line: true)
}