From d3090de02e7037d9acb45edf9972b58badc9245b Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 6 Jul 2022 07:03:36 +0300 Subject: [PATCH] checker: apply the new array check only to `len:`, not `cap:` inits --- vlib/v/checker/containers.v | 5 ++--- vlib/v/checker/tests/ptr_array_init.out | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/vlib/v/checker/containers.v b/vlib/v/checker/containers.v index ce88c2c3b0..f58214e894 100644 --- a/vlib/v/checker/containers.v +++ b/vlib/v/checker/containers.v @@ -58,9 +58,8 @@ pub fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type { } // &int{} check - if node.elem_type.is_any_kind_of_pointer() && !c.inside_unsafe - && (node.has_len || node.has_cap) { - c.warn('arrays of references need to be initialized right away (unless used inside `unsafe`)', + if node.elem_type.is_any_kind_of_pointer() && !c.inside_unsafe && node.has_len { + c.warn('arrays of references need to be initialized right away, therefore `len:` cannot be used (unless inside `unsafe`)', node.pos) } return node.typ diff --git a/vlib/v/checker/tests/ptr_array_init.out b/vlib/v/checker/tests/ptr_array_init.out index ada30a138b..47af51cd2e 100644 --- a/vlib/v/checker/tests/ptr_array_init.out +++ b/vlib/v/checker/tests/ptr_array_init.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/ptr_array_init.vv:2:14: warning: arrays of references need to be initialized right away (unless used inside `unsafe`) +vlib/v/checker/tests/ptr_array_init.vv:2:14: warning: arrays of references need to be initialized right away, therefore `len:` cannot be used (unless inside `unsafe`) 1 | fn main() { 2 | println(*[]&int{len: 1}[0]) | ~~~~~~~