1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: improve array init warning (#8024)

This commit is contained in:
zakuro 2021-01-13 14:03:23 +09:00 committed by GitHub
parent e8ab79205f
commit 3e3d45b2b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 2 deletions

View File

@ -36,6 +36,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
array_type = table.new_type(idx)
has_type = true
}
last_pos = p.tok.position()
} else {
// [1,2,3] or [const]byte
for i := 0; p.tok.kind !in [.rsbr, .eof]; i++ {
@ -57,6 +58,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
if exprs.len == 1 && p.tok.kind in [.name, .amp, .lsbr] && p.tok.line_nr == line_nr {
// [100]byte
elem_type = p.parse_type()
last_pos = p.tok.position()
is_fixed = true
if p.tok.kind == .lcbr {
p.next()
@ -75,7 +77,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
p.check(.rcbr)
} else {
p.warn_with_pos('use e.g. `x := [1]Type{}` instead of `x := [1]Type`',
last_pos)
first_pos.extend(last_pos))
}
} else {
if p.tok.kind == .not && p.tok.line_nr == p.prev_tok.line_nr {
@ -92,7 +94,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
}
}
if exprs.len == 0 && p.tok.kind != .lcbr && has_type {
p.warn_with_pos('use `x := []Type{}` instead of `x := []Type`', last_pos)
p.warn_with_pos('use `x := []Type{}` instead of `x := []Type`', first_pos.extend(last_pos))
}
mut has_len := false
mut has_cap := false

View File

@ -0,0 +1,12 @@
vlib/v/parser/tests/array_init.vv:2:7: warning: use `x := []Type{}` instead of `x := []Type`
1 | fn main() {
2 | _ := []int
| ~~~~~
3 | _ := [1]int
4 | }
vlib/v/parser/tests/array_init.vv:3:7: warning: use e.g. `x := [1]Type{}` instead of `x := [1]Type`
1 | fn main() {
2 | _ := []int
3 | _ := [1]int
| ~~~~~~
4 | }

View File

@ -0,0 +1,4 @@
fn main() {
_ := []int
_ := [1]int
}