Skip to content

Commit

Permalink
Fix false positive with selector expressions (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
charithe authored Feb 19, 2021
1 parent 5becc4e commit a928eef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 1 addition & 2 deletions durationcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ func isAcceptableNestedExpr(pass *analysis.Pass, n ast.Expr) bool {
t := pass.TypesInfo.TypeOf(e)
return !isDuration(t)
case *ast.SelectorExpr:
t := pass.TypesInfo.TypeOf(e)
return !isDuration(t)
return isAcceptableNestedExpr(pass, e.X) && isAcceptableIdent(pass, e.Sel)
}

return false
Expand Down
9 changes: 9 additions & 0 deletions testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package a

import (
"b"
"time"
)

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

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

_ = b.SomeInt * time.Second

_ = time.Second * b.SomeInt
}

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

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

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

_ = time.Second * b.SomeDuration // want `Multiplication of durations`
}

func someDuration() time.Duration {
Expand Down
8 changes: 8 additions & 0 deletions testdata/src/b/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package b

import "time"

const (
SomeInt = 10
SomeDuration = 1 * time.Second
)

0 comments on commit a928eef

Please sign in to comment.