diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 4580fbb8dc..b209aade69 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -365,13 +365,16 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) { field.default_expr.position()) } // Check for unnecessary inits like ` = 0` and ` = ''` + if field.typ.is_ptr() { + continue + } 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 == "''" { + if x.val == '' { c.error("unnecessary default value of '': struct fields are zeroed by default", field.pos) } diff --git a/vlib/v/checker/tests/struct_unneeded_default.out b/vlib/v/checker/tests/struct_unneeded_default.out index 736f6a1dad..ed5e0d40f5 100644 --- a/vlib/v/checker/tests/struct_unneeded_default.out +++ b/vlib/v/checker/tests/struct_unneeded_default.out @@ -4,3 +4,10 @@ vlib/v/checker/tests/struct_unneeded_default.vv:2:2: error: unnecessary default | ~~~~~~ 3 | s string = '' 4 | } +vlib/v/checker/tests/struct_unneeded_default.vv:3:2: error: unnecessary default value of '': struct fields are zeroed by default + 1 | struct Test { + 2 | n int = 0 + 3 | s string = '' + | ~~~~~~~~~ + 4 | } + 5 | diff --git a/vlib/v/tests/string_interpolation_of_array_of_structs_test.v b/vlib/v/tests/string_interpolation_of_array_of_structs_test.v index 1e183dc0d3..2279f91891 100644 --- a/vlib/v/tests/string_interpolation_of_array_of_structs_test.v +++ b/vlib/v/tests/string_interpolation_of_array_of_structs_test.v @@ -2,8 +2,8 @@ module main struct Anything { mut: - name string = '' - keepo int = 0 + name string + keepo int } fn (a Anything) str() string { @@ -34,8 +34,8 @@ fn test_array_of_ptrs_to_structs_can_be_printed() { // (note the str method defined on (a &T), instead on (a T)) struct PstrAnything { mut: - name string = '' - keepo int = 0 + name string + keepo int } fn (a &PstrAnything) str() string { @@ -64,7 +64,7 @@ fn test_array_of_ptrs_to_structs_can_be_printed_when_structs_have_str_with_ptr() // fn test_stack_array_of_structs_can_be_printed_when_structs_have_ordinary_str() { - mut t := [3]Anything + mut t := [3]Anything{} t[0] = Anything{ name: '012' } @@ -86,7 +86,7 @@ fn test_stack_array_of_structs_can_be_printed_when_structs_have_ordinary_str() { fn test_stack_array_of_structs_can_be_printed_when_structs_have_str_with_ptr() { // this generates a C error - mut pt := [3]PstrAnything + mut pt := [3]PstrAnything{} pt[0] = PstrAnything{ name: 'P012' }