Skip to content
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

DefaultExcludePatterns should only be used for specified linter #1493

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions pkg/config/config.go
Expand Up @@ -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 {
Expand All @@ -121,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
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/goanalysis/runner.go
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gocognit.go
@@ -1,4 +1,4 @@
// nolint:dupl
// nolint:dupl,stylecheck
package golinters

import (
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gocyclo.go
@@ -1,4 +1,4 @@
// nolint:dupl
// nolint:dupl,stylecheck
package golinters

import (
Expand Down
2 changes: 1 addition & 1 deletion pkg/lint/lintersdb/manager.go
Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions pkg/lint/runner.go
Expand Up @@ -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.GetExcludePatterns(cfg.IncludeDefaultExcludes)
}

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
Expand Down
2 changes: 1 addition & 1 deletion pkg/packages/errors.go
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/pkg/errors"
)

//nolint:gomnd
//nolint:gomnd,stylecheck
func ParseErrorPosition(pos string) (*token.Position, error) {
// file:line(<optional>:colon)
parts := strings.Split(pos, ":")
Expand Down
2 changes: 1 addition & 1 deletion pkg/printers/github.go
Expand Up @@ -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{}
Expand Down
45 changes: 31 additions & 14 deletions pkg/result/processors/exclude.go
Expand Up @@ -3,36 +3,52 @@ 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 {
return "exclude"
}

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
}

Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/result/processors/exclude_test.go
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions pkg/result/processors/sort_results.go
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion 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 {
Expand Down