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:
parent
039c9b2550
commit
f5423ed26b
@ -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' {
|
if field.default_expr.val == '0' {
|
||||||
c.warn('unnecessary default value of `0`: struct fields are zeroed by default',
|
c.warn('unnecessary default value of `0`: struct fields are zeroed by default',
|
||||||
field.default_expr.pos)
|
field.default_expr.pos)
|
||||||
}
|
}
|
||||||
} else if field.default_expr is ast.StringLiteral {
|
}
|
||||||
|
ast.StringLiteral {
|
||||||
if field.default_expr.val == '' {
|
if field.default_expr.val == '' {
|
||||||
c.warn("unnecessary default value of '': struct fields are zeroed by default",
|
c.warn("unnecessary default value of '': struct fields are zeroed by default",
|
||||||
field.default_expr.pos)
|
field.default_expr.pos)
|
||||||
}
|
}
|
||||||
} else if field.default_expr is ast.BoolLiteral {
|
}
|
||||||
|
ast.BoolLiteral {
|
||||||
if field.default_expr.val == false {
|
if field.default_expr.val == false {
|
||||||
c.warn('unnecessary default value `false`: 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Ensure each generic type of the field was declared in the struct's definition
|
// 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) {
|
if node.generic_types.len > 0 && field.typ.has_flag(.generic) {
|
||||||
|
@ -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 | s string = ''
|
3 | n_ok int = 1
|
||||||
|
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 | }
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
struct Test {
|
struct Test {
|
||||||
n int = 0
|
n int = 0
|
||||||
|
n_ok int = 1
|
||||||
s string = ''
|
s string = ''
|
||||||
|
s_ok string = 's'
|
||||||
b bool = false
|
b bool = false
|
||||||
|
b_ok bool = true
|
||||||
|
opt ?int = none
|
||||||
|
opt_ok ?int = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
Loading…
Reference in New Issue
Block a user