Skip to content

Commit 8121877

Browse files
authored
Fix false positive with parenthetical expressions (#15)
1 parent 2f9d2a0 commit 8121877

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

durationcheck.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ func isUnacceptableExpr(pass *analysis.Pass, expr ast.Expr) bool {
9696
return !isAcceptableNestedExpr(pass, e)
9797
case *ast.StarExpr:
9898
return !isAcceptableNestedExpr(pass, e)
99+
case *ast.ParenExpr:
100+
return !isAcceptableNestedExpr(pass, e)
99101
default:
100102
return true
101103
}
102-
103104
}
104105

105106
// isAcceptableCast returns true if the argument is an acceptable expression cast to time.Duration
@@ -153,6 +154,8 @@ func isAcceptableNestedExpr(pass *analysis.Pass, n ast.Expr) bool {
153154
return isAcceptableNestedExpr(pass, e.X) && isAcceptableIdent(pass, e.Sel)
154155
case *ast.StarExpr:
155156
return isAcceptableNestedExpr(pass, e.X)
157+
case *ast.ParenExpr:
158+
return isAcceptableNestedExpr(pass, e.X)
156159
default:
157160
return false
158161
}

testdata/src/a/a.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package a
22

33
import (
4-
"time"
5-
64
"b"
5+
"time"
76
)
87

98
const (
@@ -31,6 +30,10 @@ func validCases() {
3130

3231
_ = time.Second * time.Duration(10+20*5)
3332

33+
_ = time.Duration((foo + 20)) * time.Second
34+
35+
_ = time.Second * time.Duration((foo + 20))
36+
3437
_ = 2 * 24 * time.Hour
3538

3639
_ = time.Hour * 2 * 24

0 commit comments

Comments
 (0)