Skip to content

Commit a928eef

Browse files
authored
Fix false positive with selector expressions (#10)
1 parent 5becc4e commit a928eef

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

durationcheck.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ func isAcceptableNestedExpr(pass *analysis.Pass, n ast.Expr) bool {
147147
t := pass.TypesInfo.TypeOf(e)
148148
return !isDuration(t)
149149
case *ast.SelectorExpr:
150-
t := pass.TypesInfo.TypeOf(e)
151-
return !isDuration(t)
150+
return isAcceptableNestedExpr(pass, e.X) && isAcceptableIdent(pass, e.Sel)
152151
}
153152

154153
return false

testdata/src/a/a.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package a
22

33
import (
4+
"b"
45
"time"
56
)
67

@@ -53,6 +54,10 @@ func validCases() {
5354
_ = time.Duration(ms.fieldA) * time.Second
5455

5556
_ = time.Second * time.Duration(ms.fieldA)
57+
58+
_ = b.SomeInt * time.Second
59+
60+
_ = time.Second * b.SomeInt
5661
}
5762

5863
func invalidCases() {
@@ -80,6 +85,10 @@ func invalidCases() {
8085
_ = ms.fieldB * time.Second // want `Multiplication of durations`
8186

8287
_ = time.Second * ms.fieldB // want `Multiplication of durations`
88+
89+
_ = b.SomeDuration * time.Second // want `Multiplication of durations`
90+
91+
_ = time.Second * b.SomeDuration // want `Multiplication of durations`
8392
}
8493

8594
func someDuration() time.Duration {

testdata/src/b/b.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package b
2+
3+
import "time"
4+
5+
const (
6+
SomeInt = 10
7+
SomeDuration = 1 * time.Second
8+
)

0 commit comments

Comments
 (0)