Skip to content

Commit 9e0c9f3

Browse files
authored
Expose more config options for forbidigo (#1677)
1 parent 443e5b6 commit 9e0c9f3

7 files changed

+31
-23
lines changed

.golangci.example.yml

+2
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ linters-settings:
414414
- fmt.Errorf # consider errors.Errorf in github.com/pkg/errors
415415
- fmt.Print.* # too much log noise
416416
- ginkgo\\.F.* # these are used just for local development
417+
# Exclude godoc examples from forbidigo checks. Default is true.
418+
exclude_godoc_examples: false
417419

418420
# The custom section can be used to define linter plugins to be loaded at runtime. See README doc
419421
# for more info.

go.sum

-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/config.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ type IfshortSettings struct {
416416
}
417417

418418
type ForbidigoSettings struct {
419-
Forbid []string `mapstructure:"forbid"`
419+
Forbid []string `mapstructure:"forbid"`
420+
ExcludeGodocExamples bool `mapstructure:"exclude-godoc-examples"`
420421
}
421422

422423
type PredeclaredSettings struct {
@@ -488,6 +489,9 @@ var defaultLintersSettings = LintersSettings{
488489
Ignore: "",
489490
Qualified: false,
490491
},
492+
Forbidigo: ForbidigoSettings{
493+
ExcludeGodocExamples: true,
494+
},
491495
}
492496

493497
type CustomLinterSettings struct {

pkg/golinters/forbidigo.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ func NewForbidigo() *goanalysis.Linter {
3131

3232
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
3333
var res []goanalysis.Issue
34-
forbid, err := forbidigo.NewLinter(s.Forbid)
34+
options := []forbidigo.Option{
35+
forbidigo.OptionExcludeGodocExamples(s.ExcludeGodocExamples),
36+
// disable "//permit" directives so only "//nolint" directives matters within golangci lint
37+
forbidigo.OptionIgnorePermitDirectives(true),
38+
}
39+
forbid, err := forbidigo.NewLinter(s.Forbid, options...)
3540
if err != nil {
3641
return nil, errors.Wrapf(err, "failed to create linter %q", linterName)
3742
}
File renamed without changes.
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//args: -Eforbidigo
2+
//config_path: testdata/configs/forbidigo.yml
3+
package testdata
4+
5+
import "fmt"
6+
7+
func ExampleForbidigo() {
8+
fmt.Printf("too noisy!!!") // godoc examples are ignored (in *_test.go files only)
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//args: -Eforbidigo
2+
//config: linters-settings.forbidigo.exclude-godoc-examples=false
3+
package testdata
4+
5+
import "fmt"
6+
7+
func ExampleForbidigoNoGodoc() {
8+
fmt.Printf("too noisy!!!") // ERROR "use of `fmt.Printf` forbidden by pattern.*"
9+
}

0 commit comments

Comments
 (0)