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' { if field.typ.has_flag(.option) {
c.warn('unnecessary default value of `0`: struct fields are zeroed by default', if field.default_expr is ast.None {
c.warn('unnecessary default value of `none`: struct fields are zeroed by default',
field.default_expr.pos) field.default_expr.pos)
} }
} else if field.default_expr is ast.StringLiteral { } else if field.typ.has_flag(.result) {
if field.default_expr.val == '' { // struct field does not support result. Nothing to do
c.warn("unnecessary default value of '': struct fields are zeroed by default", } else {
field.default_expr.pos) match field.default_expr {
} ast.IntegerLiteral {
} else if field.default_expr is ast.BoolLiteral { if field.default_expr.val == '0' {
if field.default_expr.val == false { c.warn('unnecessary default value of `0`: struct fields are zeroed by default',
c.warn('unnecessary default value `false`: struct fields are zeroed by default', field.default_expr.pos)
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 { 1 | struct Test {
2 | n int = 0 2 | n int = 0
| ^ | ^
3 | s string = '' 3 | n_ok int = 1
4 | b bool = false 4 | s string = ''
vlib/v/checker/tests/struct_unneeded_default.vv:3:13: warning: unnecessary default value of '': struct fields are zeroed by default vlib/v/checker/tests/struct_unneeded_default.vv:4:18: warning: unnecessary default value of '': struct fields are zeroed by default
1 | struct Test { 2 | n int = 0
2 | n int = 0 3 | n_ok int = 1
3 | s string = '' 4 | s string = ''
| ~~ | ~~
4 | b bool = false 5 | s_ok string = 's'
5 | } 6 | b bool = false
vlib/v/checker/tests/struct_unneeded_default.vv:4:11: warning: unnecessary default value `false`: struct fields are zeroed by default vlib/v/checker/tests/struct_unneeded_default.vv:6:18: warning: unnecessary default value `false`: struct fields are zeroed by default
2 | n int = 0 4 | s string = ''
3 | s string = '' 5 | s_ok string = 's'
4 | b bool = false 6 | b bool = false
| ~~~~~ | ~~~~~
5 | } 7 | b_ok bool = true
6 | 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 { struct Test {
n int = 0 n int = 0
s string = '' n_ok int = 1
b bool = false s string = ''
s_ok string = 's'
b bool = false
b_ok bool = true
opt ?int = none
opt_ok ?int = 1
} }
fn main() { fn main() {