Skip to content

Commit 4a3e9fd

Browse files
authored
Merge pull request #1045 from JohnStarich/feature/include-excludes
Re-enable default excludes by ID
2 parents c25bf85 + 36d8d88 commit 4a3e9fd

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -546,34 +546,34 @@ Flags:
546546
--fast Run only fast linters from enabled linters set (first run won't be fast)
547547
-e, --exclude strings Exclude issue by regexp
548548
--exclude-use-default Use or not use default excludes:
549-
# errcheck: Almost all programs ignore errors on these functions and in most cases it's ok
549+
# EXC0001 errcheck: Almost all programs ignore errors on these functions and in most cases it's ok
550550
- Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*printf?|os\.(Un)?Setenv). is not checked
551551
552-
# golint: Annoying issue about not having a comment. The rare codebase has such comments
552+
# EXC0002 golint: Annoying issue about not having a comment. The rare codebase has such comments
553553
- (comment on exported (method|function|type|const)|should have( a package)? comment|comment should be of the form)
554554
555-
# golint: False positive when tests are defined in package 'test'
555+
# EXC0003 golint: False positive when tests are defined in package 'test'
556556
- func name will be used as test\.Test.* by other packages, and that stutters; consider calling this
557557
558-
# govet: Common false positives
558+
# EXC0004 govet: Common false positives
559559
- (possible misuse of unsafe.Pointer|should have signature)
560560
561-
# staticcheck: Developers tend to write in C-style with an explicit 'break' in a 'switch', so it's ok to ignore
561+
# EXC0005 staticcheck: Developers tend to write in C-style with an explicit 'break' in a 'switch', so it's ok to ignore
562562
- ineffective break statement. Did you mean to break out of the outer loop
563563
564-
# gosec: Too many false-positives on 'unsafe' usage
564+
# EXC0006 gosec: Too many false-positives on 'unsafe' usage
565565
- Use of unsafe calls should be audited
566566
567-
# gosec: Too many false-positives for parametrized shell calls
567+
# EXC0007 gosec: Too many false-positives for parametrized shell calls
568568
- Subprocess launch(ed with variable|ing should be audited)
569569
570-
# gosec: Duplicated errcheck checks
570+
# EXC0008 gosec: Duplicated errcheck checks
571571
- G104
572572
573-
# gosec: Too many issues in popular repos
573+
# EXC0009 gosec: Too many issues in popular repos
574574
- (Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)
575575
576-
# gosec: False positive is triggered by 'src, err := ioutil.ReadFile(filename)'
576+
# EXC0010 gosec: False positive is triggered by 'src, err := ioutil.ReadFile(filename)'
577577
- Potential file inclusion via variable
578578
(default true)
579579
--exclude-case-sensitive If set to true exclude and exclude rules regular expressions are case sensitive

pkg/commands/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func getDefaultIssueExcludeHelp() string {
3030
parts := []string{"Use or not use default excludes:"}
3131
for _, ep := range config.DefaultExcludePatterns {
3232
parts = append(parts,
33-
fmt.Sprintf(" # %s: %s", ep.Linter, ep.Why),
33+
fmt.Sprintf(" # %s %s: %s", ep.ID, ep.Linter, ep.Why),
3434
fmt.Sprintf(" - %s", color.YellowString(ep.Pattern)),
3535
"",
3636
)

pkg/config/config.go

+28-5
Original file line numberDiff line numberDiff line change
@@ -30,70 +30,92 @@ var OutFormats = []string{
3030
}
3131

3232
type ExcludePattern struct {
33+
ID string
3334
Pattern string
3435
Linter string
3536
Why string
3637
}
3738

3839
var DefaultExcludePatterns = []ExcludePattern{
3940
{
41+
ID: "EXC0001",
4042
Pattern: "Error return value of .((os\\.)?std(out|err)\\..*|.*Close" +
4143
"|.*Flush|os\\.Remove(All)?|.*printf?|os\\.(Un)?Setenv). is not checked",
4244
Linter: "errcheck",
4345
Why: "Almost all programs ignore errors on these functions and in most cases it's ok",
4446
},
4547
{
48+
ID: "EXC0002",
4649
Pattern: "(comment on exported (method|function|type|const)|" +
4750
"should have( a package)? comment|comment should be of the form)",
4851
Linter: "golint",
4952
Why: "Annoying issue about not having a comment. The rare codebase has such comments",
5053
},
5154
{
55+
ID: "EXC0003",
5256
Pattern: "func name will be used as test\\.Test.* by other packages, and that stutters; consider calling this",
5357
Linter: "golint",
5458
Why: "False positive when tests are defined in package 'test'",
5559
},
5660
{
61+
ID: "EXC0004",
5762
Pattern: "(possible misuse of unsafe.Pointer|should have signature)",
5863
Linter: "govet",
5964
Why: "Common false positives",
6065
},
6166
{
67+
ID: "EXC0005",
6268
Pattern: "ineffective break statement. Did you mean to break out of the outer loop",
6369
Linter: "staticcheck",
6470
Why: "Developers tend to write in C-style with an explicit 'break' in a 'switch', so it's ok to ignore",
6571
},
6672
{
73+
ID: "EXC0006",
6774
Pattern: "Use of unsafe calls should be audited",
6875
Linter: "gosec",
6976
Why: "Too many false-positives on 'unsafe' usage",
7077
},
7178
{
79+
ID: "EXC0007",
7280
Pattern: "Subprocess launch(ed with variable|ing should be audited)",
7381
Linter: "gosec",
7482
Why: "Too many false-positives for parametrized shell calls",
7583
},
7684
{
85+
ID: "EXC0008",
7786
Pattern: "G104",
7887
Linter: "gosec",
7988
Why: "Duplicated errcheck checks",
8089
},
8190
{
91+
ID: "EXC0009",
8292
Pattern: "(Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)",
8393
Linter: "gosec",
8494
Why: "Too many issues in popular repos",
8595
},
8696
{
97+
ID: "EXC0010",
8798
Pattern: "Potential file inclusion via variable",
8899
Linter: "gosec",
89100
Why: "False positive is triggered by 'src, err := ioutil.ReadFile(filename)'",
90101
},
91102
}
92103

93104
func GetDefaultExcludePatternsStrings() []string {
105+
return GetExcludePatternsStrings(nil)
106+
}
107+
108+
func GetExcludePatternsStrings(include []string) []string {
109+
includeMap := make(map[string]bool, len(include))
110+
for _, inc := range include {
111+
includeMap[inc] = true
112+
}
113+
94114
var ret []string
95115
for _, p := range DefaultExcludePatterns {
96-
ret = append(ret, p.Pattern)
116+
if !includeMap[p.ID] {
117+
ret = append(ret, p.Pattern)
118+
}
97119
}
98120

99121
return ret
@@ -411,10 +433,11 @@ func (e ExcludeRule) Validate() error {
411433
}
412434

413435
type Issues struct {
414-
ExcludeCaseSensitive bool `mapstructure:"exclude-case-sensitive"`
415-
ExcludePatterns []string `mapstructure:"exclude"`
416-
ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"`
417-
UseDefaultExcludes bool `mapstructure:"exclude-use-default"`
436+
IncludeDefaultExcludes []string `mapstructure:"include"`
437+
ExcludeCaseSensitive bool `mapstructure:"exclude-case-sensitive"`
438+
ExcludePatterns []string `mapstructure:"exclude"`
439+
ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"`
440+
UseDefaultExcludes bool `mapstructure:"exclude-use-default"`
418441

419442
MaxIssuesPerLinter int `mapstructure:"max-issues-per-linter"`
420443
MaxSameIssues int `mapstructure:"max-same-issues"`

pkg/lint/runner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env,
3232
icfg := cfg.Issues
3333
excludePatterns := icfg.ExcludePatterns
3434
if icfg.UseDefaultExcludes {
35-
excludePatterns = append(excludePatterns, config.GetDefaultExcludePatternsStrings()...)
35+
excludePatterns = append(excludePatterns, config.GetExcludePatternsStrings(icfg.IncludeDefaultExcludes)...)
3636
}
3737

3838
var excludeTotalPattern string

0 commit comments

Comments
 (0)