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,22 +161,37 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
}
}
}
if field.default_expr is ast.IntegerLiteral {
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.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)
}
} else if field.default_expr is ast.StringLiteral {
}
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 {
}
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 {}
}
}
}
// Ensure each generic type of the field was declared in the struct's definition
if node.generic_types.len > 0 && field.typ.has_flag(.generic) {

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 {
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 | s string = ''
3 | n_ok int = 1
4 | 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 | 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
| ~~~~~
5 | }
6 |
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
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() {