Skip to content

Commit 012559c

Browse files
authored
Add linter wastedassign (#1651)
1 parent a3c2ed0 commit 012559c

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ require (
5454
github.com/polyfloyd/go-errorlint v0.0.0-20201127212506-19bd8db6546f
5555
github.com/ryancurrah/gomodguard v1.2.0
5656
github.com/ryanrolds/sqlclosecheck v0.3.0
57+
github.com/sanposhiho/wastedassign v0.1.3
5758
github.com/securego/gosec/v2 v2.6.1
5859
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
5960
github.com/shirou/gopsutil/v3 v3.21.1

go.sum

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

pkg/lint/lintersdb/manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
374374
WithPresets(linter.PresetBugs).
375375
WithLoadForGoAnalysis().
376376
WithURL("https://github.com/charithe/durationcheck"),
377+
linter.NewConfig(golinters.NewWastedAssign()).
378+
WithPresets(linter.PresetStyle).
379+
WithLoadForGoAnalysis().
380+
WithURL("https://github.com/sanposhiho/wastedassign"),
377381

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

test/testdata/wastedassign.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 typeSwitchNoError(val interface{}, times uint) interface{} {
13+
switch hoge := val.(type) {
14+
case int:
15+
return 12
16+
case string:
17+
return strings.Repeat(hoge, int(times))
18+
default:
19+
return nil
20+
}
21+
}
22+
23+
func noUseParamsNoError(params string) int {
24+
a := 12
25+
println(a)
26+
return a
27+
}
28+
29+
func manyif(param int) int {
30+
println(param)
31+
useOutOfIf := 1212121 // ERROR "wasted assignment"
32+
ret := 0
33+
if false {
34+
useOutOfIf = 200 // ERROR "reassigned, but never used afterwards"
35+
return 0
36+
} else if param == 100 {
37+
useOutOfIf = 100 // ERROR "wasted assignment"
38+
useOutOfIf = 201
39+
useOutOfIf = p(useOutOfIf)
40+
useOutOfIf += 200 // ERROR "wasted assignment"
41+
} else {
42+
useOutOfIf = 100
43+
useOutOfIf += 100
44+
useOutOfIf = p(useOutOfIf)
45+
useOutOfIf += 200 // ERROR "wasted assignment"
46+
}
47+
48+
if false {
49+
useOutOfIf = 200 // ERROR "reassigned, but never used afterwards"
50+
return 0
51+
} else if param == 200 {
52+
useOutOfIf = 100 // ERROR "wasted assignment"
53+
useOutOfIf = 201
54+
useOutOfIf = p(useOutOfIf)
55+
useOutOfIf += 200
56+
} else {
57+
useOutOfIf = 100
58+
useOutOfIf += 100
59+
useOutOfIf = p(useOutOfIf)
60+
useOutOfIf += 200
61+
}
62+
println(useOutOfIf)
63+
useOutOfIf = 192
64+
useOutOfIf += 100
65+
useOutOfIf += 200 // ERROR "reassigned, but never used afterwards"
66+
return ret
67+
}
68+
69+
func checkLoopTest() int {
70+
hoge := 12
71+
noUse := 1111
72+
println(noUse)
73+
74+
noUse = 1111 // ERROR "reassigned, but never used afterwards"
75+
for {
76+
if hoge == 14 {
77+
break
78+
}
79+
hoge = hoge + 1
80+
}
81+
return hoge
82+
}
83+
84+
func infinity() {
85+
var i int
86+
var hoge int
87+
for {
88+
hoge = 5 // ERROR "reassigned, but never used afterwards"
89+
}
90+
91+
println(i)
92+
println(hoge)
93+
return
94+
}
95+
96+
func infinity2() {
97+
var i int
98+
var hoge int
99+
for {
100+
hoge = 5
101+
break
102+
}
103+
104+
println(i)
105+
println(hoge)
106+
return
107+
}

0 commit comments

Comments
 (0)