Skip to content

Commit

Permalink
Fix false positive with struct fields (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
charithe committed Feb 17, 2021
1 parent b58cb90 commit 5becc4e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions durationcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func isUnacceptableExpr(pass *analysis.Pass, expr ast.Expr) bool {
return !isAcceptableNestedExpr(pass, e)
case *ast.UnaryExpr:
return !isAcceptableNestedExpr(pass, e)
case *ast.SelectorExpr:
return !isAcceptableNestedExpr(pass, e)
}

return true
Expand Down Expand Up @@ -144,6 +146,9 @@ func isAcceptableNestedExpr(pass *analysis.Pass, n ast.Expr) bool {
case *ast.CallExpr:
t := pass.TypesInfo.TypeOf(e)
return !isDuration(t)
case *ast.SelectorExpr:
t := pass.TypesInfo.TypeOf(e)
return !isDuration(t)
}

return false
Expand Down
15 changes: 15 additions & 0 deletions testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ const (
foo = 10
)

type myStruct struct {
fieldA int
fieldB time.Duration
}

func validCases() {
y := 10
ms := myStruct{fieldA: 10, fieldB: 10 * time.Second}

_ = time.Second * 30

Expand Down Expand Up @@ -43,10 +49,15 @@ func validCases() {
_ = foo * time.Second

_ = time.Second * foo

_ = time.Duration(ms.fieldA) * time.Second

_ = time.Second * time.Duration(ms.fieldA)
}

func invalidCases() {
x := 30 * time.Second
ms := myStruct{fieldA: 10, fieldB: 10 * time.Second}

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

Expand All @@ -65,6 +76,10 @@ func invalidCases() {
_ = time.Millisecond * time.Second * 1 // want `Multiplication of durations`

_ = 1 * time.Second * (time.Second) // want `Multiplication of durations`

_ = ms.fieldB * time.Second // want `Multiplication of durations`

_ = time.Second * ms.fieldB // want `Multiplication of durations`
}

func someDuration() time.Duration {
Expand Down

0 comments on commit 5becc4e

Please sign in to comment.