Skip to content

Commit 351f57b

Browse files
authored
bump wastedassign to v0.2.0 (#1815)
1 parent 921a148 commit 351f57b

File tree

3 files changed

+71
-24
lines changed

3 files changed

+71
-24
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ require (
5757
github.com/polyfloyd/go-errorlint v0.0.0-20201127212506-19bd8db6546f
5858
github.com/ryancurrah/gomodguard v1.2.0
5959
github.com/ryanrolds/sqlclosecheck v0.3.0
60-
github.com/sanposhiho/wastedassign v0.1.3
60+
github.com/sanposhiho/wastedassign v0.2.0
6161
github.com/securego/gosec/v2 v2.6.1
6262
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
6363
github.com/shirou/gopsutil/v3 v3.21.1

go.sum

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/testdata/wastedassign.go

+68-21
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
11
//args: -Ewastedassign
22
package 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

Comments
 (0)