checker: arr1=arr2 warning

pull/7424/head
Alexander Medvednikov 2020-12-20 10:42:46 +01:00
parent 4a5fb854e0
commit 875f7a77a9
5 changed files with 14 additions and 5 deletions

View File

@ -361,7 +361,10 @@ fn test_clone() {
fn test_copy() { fn test_copy() {
a := [1, 2, 3] 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() { fn test_mutli_array_clone() {

View File

@ -11,7 +11,7 @@ pub fn args_after(cut_word string) []string {
} }
mut cargs := []string{} mut cargs := []string{}
if cut_word !in args { if cut_word !in args {
cargs = args cargs = args.clone()
} else { } else {
mut found := false mut found := false
cargs << args[0] cargs << args[0]
@ -37,7 +37,7 @@ pub fn args_before(cut_word string) []string {
} }
mut cargs := []string{} mut cargs := []string{}
if cut_word !in args { if cut_word !in args {
cargs = args cargs = args.clone()
} else { } else {
cargs << args[0] cargs << args[0]
for a in args[1..] { for a in args[1..] {

View File

@ -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 // TODO replace all c.pref.translated checks with `$if !translated` for performance
continue 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() left_is_ptr := left_type.is_ptr() || left_sym.is_pointer()
if left_is_ptr { if left_is_ptr {
if !c.inside_unsafe && assign_stmt.op !in [.assign, .decl_assign] { 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) { fn scope_register_it(mut s ast.Scope, pos token.Position, typ table.Type) {

View File

@ -97,7 +97,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
} }
} }
if p.tok.kind == .rcbr { if p.tok.kind == .rcbr {
end_comments = comments end_comments = comments.clone()
break break
} }
if p.tok.kind == .key_pub { if p.tok.kind == .key_pub {

View File

@ -154,7 +154,7 @@ fn (mut mcache ModFileCacher) get_files(cfolder string) []string {
mut files := []string{} mut files := []string{}
if os.exists(cfolder) && os.is_dir(cfolder) { if os.exists(cfolder) && os.is_dir(cfolder) {
if listing := os.ls(cfolder) { if listing := os.ls(cfolder) {
files = listing files = listing.clone()
} }
} }
mcache.folder_files[cfolder] = files mcache.folder_files[cfolder] = files