From 768c136e98838eb6ec05367aaed383ab3cb8cce1 Mon Sep 17 00:00:00 2001 From: "Andrew S. Brown" Date: Sun, 31 Jan 2021 21:38:00 -0800 Subject: [PATCH 1/3] Upgrade forbidigo --- .golangci.example.yml | 2 ++ go.mod | 2 +- go.sum | 3 +++ pkg/config/config.go | 6 +++++- pkg/golinters/forbidigo.go | 7 ++++++- test/testdata/forbidigo_include_godoc_examples_test.go | 9 +++++++++ test/testdata/forbidigo_test.go | 9 +++++++++ 7 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 test/testdata/forbidigo_include_godoc_examples_test.go create mode 100644 test/testdata/forbidigo_test.go diff --git a/.golangci.example.yml b/.golangci.example.yml index 02e8b88cd69e..d820e8366c5c 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -414,6 +414,8 @@ linters-settings: - fmt.Errorf # consider errors.Errorf in github.com/pkg/errors - fmt.Print.* # too much log noise - ginkgo\\.F.* # these are used just for local development + # Exclude godoc examples from forbidigo checks. Default is true. + exclude_godoc_examples: false # The custom section can be used to define linter plugins to be loaded at runtime. See README doc # for more info. diff --git a/go.mod b/go.mod index 2ba98ab0c4ac..fb45907e8e33 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( 4d63.com/gochecknoglobals v0.0.0-20201008074935-acfc0b28355a github.com/Djarvur/go-err113 v0.0.0-20200511133814-5174e21577d5 github.com/OpenPeeDeeP/depguard v1.0.1 - github.com/ashanbrown/forbidigo v1.0.0 + 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/daixiang0/gci v0.2.8 diff --git a/go.sum b/go.sum index 3684b160f989..dc7147038a16 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,8 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/ashanbrown/forbidigo v1.0.0 h1:QdNXBduDUopc3GW+YVYZn8jzmIMklQiCfdN2N5+dQeE= github.com/ashanbrown/forbidigo v1.0.0/go.mod h1:PH+zMRWE15yW69fYfe7Kn8nYR6yYyafc3ntEGh2BBAg= +github.com/ashanbrown/forbidigo v1.1.0 h1:SJOPJyqsrVL3CvR0veFZFmIM0fXS/Kvyikqvfphd0Z4= +github.com/ashanbrown/forbidigo v1.1.0/go.mod h1:vVW7PEdqEFqapJe95xHkTfB1+XvZXBFg8t0sG2FIxmI= github.com/ashanbrown/makezero v0.0.0-20201205152432-7b7cdbb3025a h1:/U9tbJzDRof4fOR51vwzWdIBsIH6R2yU0KG1MBRM2Js= github.com/ashanbrown/makezero v0.0.0-20201205152432-7b7cdbb3025a/go.mod h1:oG9Dnez7/ESBqc4EdrdNlryeo7d0KcW1ftXHm7nU/UU= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= @@ -241,6 +243,7 @@ github.com/kyoh86/exportloopref v0.1.8 h1:5Ry/at+eFdkX9Vsdw3qU4YkvGtzuVfzT4X7S77 github.com/kyoh86/exportloopref v0.1.8/go.mod h1:1tUcJeiioIs7VWe5gcOObrux3lb66+sBqGZrRkMwPgg= github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.9.0 h1:L8nSXQQzAYByakOFMTwpjRoHsMJklur4Gi59b6VivR8= github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= diff --git a/pkg/config/config.go b/pkg/config/config.go index 7f5cd42ff4d0..af17729260f1 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -416,7 +416,8 @@ type IfshortSettings struct { } type ForbidigoSettings struct { - Forbid []string `mapstructure:"forbid"` + Forbid []string `mapstructure:"forbid"` + ExcludeGodocExamples bool `mapstructure:"exclude-godoc-examples"` } type PredeclaredSettings struct { @@ -488,6 +489,9 @@ var defaultLintersSettings = LintersSettings{ Ignore: "", Qualified: false, }, + Forbidigo: ForbidigoSettings{ + ExcludeGodocExamples: true, + }, } type CustomLinterSettings struct { diff --git a/pkg/golinters/forbidigo.go b/pkg/golinters/forbidigo.go index 41d036c8bc47..2fa9d51833e7 100644 --- a/pkg/golinters/forbidigo.go +++ b/pkg/golinters/forbidigo.go @@ -31,7 +31,12 @@ func NewForbidigo() *goanalysis.Linter { analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { var res []goanalysis.Issue - forbid, err := forbidigo.NewLinter(s.Forbid) + options := []forbidigo.Option{ + forbidigo.OptionExcludeGodocExamples(s.ExcludeGodocExamples), + // disable "//permit" directives so only "//nolint" directives matters within golangci lint + forbidigo.OptionIgnorePermitDirectives(true), + } + forbid, err := forbidigo.NewLinter(s.Forbid, options...) if err != nil { return nil, errors.Wrapf(err, "failed to create linter %q", linterName) } diff --git a/test/testdata/forbidigo_include_godoc_examples_test.go b/test/testdata/forbidigo_include_godoc_examples_test.go new file mode 100644 index 000000000000..3dd4d7a05fe6 --- /dev/null +++ b/test/testdata/forbidigo_include_godoc_examples_test.go @@ -0,0 +1,9 @@ +//args: -Eforbidigo +//config: linters-settings.forbidigo.exclude-godoc-examples=false +package testdata + +import "fmt" + +func ExampleForbidigoNoGodoc() { + fmt.Printf("too noisy!!!") // ERROR "use of `fmt\\.Printf` forbidden by pattern `fmt\\\\.Print\\.\\*`" +} diff --git a/test/testdata/forbidigo_test.go b/test/testdata/forbidigo_test.go new file mode 100644 index 000000000000..bf1f711d3fbc --- /dev/null +++ b/test/testdata/forbidigo_test.go @@ -0,0 +1,9 @@ +//args: -Eforbidigo +//config_path: testdata/configs/forbidigo.yml +package testdata + +import "fmt" + +func ExampleForbidigo() { + fmt.Printf("too noisy!!!") // godoc examples are ignored (in *_test.go files only) +} From 529e9f3524b791b55a45b1d756792a592d60dbe0 Mon Sep 17 00:00:00 2001 From: "Andrew S. Brown" Date: Sun, 31 Jan 2021 22:09:06 -0800 Subject: [PATCH 2/3] Fix test --- test/testdata/{forbidigo.go => forbidigo_example.go} | 0 test/testdata/{forbidigo_test.go => forbidigo_example_test.go} | 0 test/testdata/forbidigo_include_godoc_examples_test.go | 2 +- 3 files changed, 1 insertion(+), 1 deletion(-) rename test/testdata/{forbidigo.go => forbidigo_example.go} (100%) rename test/testdata/{forbidigo_test.go => forbidigo_example_test.go} (100%) diff --git a/test/testdata/forbidigo.go b/test/testdata/forbidigo_example.go similarity index 100% rename from test/testdata/forbidigo.go rename to test/testdata/forbidigo_example.go diff --git a/test/testdata/forbidigo_test.go b/test/testdata/forbidigo_example_test.go similarity index 100% rename from test/testdata/forbidigo_test.go rename to test/testdata/forbidigo_example_test.go diff --git a/test/testdata/forbidigo_include_godoc_examples_test.go b/test/testdata/forbidigo_include_godoc_examples_test.go index 3dd4d7a05fe6..42049edc1514 100644 --- a/test/testdata/forbidigo_include_godoc_examples_test.go +++ b/test/testdata/forbidigo_include_godoc_examples_test.go @@ -5,5 +5,5 @@ package testdata import "fmt" func ExampleForbidigoNoGodoc() { - fmt.Printf("too noisy!!!") // ERROR "use of `fmt\\.Printf` forbidden by pattern `fmt\\\\.Print\\.\\*`" + fmt.Printf("too noisy!!!") // ERROR "use of `fmt.Printf` forbidden by pattern.*" } From 24f4f90e2d541da1711d5b62d8840e26ba1517c4 Mon Sep 17 00:00:00 2001 From: "Andrew S. Brown" Date: Mon, 1 Feb 2021 07:22:22 -0800 Subject: [PATCH 3/3] Remove trailing whitespace --- .golangci.example.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.golangci.example.yml b/.golangci.example.yml index d820e8366c5c..a8ef0642885e 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -415,7 +415,7 @@ linters-settings: - fmt.Print.* # too much log noise - ginkgo\\.F.* # these are used just for local development # Exclude godoc examples from forbidigo checks. Default is true. - exclude_godoc_examples: false + exclude_godoc_examples: false # The custom section can be used to define linter plugins to be loaded at runtime. See README doc # for more info.