diff --git a/pkg/commands/run.go b/pkg/commands/run.go index b306802a4580..74beaec75223 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -189,6 +189,7 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is ic := &cfg.Issues fs.StringSliceVarP(&ic.ExcludePatterns, "exclude", "e", nil, wh("Exclude issue by regexp")) fs.BoolVar(&ic.UseDefaultExcludes, "exclude-use-default", true, getDefaultIssueExcludeHelp()) + fs.BoolVar(&ic.ExcludeCaseSensitive, "exclude-case-sensitive", false, wh("If set to true exclude rules are case sensitive")) fs.IntVar(&ic.MaxIssuesPerLinter, "max-issues-per-linter", 50, wh("Maximum issues count per one linter. Set to 0 to disable")) diff --git a/pkg/config/config.go b/pkg/config/config.go index a4c7e45391a1..c421d1df76f2 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -376,9 +376,10 @@ func (e ExcludeRule) Validate() error { } type Issues struct { - ExcludePatterns []string `mapstructure:"exclude"` - ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"` - UseDefaultExcludes bool `mapstructure:"exclude-use-default"` + ExcludeCaseSensitive bool `mapstructure:"exclude-case-sensitive"` + ExcludePatterns []string `mapstructure:"exclude"` + ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"` + UseDefaultExcludes bool `mapstructure:"exclude-use-default"` MaxIssuesPerLinter int `mapstructure:"max-issues-per-linter"` MaxSameIssues int `mapstructure:"max-same-issues"` diff --git a/pkg/lint/runner.go b/pkg/lint/runner.go index 4dc40f3fe04d..d63e85d9f5b2 100644 --- a/pkg/lint/runner.go +++ b/pkg/lint/runner.go @@ -40,6 +40,13 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env, excludeTotalPattern = fmt.Sprintf("(%s)", strings.Join(excludePatterns, "|")) } + var excludeProcessor processors.Processor + if cfg.Issues.ExcludeCaseSensitive { + excludeProcessor = processors.NewExcludeCaseSensitive(excludeTotalPattern) + } else { + excludeProcessor = processors.NewExclude(excludeTotalPattern) + } + skipFilesProcessor, err := processors.NewSkipFiles(cfg.Run.SkipFiles) if err != nil { return nil, err @@ -81,7 +88,7 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env, // Must be before exclude because users see already marked output and configure excluding by it. processors.NewIdentifierMarker(), - processors.NewExclude(excludeTotalPattern), + excludeProcessor, processors.NewExcludeRules(excludeRules, lineCache, log.Child("exclude_rules")), processors.NewNolint(log.Child("nolint"), dbManager), diff --git a/pkg/result/processors/exclude.go b/pkg/result/processors/exclude.go index 80c4b8945d68..a5a6f3a6acd0 100644 --- a/pkg/result/processors/exclude.go +++ b/pkg/result/processors/exclude.go @@ -37,3 +37,21 @@ func (p Exclude) Process(issues []result.Issue) ([]result.Issue, error) { } func (p Exclude) Finish() {} + +type ExcludeCaseSensitive struct { + *Exclude +} + +func NewExcludeCaseSensitive(pattern string) *ExcludeCaseSensitive { + var patternRe *regexp.Regexp + if pattern != "" { + patternRe = regexp.MustCompile(pattern) + } + return &ExcludeCaseSensitive{ + &Exclude{pattern: patternRe}, + } +} + +func (p ExcludeCaseSensitive) Name() string { + return "exclude-case-sensitive" +} diff --git a/pkg/result/processors/exclude_test.go b/pkg/result/processors/exclude_test.go index 7f68d908103b..7965f2fd26bc 100644 --- a/pkg/result/processors/exclude_test.go +++ b/pkg/result/processors/exclude_test.go @@ -51,3 +51,21 @@ func TestExclude(t *testing.T) { func TestNoExclude(t *testing.T) { processAssertSame(t, NewExclude(""), newTextIssue("test")) } + +func TestExcludeCaseSensitive(t *testing.T) { + p := NewExcludeCaseSensitive("^exclude$") + texts := []string{"excLude", "1", "", "exclud", "exclude"} + var issues []result.Issue + for _, t := range texts { + issues = append(issues, newTextIssue(t)) + } + + processedIssues := process(t, p, issues...) + assert.Len(t, processedIssues, len(issues)-1) + + var processedTexts []string + for _, i := range processedIssues { + processedTexts = append(processedTexts, i.Text) + } + assert.Equal(t, texts[:len(texts)-1], processedTexts) +}