Skip to content

Commit

Permalink
vet: give notice for replacing const dynamic arrays with const fixed …
Browse files Browse the repository at this point in the history
…ones (#18960)
  • Loading branch information
Delta456 committed Jul 24, 2023
1 parent d4bedeb commit c6ddbd3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
1 change: 1 addition & 0 deletions 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
1 change: 1 addition & 0 deletions cmd/tools/vvet/tests/const_dynamic_array_notice.vv
@@ -0,0 +1 @@
const a = [1, 2, 3]
28 changes: 24 additions & 4 deletions cmd/tools/vvet/vvet.v
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
23 changes: 21 additions & 2 deletions vlib/v/parser/parser.v
Expand Up @@ -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`
}

Expand Down Expand Up @@ -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}')
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions vlib/v/vet/vet.v
Expand Up @@ -7,6 +7,7 @@ import v.token
pub enum ErrorKind {
error
warning
notice
}

pub enum FixKind {
Expand Down

0 comments on commit c6ddbd3

Please sign in to comment.