mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: test for unnecessary default struct field values
This commit is contained in:
parent
eb95a4333a
commit
e018509ba6
@ -22,14 +22,14 @@ module strconv
|
||||
// dec32 is a floating decimal type representing m * 10^e.
|
||||
struct Dec32 {
|
||||
mut:
|
||||
m u32 = 0
|
||||
e int = 0
|
||||
m u32
|
||||
e int
|
||||
}
|
||||
|
||||
// support union for convert f32 to u32
|
||||
union Uf32 {
|
||||
mut:
|
||||
f f32 = 0
|
||||
f f32
|
||||
u u32
|
||||
}
|
||||
|
||||
|
@ -21,21 +21,21 @@ module strconv
|
||||
|
||||
struct Uint128 {
|
||||
mut:
|
||||
lo u64 = u64(0)
|
||||
hi u64 = u64(0)
|
||||
lo u64
|
||||
hi u64
|
||||
}
|
||||
|
||||
// dec64 is a floating decimal type representing m * 10^e.
|
||||
struct Dec64 {
|
||||
mut:
|
||||
m u64 = 0
|
||||
e int = 0
|
||||
m u64
|
||||
e int
|
||||
}
|
||||
|
||||
// support union for convert f64 to u64
|
||||
union Uf64 {
|
||||
mut:
|
||||
f f64 = 0
|
||||
f f64
|
||||
u u64
|
||||
}
|
||||
|
||||
|
@ -364,6 +364,18 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) {
|
||||
'has type `$field_expr_type_sym.source_name`, but should be `$field_type_sym.source_name`',
|
||||
field.default_expr.position())
|
||||
}
|
||||
// Check for unnecessary inits like ` = 0` and ` = ''`
|
||||
if field.default_expr is ast.IntegerLiteral as x {
|
||||
if x.val == '0' {
|
||||
c.error('unnecessary default value of `0`: struct fields are zeroed by default',
|
||||
field.pos)
|
||||
}
|
||||
} else if field.default_expr is ast.StringLiteral as x {
|
||||
if x.val == "''" {
|
||||
c.error("unnecessary default value of '': struct fields are zeroed by default",
|
||||
field.pos)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
6
vlib/v/checker/tests/struct_unneeded_default.out
Normal file
6
vlib/v/checker/tests/struct_unneeded_default.out
Normal file
@ -0,0 +1,6 @@
|
||||
vlib/v/checker/tests/struct_unneeded_default.vv:2:2: error: unnecessary default value of `0`: struct fields are zeroed by default
|
||||
1 | struct Test {
|
||||
2 | n int = 0
|
||||
| ~~~~~~
|
||||
3 | s string = ''
|
||||
4 | }
|
7
vlib/v/checker/tests/struct_unneeded_default.vv
Normal file
7
vlib/v/checker/tests/struct_unneeded_default.vv
Normal file
@ -0,0 +1,7 @@
|
||||
struct Test {
|
||||
n int = 0
|
||||
s string = ''
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
Loading…
Reference in New Issue
Block a user