Skip to content

Commit b884a93

Browse files
sylvia7788wangchunlu
and
wangchunlu
authored
fix false positive with indexical expressions (#16)
Co-authored-by: wangchunlu <[email protected]>
1 parent 8121877 commit b884a93

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

durationcheck.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ func isUnacceptableExpr(pass *analysis.Pass, expr ast.Expr) bool {
9898
return !isAcceptableNestedExpr(pass, e)
9999
case *ast.ParenExpr:
100100
return !isAcceptableNestedExpr(pass, e)
101+
case *ast.IndexExpr:
102+
return !isAcceptableNestedExpr(pass, e)
101103
default:
102104
return true
103105
}
@@ -156,6 +158,9 @@ func isAcceptableNestedExpr(pass *analysis.Pass, n ast.Expr) bool {
156158
return isAcceptableNestedExpr(pass, e.X)
157159
case *ast.ParenExpr:
158160
return isAcceptableNestedExpr(pass, e.X)
161+
case *ast.IndexExpr:
162+
t := pass.TypesInfo.TypeOf(e)
163+
return !isDuration(t)
159164
default:
160165
return false
161166
}

testdata/src/a/a.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type myStruct struct {
1919
func validCases() {
2020
y := 10
2121
ms := myStruct{fieldA: 10, fieldB: 10 * time.Second, fieldC: func(v int) *int { return &v }(10)}
22+
intArr := []int{1}
2223

2324
_ = time.Second * 30
2425

@@ -71,11 +72,14 @@ func validCases() {
7172
_ = b.SomeInt * time.Second
7273

7374
_ = time.Second * b.SomeInt
75+
76+
_ = time.Duration(intArr[0]) * time.Second
7477
}
7578

7679
func invalidCases() {
7780
x := 30 * time.Second
7881
ms := myStruct{fieldA: 10, fieldB: 10 * time.Second}
82+
tdArr := []time.Duration{1}
7983

8084
_ = x * time.Second // want `Multiplication of durations`
8185

@@ -106,6 +110,8 @@ func invalidCases() {
106110
_ = b.SomeDuration * time.Second // want `Multiplication of durations`
107111

108112
_ = time.Second * b.SomeDuration // want `Multiplication of durations`
113+
114+
_ = time.Duration(tdArr[0]) * time.Second // want `Multiplication of durations`
109115
}
110116

111117
func someDuration() time.Duration {

0 commit comments

Comments
 (0)