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

checker: fix warning of unnnecessary default none value for option struct fields (#17305)

This commit is contained in:
zakuro 2023-02-15 18:42:00 +09:00 committed by GitHub
parent 039c9b2550
commit f5423ed26b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 34 deletions

View File

@ -161,20 +161,35 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
}
}
}
if field.default_expr is ast.IntegerLiteral {
if field.default_expr.val == '0' {
c.warn('unnecessary default value of `0`: struct fields are zeroed by default',
if field.typ.has_flag(.option) {
if field.default_expr is ast.None {
c.warn('unnecessary default value of `none`: struct fields are zeroed by default',
field.default_expr.pos)
}
} else if field.default_expr is ast.StringLiteral {
if field.default_expr.val == '' {
c.warn("unnecessary default value of '': struct fields are zeroed by default",
field.default_expr.pos)
}
} else if field.default_expr is ast.BoolLiteral {
if field.default_expr.val == false {
c.warn('unnecessary default value `false`: struct fields are zeroed by default',
field.default_expr.pos)
} else if field.typ.has_flag(.result) {
// struct field does not support result. Nothing to do
} else {
match field.default_expr {
ast.IntegerLiteral {
if field.default_expr.val == '0' {
c.warn('unnecessary default value of `0`: struct fields are zeroed by default',
field.default_expr.pos)
}
}
ast.StringLiteral {
if field.default_expr.val == '' {
c.warn("unnecessary default value of '': struct fields are zeroed by default",
field.default_expr.pos)
}
}
ast.BoolLiteral {
if field.default_expr.val == false {
c.warn('unnecessary default value `false`: struct fields are zeroed by default',
field.default_expr.pos)
}
}
else {}
}
}
}

View File

@ -1,20 +1,27 @@
vlib/v/checker/tests/struct_unneeded_default.vv:2:10: warning: unnecessary default value of `0`: struct fields are zeroed by default
vlib/v/checker/tests/struct_unneeded_default.vv:2:18: warning: unnecessary default value of `0`: struct fields are zeroed by default
1 | struct Test {
2 | n int = 0
| ^
3 | s string = ''
4 | b bool = false
vlib/v/checker/tests/struct_unneeded_default.vv:3:13: warning: unnecessary default value of '': struct fields are zeroed by default
1 | struct Test {
2 | n int = 0
3 | s string = ''
| ~~
4 | b bool = false
5 | }
vlib/v/checker/tests/struct_unneeded_default.vv:4:11: warning: unnecessary default value `false`: struct fields are zeroed by default
2 | n int = 0
3 | s string = ''
4 | b bool = false
| ~~~~~
5 | }
6 |
2 | n int = 0
| ^
3 | n_ok int = 1
4 | s string = ''
vlib/v/checker/tests/struct_unneeded_default.vv:4:18: warning: unnecessary default value of '': struct fields are zeroed by default
2 | n int = 0
3 | n_ok int = 1
4 | s string = ''
| ~~
5 | s_ok string = 's'
6 | b bool = false
vlib/v/checker/tests/struct_unneeded_default.vv:6:18: warning: unnecessary default value `false`: struct fields are zeroed by default
4 | s string = ''
5 | s_ok string = 's'
6 | b bool = false
| ~~~~~
7 | b_ok bool = true
8 | opt ?int = none
vlib/v/checker/tests/struct_unneeded_default.vv:8:18: warning: unnecessary default value of `none`: struct fields are zeroed by default
6 | b bool = false
7 | b_ok bool = true
8 | opt ?int = none
| ~~~~
9 | opt_ok ?int = 1
10 | }

View File

@ -1,7 +1,12 @@
struct Test {
n int = 0
s string = ''
b bool = false
n int = 0
n_ok int = 1
s string = ''
s_ok string = 's'
b bool = false
b_ok bool = true
opt ?int = none
opt_ok ?int = 1
}
fn main() {