github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/staticcheck/sa4018/testdata/src/example.com/CheckSelfAssignment/CheckSelfAssignment.go (about) 1 package pkg 2 3 func fn(x int) { 4 var z int 5 var y int 6 x = x //@ diag(`self-assignment`) 7 y = y //@ diag(`self-assignment`) 8 y, x, z = y, x, 1 //@ diag(`self-assignment of y to y`), diag(`self-assignment of x to x`) 9 y = x 10 _ = y 11 _ = x 12 _ = z 13 func() { 14 x := x 15 println(x) 16 }() 17 } 18 19 func fn1() { 20 var ( 21 x []byte 22 ch chan int 23 ) 24 x[42] = x[42] //@ diag(`self-assignment`) 25 x[pure(42)] = x[pure(42)] //@ diag(`self-assignment`) 26 x[pure(pure(42))] = x[pure(pure(42))] //@ diag(`self-assignment`) 27 x[impure(42)] = x[impure(42)] 28 x[impure(pure(42))] = x[impure(pure(42))] 29 x[pure(impure(42))] = x[pure(impure(42))] 30 x[pure(<-ch)] = x[pure(<-ch)] 31 x[pure(pure(<-ch))] = x[pure(pure(<-ch))] 32 x[<-ch] = x[<-ch] 33 34 type T struct { 35 x []int 36 } 37 var ts []T 38 ts[impure(42)].x = ts[impure(42)].x 39 m := map[*int]int{} 40 m[ptr1()] = m[ptr1()] 41 m[ptr2()] = m[ptr2()] 42 m[new(int)] = m[new(int)] 43 44 m2 := map[int]int{} 45 m2[len(x)] = m2[len(x)] //@ diag(`self-assignment`) 46 47 gen1()[0] = gen1()[0] 48 gen2(0)[0] = gen2(0)[0] //@ diag(`self-assignment`) 49 gen3(0)[0] = gen3(0)[0] 50 } 51 52 func ptr1() *int { 53 return new(int) 54 } 55 56 func ptr2() *int { 57 x := 0 58 return &x 59 } 60 61 func gen1() []int { 62 return nil 63 } 64 65 func gen2(x int) []int { 66 return nil 67 } 68 69 func gen3(x int) []int { 70 return make([]int, 0) 71 } 72 73 func pure(n int) int { 74 return n 75 } 76 77 func impure(n int) int { 78 println(n) 79 return n 80 }