From 018b7bd892d2864d42fd7030d9c3c34059f06c3a Mon Sep 17 00:00:00 2001 From: zhangyunhao Date: Thu, 5 Nov 2020 16:36:42 +0800 Subject: [PATCH 1/3] patch-1 --- pkg/config/config.go | 6 ++++ pkg/lint/runner.go | 17 +++++----- pkg/result/processors/exclude.go | 45 ++++++++++++++++++--------- pkg/result/processors/exclude_test.go | 6 ++-- 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 5b73a36de349..dbbce244eddc 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -99,6 +99,12 @@ var DefaultExcludePatterns = []ExcludePattern{ Linter: "gosec", Why: "False positive is triggered by 'src, err := ioutil.ReadFile(filename)'", }, + { + ID: "EXC0011", + Pattern: "at least one file in a package should have a package comment", + Linter: "stylecheck", + Why: "Annoying issue about not having a package comment.", + }, } func GetDefaultExcludePatternsStrings() []string { diff --git a/pkg/lint/runner.go b/pkg/lint/runner.go index 084912226b63..ca20168ee8fe 100644 --- a/pkg/lint/runner.go +++ b/pkg/lint/runner.go @@ -225,21 +225,22 @@ func (r *Runner) processIssues(issues []result.Issue, sw *timeutils.Stopwatch, s } func getExcludeProcessor(cfg *config.Issues) processors.Processor { - excludePatterns := cfg.ExcludePatterns - if cfg.UseDefaultExcludes { - excludePatterns = append(excludePatterns, config.GetExcludePatternsStrings(cfg.IncludeDefaultExcludes)...) + var excludeTotalPattern string + excludeGlobalPatterns := cfg.ExcludePatterns + if len(excludeGlobalPatterns) != 0 { + excludeTotalPattern = fmt.Sprintf("(%s)", strings.Join(excludeGlobalPatterns, "|")) } - var excludeTotalPattern string - if len(excludePatterns) != 0 { - excludeTotalPattern = fmt.Sprintf("(%s)", strings.Join(excludePatterns, "|")) + var excludePatterns []config.ExcludePattern + if cfg.UseDefaultExcludes { + excludePatterns = config.DefaultExcludePatterns } var excludeProcessor processors.Processor if cfg.ExcludeCaseSensitive { - excludeProcessor = processors.NewExcludeCaseSensitive(excludeTotalPattern) + excludeProcessor = processors.NewExcludeCaseSensitive(excludeTotalPattern, excludePatterns) } else { - excludeProcessor = processors.NewExclude(excludeTotalPattern) + excludeProcessor = processors.NewExclude(excludeTotalPattern, excludePatterns) } return excludeProcessor diff --git a/pkg/result/processors/exclude.go b/pkg/result/processors/exclude.go index 92959a328ca2..efaf577a279f 100644 --- a/pkg/result/processors/exclude.go +++ b/pkg/result/processors/exclude.go @@ -3,23 +3,31 @@ package processors import ( "regexp" + "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/result" ) type Exclude struct { + globalPattern *regexp.Regexp + patterns []excludePattern +} + +type excludePattern struct { pattern *regexp.Regexp + linter string } var _ Processor = Exclude{} -func NewExclude(pattern string) *Exclude { - var patternRe *regexp.Regexp - if pattern != "" { - patternRe = regexp.MustCompile("(?i)" + pattern) +func NewExclude(globalPattern string, patterns []config.ExcludePattern) *Exclude { + exc := &Exclude{patterns: make([]excludePattern, 0, len(patterns))} + if globalPattern != "" { + exc.globalPattern = regexp.MustCompile("(?i)" + globalPattern) } - return &Exclude{ - pattern: patternRe, + for _, r := range patterns { + exc.patterns = append(exc.patterns, excludePattern{pattern: regexp.MustCompile("(?i)" + r.Pattern), linter: r.Linter}) } + return exc } func (p Exclude) Name() string { @@ -27,12 +35,20 @@ func (p Exclude) Name() string { } func (p Exclude) Process(issues []result.Issue) ([]result.Issue, error) { - if p.pattern == nil { + if p.globalPattern == nil && len(p.patterns) == 0 { return issues, nil } return filterIssues(issues, func(i *result.Issue) bool { - return !p.pattern.MatchString(i.Text) + if p.globalPattern != nil && p.globalPattern.MatchString(i.Text) { + return false + } + for _, v := range p.patterns { + if v.linter == i.FromLinter && v.pattern.MatchString(i.Text) { + return false + } + } + return true }), nil } @@ -44,14 +60,15 @@ type ExcludeCaseSensitive struct { var _ Processor = ExcludeCaseSensitive{} -func NewExcludeCaseSensitive(pattern string) *ExcludeCaseSensitive { - var patternRe *regexp.Regexp - if pattern != "" { - patternRe = regexp.MustCompile(pattern) +func NewExcludeCaseSensitive(globalPattern string, patterns []config.ExcludePattern) *ExcludeCaseSensitive { + exc := &ExcludeCaseSensitive{Exclude: &Exclude{patterns: make([]excludePattern, 0, len(patterns))}} + if globalPattern != "" { + exc.globalPattern = regexp.MustCompile(globalPattern) } - return &ExcludeCaseSensitive{ - &Exclude{pattern: patternRe}, + for _, r := range patterns { + exc.patterns = append(exc.patterns, excludePattern{pattern: regexp.MustCompile(r.Pattern), linter: r.Linter}) } + return exc } func (p ExcludeCaseSensitive) Name() string { diff --git a/pkg/result/processors/exclude_test.go b/pkg/result/processors/exclude_test.go index 3b92ccd75f83..390dc6079266 100644 --- a/pkg/result/processors/exclude_test.go +++ b/pkg/result/processors/exclude_test.go @@ -9,7 +9,7 @@ import ( ) func TestExclude(t *testing.T) { - p := NewExclude("^exclude$") + p := NewExclude("^exclude$", nil) texts := []string{"excLude", "1", "", "exclud", "notexclude"} var issues []result.Issue for _, t := range texts { @@ -27,11 +27,11 @@ func TestExclude(t *testing.T) { } func TestNoExclude(t *testing.T) { - processAssertSame(t, NewExclude(""), newIssueFromTextTestCase("test")) + processAssertSame(t, NewExclude("", nil), newIssueFromTextTestCase("test")) } func TestExcludeCaseSensitive(t *testing.T) { - p := NewExcludeCaseSensitive("^exclude$") + p := NewExcludeCaseSensitive("^exclude$", nil) texts := []string{"excLude", "1", "", "exclud", "exclude"} var issues []result.Issue for _, t := range texts { From b5b588982b34fd7b2058f853e4962b2439cc68f7 Mon Sep 17 00:00:00 2001 From: zhangyunhao Date: Thu, 5 Nov 2020 16:48:05 +0800 Subject: [PATCH 2/3] patch-2 --- pkg/golinters/goanalysis/runner.go | 2 +- pkg/golinters/gocognit.go | 2 +- pkg/golinters/gocyclo.go | 2 +- pkg/lint/lintersdb/manager.go | 2 +- pkg/packages/errors.go | 2 +- pkg/printers/github.go | 2 +- pkg/result/processors/sort_results.go | 12 ++++++------ 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/golinters/goanalysis/runner.go b/pkg/golinters/goanalysis/runner.go index db193f37bb20..81225277eb77 100644 --- a/pkg/golinters/goanalysis/runner.go +++ b/pkg/golinters/goanalysis/runner.go @@ -3,7 +3,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// Package checker defines the implementation of the checker commands. +// Package goanalysis defines the implementation of the checker commands. // The same code drives the multi-analysis driver, the single-analysis // driver that is conventionally provided for convenience along with // each analysis package, and the test driver. diff --git a/pkg/golinters/gocognit.go b/pkg/golinters/gocognit.go index 78afff86c59b..d39f5234eaad 100644 --- a/pkg/golinters/gocognit.go +++ b/pkg/golinters/gocognit.go @@ -1,4 +1,4 @@ -// nolint:dupl +// nolint:dupl,stylecheck package golinters import ( diff --git a/pkg/golinters/gocyclo.go b/pkg/golinters/gocyclo.go index 55f13fcfed2f..0160512678b3 100644 --- a/pkg/golinters/gocyclo.go +++ b/pkg/golinters/gocyclo.go @@ -1,4 +1,4 @@ -// nolint:dupl +// nolint:dupl,stylecheck package golinters import ( diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 52a230bd8053..ad197376b120 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -85,7 +85,7 @@ func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config) return ret } -//nolint:funlen +//nolint:funlen,stylecheck func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var govetCfg *config.GovetSettings var testpackageCfg *config.TestpackageSettings diff --git a/pkg/packages/errors.go b/pkg/packages/errors.go index c620573b9380..92239b19991d 100644 --- a/pkg/packages/errors.go +++ b/pkg/packages/errors.go @@ -9,7 +9,7 @@ import ( "github.com/pkg/errors" ) -//nolint:gomnd +//nolint:gomnd,stylecheck func ParseErrorPosition(pos string) (*token.Position, error) { // file:line(:colon) parts := strings.Split(pos, ":") diff --git a/pkg/printers/github.go b/pkg/printers/github.go index b8d70140a80c..4ebc26685784 100644 --- a/pkg/printers/github.go +++ b/pkg/printers/github.go @@ -13,7 +13,7 @@ type github struct { const defaultGithubSeverity = "error" -// Github output format outputs issues according to Github actions format: +// NewGithub output format outputs issues according to Github actions format: // https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message func NewGithub() Printer { return &github{} diff --git a/pkg/result/processors/sort_results.go b/pkg/result/processors/sort_results.go index e726c3adfe05..707b3eadab8d 100644 --- a/pkg/result/processors/sort_results.go +++ b/pkg/result/processors/sort_results.go @@ -91,10 +91,10 @@ var ( type ByName struct{ next comparator } -//nolint:golint +//nolint:golint,stylecheck func (cmp ByName) Next() comparator { return cmp.next } -//nolint:golint +//nolint:golint,stylecheck func (cmp ByName) Compare(a, b *result.Issue) compareResult { var res compareResult @@ -111,10 +111,10 @@ func (cmp ByName) Compare(a, b *result.Issue) compareResult { type ByLine struct{ next comparator } -//nolint:golint +//nolint:golint,stylecheck func (cmp ByLine) Next() comparator { return cmp.next } -//nolint:golint +//nolint:golint,stylecheck,stylecheck func (cmp ByLine) Compare(a, b *result.Issue) compareResult { var res compareResult @@ -131,10 +131,10 @@ func (cmp ByLine) Compare(a, b *result.Issue) compareResult { type ByColumn struct{ next comparator } -//nolint:golint +//nolint:golint,stylecheck func (cmp ByColumn) Next() comparator { return cmp.next } -//nolint:golint +//nolint:golint,stylecheck func (cmp ByColumn) Compare(a, b *result.Issue) compareResult { var res compareResult From c702f61f5ca9b07a7c9cba7b5bc8fc539a37f00d Mon Sep 17 00:00:00 2001 From: zhangyunhao Date: Thu, 5 Nov 2020 18:25:49 +0800 Subject: [PATCH 3/3] patch-3 --- pkg/config/config.go | 16 ++++++++++++++++ pkg/lint/runner.go | 2 +- test/testdata/stylecheck.go | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index dbbce244eddc..2d551e02f946 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -127,6 +127,22 @@ func GetExcludePatternsStrings(include []string) []string { return ret } +func GetExcludePatterns(include []string) []ExcludePattern { + includeMap := make(map[string]bool, len(include)) + for _, inc := range include { + includeMap[inc] = true + } + + var ret []ExcludePattern + for _, p := range DefaultExcludePatterns { + if !includeMap[p.ID] { + ret = append(ret, p) + } + } + + return ret +} + type Run struct { IsVerbose bool `mapstructure:"verbose"` Silent bool diff --git a/pkg/lint/runner.go b/pkg/lint/runner.go index ca20168ee8fe..20d1e06366f5 100644 --- a/pkg/lint/runner.go +++ b/pkg/lint/runner.go @@ -233,7 +233,7 @@ func getExcludeProcessor(cfg *config.Issues) processors.Processor { var excludePatterns []config.ExcludePattern if cfg.UseDefaultExcludes { - excludePatterns = config.DefaultExcludePatterns + excludePatterns = config.GetExcludePatterns(cfg.IncludeDefaultExcludes) } var excludeProcessor processors.Processor diff --git a/test/testdata/stylecheck.go b/test/testdata/stylecheck.go index a0d73053dfd9..f225bf94e86a 100644 --- a/test/testdata/stylecheck.go +++ b/test/testdata/stylecheck.go @@ -1,5 +1,5 @@ //args: -Estylecheck -package testdata +package main // don't need package comment func Stylecheck(x int) { switch x {