Skip to content

Commit f974716

Browse files
committed
refactor: new exclusion rules
1 parent 9803f18 commit f974716

File tree

10 files changed

+386
-182
lines changed

10 files changed

+386
-182
lines changed

pkg/config/issues.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ type BaseRule struct {
155155
PathExcept string `mapstructure:"path-except"`
156156
Text string
157157
Source string
158+
159+
// For compatibility with exclude-use-default/include.
160+
InternalReference string `mapstructure:"-"`
158161
}
159162

160163
func (b *BaseRule) Validate(minConditionsCount int) error {

pkg/lint/runner.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
9797
processors.NewIdentifierMarker(),
9898

9999
processors.NewExclude(&cfg.Issues),
100-
processors.NewExcludeRules(log.Child(logutils.DebugKeyExcludeRules), files, &cfg.Issues),
100+
processors.NewExclusionRules(log.Child(logutils.DebugKeyExclusionRules), files,
101+
&cfg.Linters.LinterExclusions, cfg.Issues.IncludeDefaultExcludes, cfg.Issues.ExcludeCaseSensitive),
101102

102103
processors.NewNolintFilter(log.Child(logutils.DebugKeyNolintFilter), dbManager, enabledLinters),
103104

pkg/logutils/logutils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const (
4242

4343
// Processors.
4444
const (
45-
DebugKeyExcludeRules = "exclude_rules"
45+
DebugKeyExclusionRules = "exclusion_rules"
4646
DebugKeyFilenameUnadjuster = "filename_unadjuster"
4747
DebugKeyGeneratedFileFilter = "generated_file_filter" // Debugs a filter excluding autogenerated source code.
4848
DebugKeyInvalidIssue = "invalid_issue"

pkg/result/processors/base_rule.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ type baseRule struct {
1616
path *regexp.Regexp
1717
pathExcept *regexp.Regexp
1818
linters []string
19+
20+
// For compatibility with exclude-use-default/include.
21+
internalReference string `mapstructure:"-"`
1922
}
2023

2124
func (r *baseRule) isEmpty() bool {

pkg/result/processors/exclude_rules.go

Lines changed: 0 additions & 112 deletions
This file was deleted.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package processors
2+
3+
import "github.com/golangci/golangci-lint/pkg/config"
4+
5+
var defaultLintersExclusions = map[string][]config.ExcludeRule{
6+
"comments": {
7+
{
8+
// Annoying issue about not having a comment. The rare codebase has such comments.
9+
// CheckPackageComment, CheckExportedFunctionDocs, CheckExportedTypeDocs, CheckExportedVarDocs
10+
BaseRule: config.BaseRule{
11+
Text: "(ST1000|ST1020|ST1021|ST1022)",
12+
Linters: []string{"stylecheck"},
13+
InternalReference: "EXC0011",
14+
},
15+
},
16+
{
17+
// Annoying issue about not having a comment. The rare codebase has such comments.
18+
// rule: exported
19+
BaseRule: config.BaseRule{
20+
Text: `exported (.+) should have comment( \(or a comment on this block\))? or be unexported`,
21+
Linters: []string{"revive"},
22+
InternalReference: "EXC0012",
23+
},
24+
},
25+
{
26+
// Annoying issue about not having a comment. The rare codebase has such comments.
27+
// rule: package-comments
28+
BaseRule: config.BaseRule{
29+
Text: `package comment should be of the form "(.+)..."`,
30+
Linters: []string{"revive"},
31+
InternalReference: "EXC0013",
32+
},
33+
},
34+
{
35+
// Annoying issue about not having a comment. The rare codebase has such comments.
36+
// rule: exported
37+
BaseRule: config.BaseRule{
38+
Text: `comment on exported (.+) should be of the form "(.+)..."`,
39+
Linters: []string{"revive"},
40+
InternalReference: "EXC0014",
41+
},
42+
},
43+
{
44+
// Annoying issue about not having a comment. The rare codebase has such comments.
45+
// rule: package-comments
46+
BaseRule: config.BaseRule{
47+
Text: `should have a package comment`,
48+
Linters: []string{"revive"},
49+
InternalReference: "EXC0015",
50+
},
51+
},
52+
},
53+
"stdErrorHandling": {
54+
{
55+
// Almost all programs ignore errors on these functions and in most cases it's ok.
56+
BaseRule: config.BaseRule{
57+
Text: "Error return value of .((os\\.)?std(out|err)\\..*|.*Close" +
58+
"|.*Flush|os\\.Remove(All)?|.*print(f|ln)?|os\\.(Un)?Setenv). is not checked",
59+
Linters: []string{"errcheck"},
60+
InternalReference: "EXC0001",
61+
},
62+
},
63+
},
64+
"commonFalsePositives": {
65+
{
66+
// Too many false-positives on 'unsafe' usage.
67+
BaseRule: config.BaseRule{
68+
Text: "G103: Use of unsafe calls should be audited",
69+
Linters: []string{"gosec"},
70+
InternalReference: "EXC0006",
71+
},
72+
},
73+
{
74+
// Too many false-positives for parametrized shell calls.
75+
BaseRule: config.BaseRule{
76+
Text: "G204: Subprocess launched with variable",
77+
Linters: []string{"gosec"},
78+
InternalReference: "EXC0007",
79+
},
80+
},
81+
{
82+
// False positive is triggered by 'src, err := ioutil.ReadFile(filename)'.
83+
BaseRule: config.BaseRule{
84+
Text: "G304: Potential file inclusion via variable",
85+
Linters: []string{"gosec"},
86+
InternalReference: "EXC0010",
87+
},
88+
},
89+
},
90+
"legacy": {
91+
{
92+
// Common false positives.
93+
BaseRule: config.BaseRule{
94+
Text: "(possible misuse of unsafe.Pointer|should have signature)",
95+
Linters: []string{"govet"},
96+
InternalReference: "EXC0004",
97+
},
98+
},
99+
{
100+
// Developers tend to write in C-style with an explicit 'break' in a 'switch', so it's ok to ignore.
101+
// CheckScopedBreak
102+
BaseRule: config.BaseRule{
103+
Text: "SA4011",
104+
Linters: []string{"staticcheck"},
105+
InternalReference: "EXC0005",
106+
},
107+
},
108+
{
109+
// Duplicated errcheck checks.
110+
// Errors unhandled.
111+
BaseRule: config.BaseRule{
112+
Text: "G104",
113+
Linters: []string{"gosec"},
114+
InternalReference: "EXC0008",
115+
},
116+
},
117+
{
118+
// Too many issues in popular repos.
119+
BaseRule: config.BaseRule{
120+
Text: "(G301|G302|G307): Expect (directory permissions to be 0750|file permissions to be 0600) or less",
121+
Linters: []string{"gosec"},
122+
InternalReference: "EXC0009",
123+
},
124+
},
125+
},
126+
}
127+
128+
func getDefaultLintersExclusions(names []string) []config.ExcludeRule {
129+
var rules []config.ExcludeRule
130+
131+
for _, name := range names {
132+
rules = append(rules, defaultLintersExclusions[name]...)
133+
}
134+
135+
return rules
136+
}

0 commit comments

Comments
 (0)