Skip to content

Expose more config options for forbidigo #1677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 0 additions & 21 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -488,6 +489,9 @@ var defaultLintersSettings = LintersSettings{
Ignore: "",
Qualified: false,
},
Forbidigo: ForbidigoSettings{
ExcludeGodocExamples: true,
},
}

type CustomLinterSettings struct {
Expand Down
7 changes: 6 additions & 1 deletion pkg/golinters/forbidigo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions test/testdata/forbidigo_example_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 9 additions & 0 deletions test/testdata/forbidigo_include_godoc_examples_test.go
Original file line number Diff line number Diff line change
@@ -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.*"
}