diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 5b1a77ecef94..b19e682ebd02 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1607,6 +1607,11 @@ linters-settings: # Regexp pattern to skip files. # Default: "(export|internal)_test\\.go" skip-regexp: (export|internal)_test\.go + # List of packages that don't end with _test that tests are allowed to be in. + # Default: "main" + allow-packages: + - example + - main thelper: test: diff --git a/go.mod b/go.mod index 8ed0c150c392..5e87d359c44d 100644 --- a/go.mod +++ b/go.mod @@ -55,7 +55,7 @@ require ( github.com/ldez/tagliatelle v0.3.1 github.com/leonklingele/grouper v1.1.0 github.com/lufeee/execinquery v1.2.1 - github.com/maratori/testpackage v1.0.1 + github.com/maratori/testpackage v1.1.0 github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // v1.0 github.com/mattn/go-colorable v0.1.12 github.com/mbilski/exhaustivestruct v1.2.0 @@ -155,6 +155,7 @@ require ( github.com/quasilyte/gogrep v0.0.0-20220120141003-628d8b3623b5 // indirect github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95 // indirect github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567 // indirect + github.com/sivchari/nosnakecase v1.0.1 github.com/spf13/afero v1.8.2 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/go.sum b/go.sum index 2dee0de2f096..153168370d21 100644 --- a/go.sum +++ b/go.sum @@ -428,8 +428,8 @@ github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2 github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= -github.com/maratori/testpackage v1.0.1 h1:QtJ5ZjqapShm0w5DosRjg0PRlSdAdlx+W6cCKoALdbQ= -github.com/maratori/testpackage v1.0.1/go.mod h1:ddKdw+XG0Phzhx8BFDTKgpWP4i7MpApTE5fXSKAqwDU= +github.com/maratori/testpackage v1.1.0 h1:GJY4wlzQhuBusMF1oahQCBtUV/AQ/k69IZ68vxaac2Q= +github.com/maratori/testpackage v1.1.0/go.mod h1:PeAhzU8qkCwdGEMTEupsHJNlQu2gZopMC6RjbhmHeDc= github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 h1:pWxk9e//NbPwfxat7RXkts09K+dEBJWakUWwICVqYbA= github.com/matoous/godox v0.0.0-20210227103229-6504466cf951/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s= github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE= @@ -617,6 +617,8 @@ github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sivchari/containedctx v1.0.2 h1:0hLQKpgC53OVF1VT7CeoFHk9YKstur1XOgfYIc1yrHI= github.com/sivchari/containedctx v1.0.2/go.mod h1:PwZOeqm4/DLoJOqMSIJs3aKqXRX4YO+uXww087KZ7Bw= +github.com/sivchari/nosnakecase v1.0.1 h1:ziZkXTPHCofuoB5DaozlkrU9+15PbnKxn7D7v67Xi4A= +github.com/sivchari/nosnakecase v1.0.1/go.mod h1:CwDzrzPea40/GB6uynrNLiorAlgFRvRbFSgJx2Gs+QY= github.com/sivchari/tenv v1.6.0 h1:FyE4WysxLwYljKqWhTfOMjgKjBSnmzzg7lWOmpDiAcc= github.com/sivchari/tenv v1.6.0/go.mod h1:64yStXKSOxDfX47NlhVwND4dHwfZDdbp2Lyl018Icvg= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= @@ -671,17 +673,11 @@ github.com/sylvia7788/contextcheck v1.0.4/go.mod h1:vuPKJMQ7MQ91ZTqfdyreNKwZjyUg github.com/tdakkota/asciicheck v0.1.1 h1:PKzG7JUTUmVspQTDqtkX9eSiLGossXTybutHwTXuO0A= github.com/tdakkota/asciicheck v0.1.1/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM= github.com/tenntenn/modver v1.0.1 h1:2klLppGhDgzJrScMpkj9Ujy3rXPUspSjAcev9tSEBgA= -github.com/tenntenn/modver v1.0.1 h1:2klLppGhDgzJrScMpkj9Ujy3rXPUspSjAcev9tSEBgA= -github.com/tenntenn/modver v1.0.1/go.mod h1:bePIyQPb7UeioSRkw3Q0XeMhYZSMx9B8ePqg6SAMGH0= github.com/tenntenn/modver v1.0.1/go.mod h1:bePIyQPb7UeioSRkw3Q0XeMhYZSMx9B8ePqg6SAMGH0= github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpRQGxTSkNYKJ51yaw6ChIqO+Je8UqsTKN/cDag= -github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpRQGxTSkNYKJ51yaw6ChIqO+Je8UqsTKN/cDag= github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY= -github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY= -github.com/tetafro/godot v1.4.11 h1:BVoBIqAf/2QdbFmSwAWnaIqDivZdOV0ZRwEm6jivLKw= github.com/tetafro/godot v1.4.11 h1:BVoBIqAf/2QdbFmSwAWnaIqDivZdOV0ZRwEm6jivLKw= github.com/tetafro/godot v1.4.11/go.mod h1:LR3CJpxDVGlYOWn3ZZg1PgNZdTUvzsZWu8xaEohUpn8= -github.com/tetafro/godot v1.4.11/go.mod h1:LR3CJpxDVGlYOWn3ZZg1PgNZdTUvzsZWu8xaEohUpn8= github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144 h1:kl4KhGNsJIbDHS9/4U9yQo1UcPQM0kOMJHn29EoH/Ro= github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk= github.com/tklauser/go-sysconf v0.3.10 h1:IJ1AZGZRWbY8T5Vfk04D9WOA5WSejdflXxP03OUqALw= @@ -1028,7 +1024,6 @@ golang.org/x/tools v0.0.0-20200324003944-a576cf524670/go.mod h1:Sl4aGygMT6LrqrWc golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= 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= golang.org/x/tools v0.0.0-20200426102838-f3a5411a4c3b/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200501065659-ab2804fb9c9d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200512131952-2bc93b1c0c88/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index c91f4f057e7d..3609958c69b8 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -88,7 +88,8 @@ var defaultLintersSettings = LintersSettings{ Qualified: false, }, Testpackage: TestpackageSettings{ - SkipRegexp: `(export|internal)_test\.go`, + SkipRegexp: `(export|internal)_test\.go`, + AllowPackages: []string{"main"}, }, Unparam: UnparamSettings{ Algo: "cha", @@ -558,7 +559,8 @@ type TagliatelleSettings struct { } type TestpackageSettings struct { - SkipRegexp string `mapstructure:"skip-regexp"` + SkipRegexp string `mapstructure:"skip-regexp"` + AllowPackages []string `mapstructure:"allow-packages"` } type ThelperSettings struct { diff --git a/pkg/golinters/nosnakecase.go b/pkg/golinters/nosnakecase.go new file mode 100644 index 000000000000..26d5d6d4c84f --- /dev/null +++ b/pkg/golinters/nosnakecase.go @@ -0,0 +1,19 @@ +package golinters + +import ( + "github.com/sivchari/nosnakecase" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewNoSnakeCase() *goanalysis.Linter { + a := nosnakecase.Analyzer + + return goanalysis.NewLinter( + a.Name, + a.Doc, + []*analysis.Analyzer{a}, + nil, + ).WithLoadMode(goanalysis.LoadModeSyntax) +} diff --git a/pkg/golinters/testpackage.go b/pkg/golinters/testpackage.go index 836403826c14..2cc627595ff1 100644 --- a/pkg/golinters/testpackage.go +++ b/pkg/golinters/testpackage.go @@ -1,6 +1,8 @@ package golinters import ( + "strings" + "github.com/maratori/testpackage/pkg/testpackage" "golang.org/x/tools/go/analysis" @@ -15,7 +17,8 @@ func NewTestpackage(cfg *config.TestpackageSettings) *goanalysis.Linter { if cfg != nil { settings = map[string]map[string]interface{}{ a.Name: { - testpackage.SkipRegexpFlagName: cfg.SkipRegexp, + testpackage.SkipRegexpFlagName: cfg.SkipRegexp, + testpackage.AllowPackagesFlagName: strings.Join(cfg.AllowPackages, ","), }, } } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index e6c069b7f56f..4c8f74fb7c88 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -631,6 +631,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetStyle). WithURL("https://github.com/firefart/nonamedreturns"), + linter.NewConfig(golinters.NewNoSnakeCase()). + WithSince("v1.46.0"). + WithPresets(linter.PresetStyle). + WithURL("https://github.com/sivchari/nosnakecase"), + linter.NewConfig(golinters.NewNoSprintfHostPort()). WithSince("v1.46.0"). WithPresets(linter.PresetStyle). diff --git a/test/testdata/nosnakecase.go b/test/testdata/nosnakecase.go new file mode 100644 index 000000000000..75d81c7b2975 --- /dev/null +++ b/test/testdata/nosnakecase.go @@ -0,0 +1,18 @@ +// args: -Enosnakecase +package testdata + +func a_() { // ERROR "a_ is used under score. You should use mixedCap or MixedCap." +} + +func b(a_a int) { // ERROR "a_a is used under score. You should use mixedCap or MixedCap." +} + +func c() (c_c int) { // ERROR "c_c is used under score. You should use mixedCap or MixedCap." + c_c = 1 // ERROR "c_c is used under score. You should use mixedCap or MixedCap." + return c_c // It's never detected, because `c_c` is already detected. +} + +func d() { + var d_d int // ERROR "d_d is used under score. You should use mixedCap or MixedCap." + _ = d_d // It's never detected, because `_` is meaningful in Go and `d_d` is already detected. +}