11//args: -Ewastedassign
22package testdata
33
4- import (
5- "strings"
6- )
4+ import "strings"
75
8- func p (x int ) int {
6+ func pa (x int ) int {
97 return x + 1
108}
119
12- func typeSwitchNoError (val interface {}, times uint ) interface {} {
13- switch hoge := val .(type ) {
10+ func multiple (val interface {}, times uint ) interface {} {
11+
12+ switch hogehoge := val .(type ) {
1413 case int :
1514 return 12
1615 case string :
17- return strings .Repeat (hoge , int (times ))
16+ return strings .Repeat (hogehoge , int (times ))
1817 default :
1918 return nil
2019 }
2120}
2221
23- func noUseParamsNoError (params string ) int {
22+ func noUseParams (params string ) int {
2423 a := 12
2524 println (a )
2625 return a
2726}
2827
29- func manyif (param int ) int {
28+ func f (param int ) int {
3029 println (param )
31- useOutOfIf := 1212121 // ERROR "wasted assignment "
30+ useOutOfIf := 1212121 // ERROR "reassigned, but reassigned without using the value "
3231 ret := 0
3332 if false {
3433 useOutOfIf = 200 // ERROR "reassigned, but never used afterwards"
3534 return 0
3635 } else if param == 100 {
37- useOutOfIf = 100 // ERROR "wasted assignment "
36+ useOutOfIf = 100 // ERROR "reassigned, but reassigned without using the value "
3837 useOutOfIf = 201
39- useOutOfIf = p (useOutOfIf )
40- useOutOfIf += 200 // ERROR "wasted assignment "
38+ useOutOfIf = pa (useOutOfIf )
39+ useOutOfIf += 200 // ERROR "reassigned, but reassigned without using the value "
4140 } else {
4241 useOutOfIf = 100
4342 useOutOfIf += 100
44- useOutOfIf = p (useOutOfIf )
45- useOutOfIf += 200 // ERROR "wasted assignment "
43+ useOutOfIf = pa (useOutOfIf )
44+ useOutOfIf += 200 // ERROR "reassigned, but reassigned without using the value "
4645 }
4746
4847 if false {
4948 useOutOfIf = 200 // ERROR "reassigned, but never used afterwards"
5049 return 0
5150 } else if param == 200 {
52- useOutOfIf = 100 // ERROR "wasted assignment "
51+ useOutOfIf = 100 // ERROR "reassigned, but reassigned without using the value "
5352 useOutOfIf = 201
54- useOutOfIf = p (useOutOfIf )
53+ useOutOfIf = pa (useOutOfIf )
5554 useOutOfIf += 200
5655 } else {
5756 useOutOfIf = 100
5857 useOutOfIf += 100
59- useOutOfIf = p (useOutOfIf )
58+ useOutOfIf = pa (useOutOfIf )
6059 useOutOfIf += 200
6160 }
61+ // useOutOfIf = 12
6262 println (useOutOfIf )
6363 useOutOfIf = 192
6464 useOutOfIf += 100
@@ -81,19 +81,43 @@ func checkLoopTest() int {
8181 return hoge
8282}
8383
84- func infinity () {
84+ func r (param int ) int {
85+ println (param )
86+ useOutOfIf := 1212121
87+ ret := 0
88+ if false {
89+ useOutOfIf = 200 // ERROR "reassigned, but never used afterwards"
90+ return 0
91+ } else if param == 100 {
92+ ret = useOutOfIf
93+ } else if param == 200 {
94+ useOutOfIf = 100 // ERROR "reassigned, but reassigned without using the value"
95+ useOutOfIf = 100
96+ useOutOfIf = pa (useOutOfIf )
97+ useOutOfIf += 200 // ERROR "reassigned, but reassigned without using the value"
98+ }
99+ useOutOfIf = 12
100+ println (useOutOfIf )
101+ useOutOfIf = 192
102+ useOutOfIf += 100
103+ useOutOfIf += 200 // ERROR "reassigned, but never used afterwards"
104+ return ret
105+ }
106+
107+ func mugen () {
85108 var i int
86109 var hoge int
87110 for {
88- hoge = 5 // ERROR "reassigned, but never used afterwards"
111+ hoge = 5 // ERROR "reassigned, but reassigned without using the value"
112+ // break
89113 }
90114
91115 println (i )
92116 println (hoge )
93117 return
94118}
95119
96- func infinity2 () {
120+ func noMugen () {
97121 var i int
98122 var hoge int
99123 for {
@@ -105,3 +129,26 @@ func infinity2() {
105129 println (hoge )
106130 return
107131}
132+
133+ func reassignInsideLoop () {
134+ bar := func (b []byte ) ([]byte , error ) { return b , nil }
135+ var err error
136+ var rest []byte
137+ for {
138+ rest , err = bar (rest )
139+ if err == nil {
140+ break
141+ }
142+ }
143+ return
144+ }
145+
146+ func reassignInsideLoop2 () {
147+ var x int = 0
148+ var y int = 1
149+ for i := 1 ; i < 3 ; i ++ {
150+ x += y
151+ y *= 2 * i
152+ }
153+ println (x )
154+ }
0 commit comments