1
0
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:
Swastik Baranwal
2023-07-25 00:43:38 +05:30
committed by GitHub
parent d4bedebace
commit c6ddbd308e
5 changed files with 48 additions and 6 deletions

View File

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

View File

@@ -0,0 +1 @@
const a = [1, 2, 3]

View File

@@ -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
}
}