mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vet: give notice for replacing const dynamic arrays with const fixed ones (#18960)
This commit is contained in:
parent
d4bedebace
commit
c6ddbd308e
1
cmd/tools/vvet/tests/const_dynamic_array_notice.out
Normal file
1
cmd/tools/vvet/tests/const_dynamic_array_notice.out
Normal file
@ -0,0 +1 @@
|
||||
cmd/tools/vvet/tests/const_dynamic_array_notice.vv:1: notice: use a fixed array, instead of a dynamic one
|
1
cmd/tools/vvet/tests/const_dynamic_array_notice.vv
Normal file
1
cmd/tools/vvet/tests/const_dynamic_array_notice.vv
Normal file
@ -0,0 +1 @@
|
||||
const a = [1, 2, 3]
|
@ -16,6 +16,7 @@ struct Vet {
|
||||
mut:
|
||||
errors []vet.Error
|
||||
warns []vet.Error
|
||||
notices []vet.Error
|
||||
file string
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -7,6 +7,7 @@ import v.token
|
||||
pub enum ErrorKind {
|
||||
error
|
||||
warning
|
||||
notice
|
||||
}
|
||||
|
||||
pub enum FixKind {
|
||||
|
Loading…
Reference in New Issue
Block a user