feat: add durationcheck linter. (#1734)

Co-authored-by: Sergey Vilgelm <sergey.vilgelm@ibm.com>
This commit is contained in:
Ludovic Fernandez 2021-02-14 18:47:06 +01:00 committed by GitHub
parent 35b6f35476
commit b0b2dc6b36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 0 deletions

1
go.mod
View File

@ -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

3
go.sum generated
View File

@ -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=

View File

@ -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)
}

View File

@ -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()).

9
test/testdata/durationcheck.go vendored Normal file
View File

@ -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)
}