From e018509ba68976020bc5f6a4f21e64defbf55449 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 9 Sep 2020 13:59:52 +0200 Subject: [PATCH] checker: test for unnecessary default struct field values --- vlib/strconv/f32_str.v | 6 +++--- vlib/strconv/f64_str.v | 10 +++++----- vlib/v/checker/checker.v | 12 ++++++++++++ vlib/v/checker/tests/struct_unneeded_default.out | 6 ++++++ vlib/v/checker/tests/struct_unneeded_default.vv | 7 +++++++ 5 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 vlib/v/checker/tests/struct_unneeded_default.out create mode 100644 vlib/v/checker/tests/struct_unneeded_default.vv diff --git a/vlib/strconv/f32_str.v b/vlib/strconv/f32_str.v index 2717c00b6e..aaa6d64dff 100644 --- a/vlib/strconv/f32_str.v +++ b/vlib/strconv/f32_str.v @@ -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 } diff --git a/vlib/strconv/f64_str.v b/vlib/strconv/f64_str.v index 86f531b076..ae718f81ef 100644 --- a/vlib/strconv/f64_str.v +++ b/vlib/strconv/f64_str.v @@ -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 } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index b2d94d96d2..4580fbb8dc 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) + } + } } } } diff --git a/vlib/v/checker/tests/struct_unneeded_default.out b/vlib/v/checker/tests/struct_unneeded_default.out new file mode 100644 index 0000000000..736f6a1dad --- /dev/null +++ b/vlib/v/checker/tests/struct_unneeded_default.out @@ -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 | } diff --git a/vlib/v/checker/tests/struct_unneeded_default.vv b/vlib/v/checker/tests/struct_unneeded_default.vv new file mode 100644 index 0000000000..93f94d0548 --- /dev/null +++ b/vlib/v/checker/tests/struct_unneeded_default.vv @@ -0,0 +1,7 @@ +struct Test { + n int = 0 + s string = '' +} + +fn main() { +}