Re-enable default excludes by ID

This commit is contained in:
John Starich 2020-04-25 13:49:11 -05:00
parent c25bf85999
commit c55e7614d4
4 changed files with 28 additions and 9 deletions

View File

@ -30,7 +30,7 @@ func getDefaultIssueExcludeHelp() string {
parts := []string{"Use or not use default excludes:"} parts := []string{"Use or not use default excludes:"}
for _, ep := range config.DefaultExcludePatterns { for _, ep := range config.DefaultExcludePatterns {
parts = append(parts, parts = append(parts,
fmt.Sprintf(" # %s: %s", ep.Linter, ep.Why), fmt.Sprintf(" # %s %s: %s", ep.ID, ep.Linter, ep.Why),
fmt.Sprintf(" - %s", color.YellowString(ep.Pattern)), fmt.Sprintf(" - %s", color.YellowString(ep.Pattern)),
"", "",
) )

View File

@ -30,6 +30,7 @@ var OutFormats = []string{
} }
type ExcludePattern struct { type ExcludePattern struct {
ID string
Pattern string Pattern string
Linter string Linter string
Why string Why string
@ -37,63 +38,80 @@ type ExcludePattern struct {
var DefaultExcludePatterns = []ExcludePattern{ var DefaultExcludePatterns = []ExcludePattern{
{ {
ID: "EXC0001",
Pattern: "Error return value of .((os\\.)?std(out|err)\\..*|.*Close" + Pattern: "Error return value of .((os\\.)?std(out|err)\\..*|.*Close" +
"|.*Flush|os\\.Remove(All)?|.*printf?|os\\.(Un)?Setenv). is not checked", "|.*Flush|os\\.Remove(All)?|.*printf?|os\\.(Un)?Setenv). is not checked",
Linter: "errcheck", Linter: "errcheck",
Why: "Almost all programs ignore errors on these functions and in most cases it's ok", Why: "Almost all programs ignore errors on these functions and in most cases it's ok",
}, },
{ {
ID: "EXC0002",
Pattern: "(comment on exported (method|function|type|const)|" + Pattern: "(comment on exported (method|function|type|const)|" +
"should have( a package)? comment|comment should be of the form)", "should have( a package)? comment|comment should be of the form)",
Linter: "golint", Linter: "golint",
Why: "Annoying issue about not having a comment. The rare codebase has such comments", Why: "Annoying issue about not having a comment. The rare codebase has such comments",
}, },
{ {
ID: "EXC0003",
Pattern: "func name will be used as test\\.Test.* by other packages, and that stutters; consider calling this", Pattern: "func name will be used as test\\.Test.* by other packages, and that stutters; consider calling this",
Linter: "golint", Linter: "golint",
Why: "False positive when tests are defined in package 'test'", Why: "False positive when tests are defined in package 'test'",
}, },
{ {
ID: "EXC0004",
Pattern: "(possible misuse of unsafe.Pointer|should have signature)", Pattern: "(possible misuse of unsafe.Pointer|should have signature)",
Linter: "govet", Linter: "govet",
Why: "Common false positives", Why: "Common false positives",
}, },
{ {
ID: "EXC0005",
Pattern: "ineffective break statement. Did you mean to break out of the outer loop", Pattern: "ineffective break statement. Did you mean to break out of the outer loop",
Linter: "staticcheck", Linter: "staticcheck",
Why: "Developers tend to write in C-style with an explicit 'break' in a 'switch', so it's ok to ignore", Why: "Developers tend to write in C-style with an explicit 'break' in a 'switch', so it's ok to ignore",
}, },
{ {
ID: "EXC0006",
Pattern: "Use of unsafe calls should be audited", Pattern: "Use of unsafe calls should be audited",
Linter: "gosec", Linter: "gosec",
Why: "Too many false-positives on 'unsafe' usage", Why: "Too many false-positives on 'unsafe' usage",
}, },
{ {
ID: "EXC0007",
Pattern: "Subprocess launch(ed with variable|ing should be audited)", Pattern: "Subprocess launch(ed with variable|ing should be audited)",
Linter: "gosec", Linter: "gosec",
Why: "Too many false-positives for parametrized shell calls", Why: "Too many false-positives for parametrized shell calls",
}, },
{ {
ID: "EXC0008",
Pattern: "G104", Pattern: "G104",
Linter: "gosec", Linter: "gosec",
Why: "Duplicated errcheck checks", Why: "Duplicated errcheck checks",
}, },
{ {
ID: "EXC0009",
Pattern: "(Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)", Pattern: "(Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)",
Linter: "gosec", Linter: "gosec",
Why: "Too many issues in popular repos", Why: "Too many issues in popular repos",
}, },
{ {
ID: "EXC0010",
Pattern: "Potential file inclusion via variable", Pattern: "Potential file inclusion via variable",
Linter: "gosec", Linter: "gosec",
Why: "False positive is triggered by 'src, err := ioutil.ReadFile(filename)'", Why: "False positive is triggered by 'src, err := ioutil.ReadFile(filename)'",
}, },
} }
func GetDefaultExcludePatternsStrings() []string { func GetDefaultExcludePatternsStrings(include []string) []string {
includeMap := make(map[string]bool, len(include))
for _, inc := range include {
includeMap[inc] = true
}
var ret []string var ret []string
for _, p := range DefaultExcludePatterns { for _, p := range DefaultExcludePatterns {
ret = append(ret, p.Pattern) if !includeMap[p.ID] {
ret = append(ret, p.Pattern)
}
} }
return ret return ret
@ -411,10 +429,11 @@ func (e ExcludeRule) Validate() error {
} }
type Issues struct { type Issues struct {
ExcludeCaseSensitive bool `mapstructure:"exclude-case-sensitive"` IncludeDefaultExcludes []string `mapstructure:"include"`
ExcludePatterns []string `mapstructure:"exclude"` ExcludeCaseSensitive bool `mapstructure:"exclude-case-sensitive"`
ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"` ExcludePatterns []string `mapstructure:"exclude"`
UseDefaultExcludes bool `mapstructure:"exclude-use-default"` ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"`
UseDefaultExcludes bool `mapstructure:"exclude-use-default"`
MaxIssuesPerLinter int `mapstructure:"max-issues-per-linter"` MaxIssuesPerLinter int `mapstructure:"max-issues-per-linter"`
MaxSameIssues int `mapstructure:"max-same-issues"` MaxSameIssues int `mapstructure:"max-same-issues"`

View File

@ -32,7 +32,7 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env,
icfg := cfg.Issues icfg := cfg.Issues
excludePatterns := icfg.ExcludePatterns excludePatterns := icfg.ExcludePatterns
if icfg.UseDefaultExcludes { if icfg.UseDefaultExcludes {
excludePatterns = append(excludePatterns, config.GetDefaultExcludePatternsStrings()...) excludePatterns = append(excludePatterns, config.GetDefaultExcludePatternsStrings(icfg.IncludeDefaultExcludes)...)
} }
var excludeTotalPattern string var excludeTotalPattern string

View File

@ -77,7 +77,7 @@ func getGometalinterCommonArgs() []string {
"--vendor", "--vendor",
"--cyclo-over=30", "--cyclo-over=30",
"--dupl-threshold=150", "--dupl-threshold=150",
"--exclude", fmt.Sprintf("(%s)", strings.Join(config.GetDefaultExcludePatternsStrings(), "|")), "--exclude", fmt.Sprintf("(%s)", strings.Join(config.GetDefaultExcludePatternsStrings(nil), "|")),
"--disable-all", "--disable-all",
"--enable=vet", "--enable=vet",
"--enable=vetshadow", "--enable=vetshadow",