github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/staticcheck/sa4006/testdata/src/example.com/CheckUnreadVariableValues/CheckUnreadVariableValues.go (about) 1 package pkg 2 3 import "fmt" 4 5 func fn1() { 6 var x int 7 x = gen() //@ diag(`this value of x is never used`) 8 x = gen() 9 println(x) 10 11 var y int 12 if true { 13 y = gen() //@ diag(`this value of y is never used`) 14 } 15 y = gen() 16 println(y) 17 } 18 19 func gen() int { 20 println() // make it unpure 21 return 0 22 } 23 24 func fn2() { 25 x, y := gen(), gen() //@ diag(`this value of x is never used`), diag(`this value of y is never used`) 26 x, y = gen(), gen() 27 println(x, y) 28 } 29 30 func fn3() { 31 x := uint32(0) 32 if true { 33 x = 1 34 } else { 35 x = 2 36 } 37 println(x) 38 } 39 40 func gen2() (int, int) { 41 println() 42 return 0, 0 43 } 44 45 func fn4() { 46 x, y := gen2() //@ diag(`this value of x is never used`) 47 println(y) 48 x, y = gen2() //@ diag(`this value of x is never used`), diag(`this value of y is never used`) 49 x, _ = gen2() //@ diag(`this value of x is never used`) 50 x, y = gen2() 51 println(x, y) 52 } 53 54 func fn5(m map[string]string) { 55 v, ok := m[""] //@ diag(`this value of v is never used`), diag(`this value of ok is never used`) 56 v, ok = m[""] 57 println(v, ok) 58 } 59 60 func fn6() { 61 x := gen() 62 // Do not report variables if they've been assigned to the blank identifier 63 _ = x 64 } 65 66 func fn7() { 67 func() { 68 var x int 69 x = gen() //@ diag(`this value of x is never used`) 70 x = gen() 71 println(x) 72 }() 73 } 74 75 func fn() int { println(); return 0 } 76 77 var y = func() { 78 v := fn() //@ diag(`never used`) 79 v = fn() 80 println(v) 81 } 82 83 func fn8() { 84 x := gen() 85 switch x { 86 } 87 88 y := gen() //@ diag(`this value of y is never used`) 89 y = gen() 90 switch y { 91 } 92 93 z, _ := gen2() 94 switch z { 95 } 96 97 _, a := gen2() 98 switch a { 99 } 100 101 b, c := gen2() //@ diag(`this value of b is never used`) 102 println(c) 103 b, c = gen2() //@ diag(`this value of c is never used`) 104 switch b { 105 } 106 } 107 108 func fn9() { 109 xs := []int{} 110 for _, x := range xs { 111 foo, err := work(x) //@ diag(`this value of foo is never used`) 112 if err != nil { 113 return 114 } 115 if !foo { 116 continue 117 } 118 } 119 } 120 121 func work(int) (bool, error) { return false, nil } 122 123 func resolveWeakTypes(types []int) { 124 for i := range types { 125 runEnd := findRunLimit(i) 126 127 if true { 128 _ = runEnd 129 } 130 i = runEnd //@ diag(`this value of i is never used`) 131 } 132 } 133 134 func findRunLimit(int) int { return 0 } 135 136 func fn10() { 137 slice := []string(nil) 138 if true { 139 slice = []string{"1", "2"} 140 } else { 141 slice = []string{"3", "4"} 142 } 143 fmt.Println(slice) 144 }