From 875f7a77a9bbc82b6029d7c2b0cbeb855b65b421 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 20 Dec 2020 10:42:46 +0100 Subject: [PATCH] checker: arr1=arr2 warning --- vlib/builtin/array_test.v | 5 ++++- vlib/os/args.v | 4 ++-- vlib/v/checker/checker.v | 6 ++++++ vlib/v/parser/struct.v | 2 +- vlib/v/vmod/vmod.v | 2 +- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index 75bc710af8..1b768fd2a9 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -361,7 +361,10 @@ fn test_clone() { fn test_copy() { a := [1, 2, 3] - b := [3, 4, 5] + b := a + assert b[0] == 1 + assert b[1] == 2 + assert b[2] == 3 } fn test_mutli_array_clone() { diff --git a/vlib/os/args.v b/vlib/os/args.v index 4dfdf881ef..f36fcea74d 100644 --- a/vlib/os/args.v +++ b/vlib/os/args.v @@ -11,7 +11,7 @@ pub fn args_after(cut_word string) []string { } mut cargs := []string{} if cut_word !in args { - cargs = args + cargs = args.clone() } else { mut found := false cargs << args[0] @@ -37,7 +37,7 @@ pub fn args_before(cut_word string) []string { } mut cargs := []string{} if cut_word !in args { - cargs = args + cargs = args.clone() } else { cargs << args[0] for a in args[1..] { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index ca81b272f1..2cd848d61b 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2209,6 +2209,11 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { // TODO replace all c.pref.translated checks with `$if !translated` for performance continue } + if left_sym.kind == .array && + assign_stmt.op == .assign && right_sym.kind == .array && left is ast.Ident && right is ast.Ident { + // Do not allow `a = b`, only `a = b.clone()` + c.warn('use `array2 = array1.clone()` instead of `array1 = array2`', assign_stmt.pos) + } left_is_ptr := left_type.is_ptr() || left_sym.is_pointer() if left_is_ptr { if !c.inside_unsafe && assign_stmt.op !in [.assign, .decl_assign] { @@ -2321,6 +2326,7 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { } } } + // right_sym := c.table.get_type_symbol(right_type_unwrapped) } fn scope_register_it(mut s ast.Scope, pos token.Position, typ table.Type) { diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 3b408da05c..88b772b8b0 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -97,7 +97,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl { } } if p.tok.kind == .rcbr { - end_comments = comments + end_comments = comments.clone() break } if p.tok.kind == .key_pub { diff --git a/vlib/v/vmod/vmod.v b/vlib/v/vmod/vmod.v index 7e835d4c69..8f35966af6 100644 --- a/vlib/v/vmod/vmod.v +++ b/vlib/v/vmod/vmod.v @@ -154,7 +154,7 @@ fn (mut mcache ModFileCacher) get_files(cfolder string) []string { mut files := []string{} if os.exists(cfolder) && os.is_dir(cfolder) { if listing := os.ls(cfolder) { - files = listing + files = listing.clone() } } mcache.folder_files[cfolder] = files