Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 0e60dde

Browse files
committed
Fix false positives
1 parent c1bcdfa commit 0e60dde

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

pkg/analyzer/analyzer.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ func (nom namedOccurrenceMap) checkStatement(stmt ast.Stmt, ifPos token.Pos) {
8585
nom.checkExpression(a, ifPos)
8686
}
8787
case *ast.ExprStmt:
88-
if callExpr, ok := v.X.(*ast.CallExpr); ok {
89-
nom.checkExpression(callExpr, ifPos)
88+
switch v.X.(type) {
89+
case *ast.CallExpr, *ast.UnaryExpr:
90+
nom.checkExpression(v.X, ifPos)
9091
}
9192
case *ast.ForStmt:
9293
for _, el := range v.Body.List {
@@ -157,12 +158,31 @@ func (nom namedOccurrenceMap) checkStatement(stmt ast.Stmt, ifPos token.Pos) {
157158
}
158159

159160
for _, c := range clauses.Body {
160-
if est, ok := c.(*ast.ExprStmt); ok {
161-
nom.checkExpression(est.X, ifPos)
161+
switch v := c.(type) {
162+
case *ast.AssignStmt:
163+
for _, el := range v.Lhs {
164+
nom.checkExpression(el, ifPos)
165+
}
166+
for _, el := range v.Rhs {
167+
nom.checkExpression(el, ifPos)
168+
}
169+
case *ast.ExprStmt:
170+
nom.checkExpression(v.X, ifPos)
162171
}
172+
}
173+
}
174+
case *ast.SelectStmt:
175+
for _, el := range v.Body.List {
176+
clause := el.(*ast.CommClause)
163177

178+
nom.checkStatement(clause.Comm, ifPos)
179+
180+
for _, c := range clause.Body {
164181
switch v := c.(type) {
165182
case *ast.AssignStmt:
183+
for _, el := range v.Lhs {
184+
nom.checkExpression(el, ifPos)
185+
}
166186
for _, el := range v.Rhs {
167187
nom.checkExpression(el, ifPos)
168188
}
@@ -171,6 +191,8 @@ func (nom namedOccurrenceMap) checkStatement(stmt ast.Stmt, ifPos token.Pos) {
171191
}
172192
}
173193
}
194+
case *ast.LabeledStmt:
195+
nom.checkStatement(v.Stmt, ifPos)
174196
}
175197
}
176198

@@ -190,7 +212,7 @@ func (nom namedOccurrenceMap) checkExpression(candidate ast.Expr, ifPos token.Po
190212
case *ast.CompositeLit:
191213
for _, el := range v.Elts {
192214
switch v := el.(type) {
193-
case *ast.Ident:
215+
case *ast.Ident, *ast.CompositeLit:
194216
nom.checkExpression(v, ifPos)
195217
case *ast.KeyValueExpr:
196218
nom.checkExpression(v.Key, ifPos)
@@ -217,6 +239,8 @@ func (nom namedOccurrenceMap) checkExpression(candidate ast.Expr, ifPos token.Po
217239
}
218240
}
219241
}
242+
case *ast.StarExpr:
243+
nom.checkExpression(v.X, ifPos)
220244
case *ast.IndexExpr:
221245
nom.checkExpression(v.X, ifPos)
222246
switch index := v.Index.(type) {

testdata/dummies.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ func getBool(...interface{}) bool { return false }
1212

1313
func getInt(...interface{}) int { return 0 }
1414

15+
func getChan(...interface{}) chan interface{} { return nil }
16+
1517
type dummyType struct {
1618
interf interface{}
1719
slice []interface{}
1820
}
1921

2022
func getDummy(...interface{}) dummyType { return dummyType{} }
2123

24+
func getDummyPtr(...interface{}) *dummyType { return &dummyType{} }
25+
2226
func getTwoDummies(...interface{}) (dummyType, dummyType) { return dummyType{}, dummyType{} }
2327

2428
func (dt dummyType) noOp(...interface{}) {}

testdata/testdata.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,52 @@ func notUsed_BinaryExprInAssign_OK() {
518518
noOp1(v1)
519519
}
520520
}
521+
522+
func notUsed_ReferenceInSelect_OK() {
523+
v := 0
524+
if getBool(v) {
525+
v = 1
526+
}
527+
select {
528+
case <-getChan(v):
529+
}
530+
}
531+
532+
func notUsed_AssignInSwitch_OK() {
533+
y := 100
534+
switch {
535+
case true:
536+
y = 1
537+
}
538+
if y < 5 {
539+
}
540+
}
541+
542+
func notUsed_PassPointer_OK() {
543+
a := getDummyPtr()
544+
if a == nil {
545+
}
546+
noOp1(*a)
547+
}
548+
549+
func notUsed_Labels_OK() {
550+
foo := true
551+
BREAKOUT:
552+
if getBool() {
553+
foo = false
554+
goto BREAKOUT
555+
}
556+
if foo {
557+
return
558+
}
559+
}
560+
561+
func notUsed_UnnerStruct_OK() {
562+
v := getInt()
563+
if v == 0 {
564+
v = 1
565+
}
566+
type Wrapper struct{ V int }
567+
t := []Wrapper{{v}}
568+
noOp1(t)
569+
}

0 commit comments

Comments
 (0)