diff --git a/cmd/tools/vvet/tests/const_dynamic_array_notice.out b/cmd/tools/vvet/tests/const_dynamic_array_notice.out new file mode 100644 index 00000000000000..4a8de48357ab53 --- /dev/null +++ b/cmd/tools/vvet/tests/const_dynamic_array_notice.out @@ -0,0 +1 @@ +cmd/tools/vvet/tests/const_dynamic_array_notice.vv:1: notice: use a fixed array, instead of a dynamic one diff --git a/cmd/tools/vvet/tests/const_dynamic_array_notice.vv b/cmd/tools/vvet/tests/const_dynamic_array_notice.vv new file mode 100644 index 00000000000000..2823f94a8592aa --- /dev/null +++ b/cmd/tools/vvet/tests/const_dynamic_array_notice.vv @@ -0,0 +1 @@ +const a = [1, 2, 3] diff --git a/cmd/tools/vvet/vvet.v b/cmd/tools/vvet/vvet.v index e48f06c2eb66eb..67447bcd2b7bd7 100644 --- a/cmd/tools/vvet/vvet.v +++ b/cmd/tools/vvet/vvet.v @@ -14,9 +14,10 @@ import term struct Vet { opt Options mut: - errors []vet.Error - warns []vet.Error - file string + errors []vet.Error + warns []vet.Error + notices []vet.Error + file string } struct Options { @@ -69,6 +70,9 @@ fn main() { } } vfmt_err_count := vt.errors.filter(it.fix == .vfmt).len + for n in vt.notices { + eprintln(vt.e2string(n)) + } if vt.opt.show_warnings { for w in vt.warns { eprintln(vt.e2string(w)) @@ -100,9 +104,10 @@ fn (mut vt Vet) vet_file(path string) { prefs.is_vsh = path.ends_with('.vsh') table := ast.new_table() vt.vprintln("vetting file '${path}'...") - _, errors := parser.parse_vet_file(path, table, prefs) + _, errors, notices := parser.parse_vet_file(path, table, prefs) // Transfer errors from scanner and parser vt.errors << errors + vt.notices << notices // Scan each line in file for things to improve source_lines := os.read_lines(vt.file) or { []string{} } for lnumber, line in source_lines { @@ -235,6 +240,7 @@ fn (vt &Vet) e2string(err vet.Error) string { kind = match err.kind { .warning { term.magenta(kind) } .error { term.red(kind) } + .notice { term.yellow(kind) } } kind = term.bold(kind) location = term.bold(location) @@ -275,3 +281,17 @@ fn (mut vt Vet) warn(msg string, line int, fix vet.FixKind) { vt.warns << w } } + +fn (mut vt Vet) notice(msg string, line int, fix vet.FixKind) { + pos := token.Pos{ + line_nr: line + 1 + } + vt.notices << vet.Error{ + message: msg + file_path: vt.file + pos: pos + kind: .notice + fix: fix + typ: .default + } +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index c30a302bb7a593..bb698b244a93b7 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -106,6 +106,7 @@ pub mut: warnings []errors.Warning notices []errors.Notice vet_errors []vet.Error + vet_notices []vet.Error template_paths []string // record all compiled $tmpl files; needed for `v watch run webserver.v` } @@ -254,7 +255,7 @@ pub fn parse_file(path string, table &ast.Table, comments_mode scanner.CommentsM return res } -pub fn parse_vet_file(path string, table_ &ast.Table, pref_ &pref.Preferences) (&ast.File, []vet.Error) { +pub fn parse_vet_file(path string, table_ &ast.Table, pref_ &pref.Preferences) (&ast.File, []vet.Error, []vet.Error) { $if trace_parse_vet_file ? { eprintln('> ${@MOD}.${@FN} path: ${path}') } @@ -290,7 +291,7 @@ pub fn parse_vet_file(path string, table_ &ast.Table, pref_ &pref.Preferences) ( p.vet_errors << p.scanner.vet_errors file := p.parse() unsafe { p.free_scanner() } - return file, p.vet_errors + return file, p.vet_errors, p.vet_notices } pub fn (mut p Parser) parse() &ast.File { @@ -2068,6 +2069,20 @@ fn (mut p Parser) vet_error(msg string, line int, fix vet.FixKind, typ vet.Error } } +fn (mut p Parser) vet_notice(msg string, line int, fix vet.FixKind, typ vet.ErrorType) { + pos := token.Pos{ + line_nr: line + 1 + } + p.vet_notices << vet.Error{ + message: msg + file_path: p.scanner.file_path + pos: pos + kind: .notice + fix: fix + typ: typ + } +} + fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt { // in here might be 1) multi-expr 2) multi-assign // 1, a, c ... } // multi-expression @@ -3712,6 +3727,10 @@ 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) + } field := ast.ConstField{ name: full_name mod: p.mod diff --git a/vlib/v/vet/vet.v b/vlib/v/vet/vet.v index 280d55743a190b..7e1b16be927a2c 100644 --- a/vlib/v/vet/vet.v +++ b/vlib/v/vet/vet.v @@ -7,6 +7,7 @@ import v.token pub enum ErrorKind { error warning + notice } pub enum FixKind {