diff --git a/go.mod b/go.mod index f9b4749bab59..700dbfcb09e3 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/ashanbrown/forbidigo v1.1.0 github.com/ashanbrown/makezero v0.0.0-20201205152432-7b7cdbb3025a github.com/bombsimon/wsl/v3 v3.1.0 + github.com/charithe/durationcheck v0.0.3 github.com/daixiang0/gci v0.2.8 github.com/denis-tingajkin/go-header v0.4.2 github.com/esimonov/ifshort v1.0.1 diff --git a/go.sum b/go.sum index e30ed185d42d..5483aef12156 100644 --- a/go.sum +++ b/go.sum @@ -44,6 +44,8 @@ github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJm github.com/bombsimon/wsl/v3 v3.1.0 h1:E5SRssoBgtVFPcYWUOFJEcgaySgdtTNYzsSKDOY7ss8= github.com/bombsimon/wsl/v3 v3.1.0/go.mod h1:st10JtZYLE4D5sC7b8xV4zTKZwAQjCH/Hy2Pm1FNZIc= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/charithe/durationcheck v0.0.3 h1:W+9cu38epH7otBF96pdhbFw5vLilsl1oseMOiOgex5E= +github.com/charithe/durationcheck v0.0.3/go.mod h1:0oCYOIgY8Om3hZxPedxKn0mzy0rneKTWJhRm+r6Gl20= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= @@ -572,6 +574,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20200117220505-0cba7a3a9ee9/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200324003944-a576cf524670/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= +golang.org/x/tools v0.0.0-20200403190813-44a64ad78b9b/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200410194907-79a7a3126eef/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200414032229-332987a829c3/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200422022333-3d57cf2e726e/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= diff --git a/pkg/golinters/durationcheck.go b/pkg/golinters/durationcheck.go new file mode 100644 index 000000000000..9c452af50ea5 --- /dev/null +++ b/pkg/golinters/durationcheck.go @@ -0,0 +1,15 @@ +package golinters + +import ( + "github.com/charithe/durationcheck" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewDurationCheck() *goanalysis.Linter { + a := durationcheck.Analyzer + + return goanalysis.NewLinter(a.Name, a.Doc, []*analysis.Analyzer{a}, nil). + WithLoadMode(goanalysis.LoadModeTypesInfo) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index f2d392f18b8e..d2d5d1e1e752 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -357,6 +357,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { linter.NewConfig(golinters.NewRevive(reviveCfg)). WithPresets(linter.PresetStyle). WithURL("https://github.com/mgechev/revive"), + linter.NewConfig(golinters.NewDurationCheck()). + WithPresets(linter.PresetBugs). + WithLoadForGoAnalysis(). + WithURL("https://github.com/charithe/durationcheck"), // nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives linter.NewConfig(golinters.NewNoLintLint()). diff --git a/test/testdata/durationcheck.go b/test/testdata/durationcheck.go new file mode 100644 index 000000000000..8fc6270bfee8 --- /dev/null +++ b/test/testdata/durationcheck.go @@ -0,0 +1,9 @@ +//args: -Edurationcheck +package testdata + +import "time" + +func waitFor(someDuration time.Duration) { + timeToWait := someDuration * time.Second // ERROR "Multiplication of durations: `someDuration \\* time.Second` " + time.Sleep(timeToWait) +}