File tree Expand file tree Collapse file tree 5 files changed +135
-0
lines changed Expand file tree Collapse file tree 5 files changed +135
-0
lines changed Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ require (
54
54
github.com/polyfloyd/go-errorlint v0.0.0-20201127212506-19bd8db6546f
55
55
github.com/ryancurrah/gomodguard v1.2.0
56
56
github.com/ryanrolds/sqlclosecheck v0.3.0
57
+ github.com/sanposhiho/wastedassign v0.1.3
57
58
github.com/securego/gosec/v2 v2.6.1
58
59
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
59
60
github.com/shirou/gopsutil/v3 v3.21.1
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -374,6 +374,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
374
374
WithPresets (linter .PresetBugs ).
375
375
WithLoadForGoAnalysis ().
376
376
WithURL ("https://github.com/charithe/durationcheck" ),
377
+ linter .NewConfig (golinters .NewWastedAssign ()).
378
+ WithPresets (linter .PresetStyle ).
379
+ WithLoadForGoAnalysis ().
380
+ WithURL ("https://github.com/sanposhiho/wastedassign" ),
377
381
378
382
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
379
383
linter .NewConfig (golinters .NewNoLintLint ()).
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments