Skip to content

Commit efdda8e

Browse files
committed
Add linter wastedassign
1 parent 257eb95 commit efdda8e

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ require (
4848
github.com/polyfloyd/go-errorlint v0.0.0-20201127212506-19bd8db6546f
4949
github.com/ryancurrah/gomodguard v1.2.0
5050
github.com/ryanrolds/sqlclosecheck v0.3.0
51+
github.com/sanposhiho/wastedassign v0.1.1
5152
github.com/securego/gosec/v2 v2.5.0
5253
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
5354
github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada // v2.19.8

go.sum

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/wastedassign.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package golinters
2+
3+
import (
4+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
5+
"github.com/sanposhiho/wastedassign"
6+
"golang.org/x/tools/go/analysis"
7+
)
8+
9+
func NewWastedAssign() *goanalysis.Linter {
10+
analyzers := []*analysis.Analyzer{
11+
wastedassign.Analyzer,
12+
}
13+
14+
return goanalysis.NewLinter(
15+
"wastedassign",
16+
"wastedassign finds wasted assignment statements.",
17+
analyzers,
18+
nil,
19+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
20+
}

pkg/lint/lintersdb/manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
347347
linter.NewConfig(golinters.NewPredeclared(predeclaredCfg)).
348348
WithPresets(linter.PresetStyle).
349349
WithURL("https://github.com/nishanths/predeclared"),
350+
linter.NewConfig(golinters.NewWastedAssign()).
351+
WithLoadForGoAnalysis().
352+
WithPresets(linter.PresetStyle).
353+
WithURL("https://github.com/sanposhiho/wastedassign"),
350354

351355
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
352356
linter.NewConfig(golinters.NewNoLintLint()).

test/testdata/wastedassign.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//args: -Ewastedassign
2+
package testdata
3+
4+
import (
5+
"strings"
6+
)
7+
8+
func p(x int) int {
9+
return x + 1
10+
}
11+
12+
func multiple(val interface{}, times uint) interface{} {
13+
14+
switch hogehoge := val.(type) {
15+
case int:
16+
return 12
17+
case string:
18+
return strings.Repeat(hogehoge, int(times))
19+
default:
20+
return nil
21+
}
22+
}
23+
24+
func noUseParams(params string) int {
25+
a := 12
26+
println(a)
27+
return a
28+
}
29+
30+
func f(param int) int {
31+
println(param)
32+
useOutOfIf := 1212121 // ERROR "wasted assignment"
33+
ret := 0
34+
if false {
35+
useOutOfIf = 200 // ERROR "reassigned, but never used afterwards"
36+
return 0
37+
} else if param == 100 {
38+
useOutOfIf = 100 // ERROR "wasted assignment"
39+
useOutOfIf = 201
40+
useOutOfIf = p(useOutOfIf)
41+
useOutOfIf += 200 // ERROR "wasted assignment"
42+
} else {
43+
useOutOfIf = 100
44+
useOutOfIf += 100
45+
useOutOfIf = p(useOutOfIf)
46+
useOutOfIf += 200 // ERROR "wasted assignment"
47+
}
48+
49+
if false {
50+
useOutOfIf = 200 // ERROR "reassigned, but never used afterwards"
51+
return 0
52+
} else if param == 200 {
53+
useOutOfIf = 100 // ERROR "wasted assignment"
54+
useOutOfIf = 201
55+
useOutOfIf = p(useOutOfIf)
56+
useOutOfIf += 200
57+
} else {
58+
useOutOfIf = 100
59+
useOutOfIf += 100
60+
useOutOfIf = p(useOutOfIf)
61+
useOutOfIf += 200
62+
}
63+
println(useOutOfIf)
64+
useOutOfIf = 192
65+
useOutOfIf += 100
66+
useOutOfIf += 200 // ERROR "reassigned, but never used afterwards"
67+
return ret
68+
}
69+
70+
func checkLoopTest() int {
71+
hoge := 12
72+
noUse := 1111
73+
println(noUse)
74+
75+
noUse = 1111 // ERROR "reassigned, but never used afterwards"
76+
for {
77+
if hoge == 14 {
78+
break
79+
}
80+
hoge = hoge + 1
81+
}
82+
return hoge
83+
}
84+
85+
func r(param int) int {
86+
println(param)
87+
useOutOfIf := 1212121
88+
ret := 0
89+
if false {
90+
useOutOfIf = 200 // ERROR "reassigned, but never used afterwards"
91+
return 0
92+
} else if param == 100 {
93+
ret = useOutOfIf
94+
} else if param == 200 {
95+
useOutOfIf = 100 // ERROR "wasted assignment"
96+
useOutOfIf = 100
97+
useOutOfIf = p(useOutOfIf)
98+
useOutOfIf += 200 // ERROR "wasted assignment"
99+
}
100+
useOutOfIf = 12
101+
println(useOutOfIf)
102+
useOutOfIf = 192
103+
useOutOfIf += 100
104+
useOutOfIf += 200 // ERROR "reassigned, but never used afterwards"
105+
return ret
106+
}
107+
108+
func mugen() {
109+
var i int
110+
var hoge int
111+
for {
112+
hoge = 5 // ERROR "reassigned, but never used afterwards"
113+
}
114+
115+
println(i)
116+
println(hoge)
117+
return
118+
}
119+
120+
func noMugen() {
121+
var i int
122+
var hoge int
123+
for {
124+
hoge = 5
125+
break
126+
}
127+
128+
println(i)
129+
println(hoge)
130+
return
131+
}

0 commit comments

Comments
 (0)