Skip to content

Add forbidigo linter #1569

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 1 commit into from
Dec 24, 2020
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
6 changes: 6 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,12 @@ linters-settings:
makezero:
# Allow only slices initialized with a length of zero. Default is false.
always: false
forbidigo:
# Forbid the following identifiers
forbid:
- 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

# The custom section can be used to define linter plugins to be loaded at runtime. See README doc
# for more info.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +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/makezero v0.0.0-20201205152432-7b7cdbb3025a
github.com/bombsimon/wsl/v3 v3.1.0
github.com/daixiang0/gci v0.2.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum

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

5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ type LintersSettings struct {
ErrorLint ErrorLintSettings
Makezero MakezeroSettings
Thelper ThelperSettings
Forbidigo ForbidigoSettings

Custom map[string]CustomLinterSettings
}
Expand Down Expand Up @@ -406,6 +407,10 @@ type ThelperSettings struct {
} `mapstructure:"benchmark"`
}

type ForbidigoSettings struct {
Forbid []string `mapstructure:"forbid"`
}

var defaultLintersSettings = LintersSettings{
Lll: LllSettings{
LineLength: 120,
Expand Down
66 changes: 66 additions & 0 deletions pkg/golinters/forbidigo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package golinters

import (
"sync"

"github.com/ashanbrown/forbidigo/forbidigo"
"github.com/pkg/errors"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)

const forbidigoName = "forbidigo"

func NewForbidigo() *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue

analyzer := &analysis.Analyzer{
Name: forbidigoName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
return goanalysis.NewLinter(
forbidigoName,
"Forbids identifiers",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
s := &lintCtx.Settings().Forbidigo

analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var res []goanalysis.Issue
forbid, err := forbidigo.NewLinter(s.Forbid)
if err != nil {
return nil, errors.Wrapf(err, "failed to create linter %q", forbidigoName)
}

for _, file := range pass.Files {
hints, err := forbid.Run(pass.Fset, file)
if err != nil {
return nil, errors.Wrapf(err, "forbidigo linter failed on file %q", file.Name.String())
}
for _, hint := range hints {
res = append(res, goanalysis.NewIssue(&result.Issue{
Pos: hint.Position(),
Text: hint.Details(),
FromLinter: makezeroName,
}, pass))
}
}

if len(res) == 0 {
return nil, nil
}

mu.Lock()
resIssues = append(resIssues, res...)
mu.Unlock()
return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return resIssues
}).WithLoadMode(goanalysis.LoadModeSyntax)
}
3 changes: 3 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
linter.NewConfig(golinters.NewMakezero()).
WithPresets(linter.PresetStyle, linter.PresetBugs).
WithURL("https://github.com/ashanbrown/makezero"),
linter.NewConfig(golinters.NewForbidigo()).
WithPresets(linter.PresetStyle).
WithURL("https://github.com/ashanbrown/forbidigo"),

// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint()).
Expand Down
4 changes: 4 additions & 0 deletions test/testdata/configs/forbidigo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters-settings:
forbidigo:
forbid:
- fmt\.Print.*
9 changes: 9 additions & 0 deletions test/testdata/forbidigo.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 Forbidigo() {
fmt.Printf("too noisy!!!") // ERROR "use of `fmt.Printf` forbidden by pattern `fmt\\.Print.*`"
}