-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathissues.go
247 lines (213 loc) · 7.08 KB
/
issues.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package config
import (
"errors"
"fmt"
"regexp"
)
const excludeRuleMinConditionsCount = 2
var DefaultExcludePatterns = []ExcludePattern{
{
ID: "EXC0001",
Pattern: "Error return value of .((os\\.)?std(out|err)\\..*|.*Close" +
"|.*Flush|os\\.Remove(All)?|.*print(f|ln)?|os\\.(Un)?Setenv). is not checked",
Linter: "errcheck",
Why: "Almost all programs ignore errors on these functions and in most cases it's ok.",
},
{
ID: "EXC0002", // TODO(ldez): should be remove in v2
Pattern: "(comment on exported (method|function|type|const)|" +
"should have( a package)? comment|comment should be of the form)",
Linter: "golint",
Why: "Annoying issue about not having a comment. The rare codebase has such comments.",
},
{
ID: "EXC0003", // TODO(ldez): should be remove in v2
Pattern: "func name will be used as test\\.Test.* by other packages, and that stutters; consider calling this",
Linter: "golint",
Why: "False positive when tests are defined in package 'test'.",
},
{
ID: "EXC0004",
Pattern: "(possible misuse of unsafe.Pointer|should have signature)",
Linter: "govet",
Why: "Common false positives.",
},
{
ID: "EXC0005",
Pattern: "SA4011", // CheckScopedBreak
Linter: "staticcheck",
Why: "Developers tend to write in C-style with an explicit 'break' in a 'switch', so it's ok to ignore.",
},
{
ID: "EXC0006",
Pattern: "G103: Use of unsafe calls should be audited",
Linter: "gosec",
Why: "Too many false-positives on 'unsafe' usage.",
},
{
ID: "EXC0007",
Pattern: "G204: Subprocess launched with variable",
Linter: "gosec",
Why: "Too many false-positives for parametrized shell calls.",
},
{
ID: "EXC0008",
Pattern: "G104", // Errors unhandled.
Linter: "gosec",
Why: "Duplicated errcheck checks.",
},
{
ID: "EXC0009",
Pattern: "(G301|G302|G307): Expect (directory permissions to be 0750|file permissions to be 0600) or less",
Linter: "gosec",
Why: "Too many issues in popular repos.",
},
{
ID: "EXC0010",
Pattern: "G304: Potential file inclusion via variable",
Linter: "gosec",
Why: "False positive is triggered by 'src, err := ioutil.ReadFile(filename)'.",
},
{
ID: "EXC0011",
Pattern: "(ST1000|ST1020|ST1021|ST1022)", // CheckPackageComment, CheckExportedFunctionDocs, CheckExportedTypeDocs, CheckExportedVarDocs
Linter: "stylecheck",
Why: "Annoying issue about not having a comment. The rare codebase has such comments.",
},
{
ID: "EXC0012",
Pattern: `exported (.+) should have comment( \(or a comment on this block\))? or be unexported`, // rule: exported
Linter: "revive",
Why: "Annoying issue about not having a comment. The rare codebase has such comments.",
},
{
ID: "EXC0013",
Pattern: `package comment should be of the form "(.+)..."`, // rule: package-comments
Linter: "revive",
Why: "Annoying issue about not having a comment. The rare codebase has such comments.",
},
{
ID: "EXC0014",
Pattern: `comment on exported (.+) should be of the form "(.+)..."`, // rule: exported
Linter: "revive",
Why: "Annoying issue about not having a comment. The rare codebase has such comments.",
},
{
ID: "EXC0015",
Pattern: `should have a package comment`, // rule: package-comments
Linter: "revive",
Why: "Annoying issue about not having a comment. The rare codebase has such comments.",
},
}
type Issues struct {
IncludeDefaultExcludes []string `mapstructure:"include"`
ExcludeCaseSensitive bool `mapstructure:"exclude-case-sensitive"`
ExcludePatterns []string `mapstructure:"exclude"`
ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"`
UseDefaultExcludes bool `mapstructure:"exclude-use-default"`
ExcludeGenerated string `mapstructure:"exclude-generated"`
ExcludeFiles []string `mapstructure:"exclude-files"`
ExcludeDirs []string `mapstructure:"exclude-dirs"`
UseDefaultExcludeDirs bool `mapstructure:"exclude-dirs-use-default"`
MaxIssuesPerLinter int `mapstructure:"max-issues-per-linter"`
MaxSameIssues int `mapstructure:"max-same-issues"`
UniqByLine bool `mapstructure:"uniq-by-line"`
DiffFromRevision string `mapstructure:"new-from-rev"`
DiffPatchFilePath string `mapstructure:"new-from-patch"`
WholeFiles bool `mapstructure:"whole-files"`
Diff bool `mapstructure:"new"`
NeedFix bool `mapstructure:"fix"`
ExcludeGeneratedStrict *bool `mapstructure:"exclude-generated-strict"` // Deprecated: use ExcludeGenerated instead.
}
func (i *Issues) Validate() error {
for i, rule := range i.ExcludeRules {
if err := rule.Validate(); err != nil {
return fmt.Errorf("error in exclude rule #%d: %w", i, err)
}
}
return nil
}
type ExcludeRule struct {
BaseRule `mapstructure:",squash"`
}
func (e *ExcludeRule) Validate() error {
return e.BaseRule.Validate(excludeRuleMinConditionsCount)
}
type BaseRule struct {
Linters []string
Path string
PathExcept string `mapstructure:"path-except"`
Text string
Source string
}
func (b *BaseRule) Validate(minConditionsCount int) error {
if err := validateOptionalRegex(b.Path); err != nil {
return fmt.Errorf("invalid path regex: %w", err)
}
if err := validateOptionalRegex(b.PathExcept); err != nil {
return fmt.Errorf("invalid path-except regex: %w", err)
}
if err := validateOptionalRegex(b.Text); err != nil {
return fmt.Errorf("invalid text regex: %w", err)
}
if err := validateOptionalRegex(b.Source); err != nil {
return fmt.Errorf("invalid source regex: %w", err)
}
if b.Path != "" && b.PathExcept != "" {
return errors.New("path and path-except should not be set at the same time")
}
nonBlank := 0
if len(b.Linters) > 0 {
nonBlank++
}
// Filtering by path counts as one condition, regardless how it is done (one or both).
// Otherwise, a rule with Path and PathExcept set would pass validation
// whereas before the introduction of path-except that wouldn't have been precise enough.
if b.Path != "" || b.PathExcept != "" {
nonBlank++
}
if b.Text != "" {
nonBlank++
}
if b.Source != "" {
nonBlank++
}
if nonBlank < minConditionsCount {
return fmt.Errorf("at least %d of (text, source, path[-except], linters) should be set", minConditionsCount)
}
return nil
}
func validateOptionalRegex(value string) error {
if value == "" {
return nil
}
_, err := regexp.Compile(value)
return err
}
type ExcludePattern struct {
ID string
Pattern string
Linter string
Why string
}
func GetDefaultExcludePatternsStrings() []string {
ret := make([]string, len(DefaultExcludePatterns))
for i, p := range DefaultExcludePatterns {
ret[i] = p.Pattern
}
return ret
}
// TODO(ldez): this behavior must be changed in v2, because this is confusing.
func GetExcludePatterns(include []string) []ExcludePattern {
includeMap := make(map[string]struct{}, len(include))
for _, inc := range include {
includeMap[inc] = struct{}{}
}
var ret []ExcludePattern
for _, p := range DefaultExcludePatterns {
if _, ok := includeMap[p.ID]; !ok {
ret = append(ret, p)
}
}
return ret
}