Skip to content

Commit 9948153

Browse files
DefaultExcludePatterns should only be used for specified linter (#1494)
Co-authored-by: zhangyunhao <[email protected]>
1 parent df2e9e2 commit 9948153

File tree

7 files changed

+90
-13
lines changed

7 files changed

+90
-13
lines changed

pkg/config/config.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,33 @@ var DefaultExcludePatterns = []ExcludePattern{
9999
Linter: "gosec",
100100
Why: "False positive is triggered by 'src, err := ioutil.ReadFile(filename)'",
101101
},
102+
{
103+
ID: "EXC0011",
104+
Pattern: "(comment on exported (method|function|type|const)|" +
105+
"should have( a package)? comment|comment should be of the form)",
106+
Linter: "stylecheck",
107+
Why: "Annoying issue about not having a comment. The rare codebase has such comments",
108+
},
102109
}
103110

104111
func GetDefaultExcludePatternsStrings() []string {
105-
return GetExcludePatternsStrings(nil)
112+
ret := make([]string, len(DefaultExcludePatterns))
113+
for i, p := range DefaultExcludePatterns {
114+
ret[i] = p.Pattern
115+
}
116+
return ret
106117
}
107118

108-
func GetExcludePatternsStrings(include []string) []string {
119+
func GetExcludePatterns(include []string) []ExcludePattern {
109120
includeMap := make(map[string]bool, len(include))
110121
for _, inc := range include {
111122
includeMap[inc] = true
112123
}
113124

114-
var ret []string
125+
var ret []ExcludePattern
115126
for _, p := range DefaultExcludePatterns {
116127
if !includeMap[p.ID] {
117-
ret = append(ret, p.Pattern)
128+
ret = append(ret, p)
118129
}
119130
}
120131

pkg/config/config_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestGetExcludePatterns(t *testing.T) {
11+
assert.Equal(t, GetExcludePatterns(nil), DefaultExcludePatterns)
12+
13+
include := make([]string, 2)
14+
include[0], include[1] = DefaultExcludePatterns[0].ID, DefaultExcludePatterns[1].ID
15+
16+
exclude := GetExcludePatterns(include)
17+
assert.Equal(t, len(exclude), len(DefaultExcludePatterns)-len(include))
18+
19+
for _, p := range exclude {
20+
// Not in include.
21+
for _, i := range include {
22+
if i == p.ID {
23+
t.Fatalf("%s can't appear inside include.", p.ID)
24+
}
25+
}
26+
// Must in DefaultExcludePatterns.
27+
var inDefaultExc bool
28+
for _, i := range DefaultExcludePatterns {
29+
if i == p {
30+
inDefaultExc = true
31+
break
32+
}
33+
}
34+
assert.True(t, inDefaultExc, fmt.Sprintf("%s must appear inside DefaultExcludePatterns.", p.ID))
35+
}
36+
}

pkg/golinters/goanalysis/runner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Use of this source code is governed by a BSD-style
44
// license that can be found in the LICENSE file.
55

6-
// Package checker defines the implementation of the checker commands.
6+
// Package goanalysis defines the implementation of the checker commands.
77
// The same code drives the multi-analysis driver, the single-analysis
88
// driver that is conventionally provided for convenience along with
99
// each analysis package, and the test driver.

pkg/lint/runner.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,10 @@ func (r *Runner) processIssues(issues []result.Issue, sw *timeutils.Stopwatch, s
225225
}
226226

227227
func getExcludeProcessor(cfg *config.Issues) processors.Processor {
228-
excludePatterns := cfg.ExcludePatterns
229-
if cfg.UseDefaultExcludes {
230-
excludePatterns = append(excludePatterns, config.GetExcludePatternsStrings(cfg.IncludeDefaultExcludes)...)
231-
}
232-
233228
var excludeTotalPattern string
234-
if len(excludePatterns) != 0 {
235-
excludeTotalPattern = fmt.Sprintf("(%s)", strings.Join(excludePatterns, "|"))
229+
excludeGlobalPatterns := cfg.ExcludePatterns
230+
if len(excludeGlobalPatterns) != 0 {
231+
excludeTotalPattern = fmt.Sprintf("(%s)", strings.Join(excludeGlobalPatterns, "|"))
236232
}
237233

238234
var excludeProcessor processors.Processor
@@ -258,6 +254,17 @@ func getExcludeRulesProcessor(cfg *config.Issues, log logutils.Log, lineCache *f
258254
})
259255
}
260256

257+
if cfg.UseDefaultExcludes {
258+
for _, r := range config.GetExcludePatterns(cfg.IncludeDefaultExcludes) {
259+
excludeRules = append(excludeRules, processors.ExcludeRule{
260+
BaseRule: processors.BaseRule{
261+
Text: r.Pattern,
262+
Linters: []string{r.Linter},
263+
},
264+
})
265+
}
266+
}
267+
261268
var excludeRulesProcessor processors.Processor
262269
if cfg.ExcludeCaseSensitive {
263270
excludeRulesProcessor = processors.NewExcludeRulesCaseSensitive(

pkg/printers/github.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type github struct {
1313

1414
const defaultGithubSeverity = "error"
1515

16-
// Github output format outputs issues according to Github actions format:
16+
// NewGithub output format outputs issues according to Github actions format:
1717
// https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
1818
func NewGithub() Printer {
1919
return &github{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
issues:
2+
include:
3+
- EXC0011 # include issues about comments from `stylecheck`

test/testdata/default_exclude.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//args: -Estylecheck,golint
2+
//config_path: testdata/configs/default_exclude.yml
3+
4+
/*Package testdata ...*/
5+
package testdata
6+
7+
// InvalidFuncComment, both golint and stylecheck will complain about this, // ERROR `ST1020: comment on exported function ExportedFunc1 should be of the form "ExportedFunc1 ..."`
8+
// if include EXC0011, only the warning from golint will be ignored.
9+
// And only the warning from stylecheck will start with "ST1020".
10+
func ExportedFunc1() {
11+
}
12+
13+
// InvalidFuncComment // ERROR `ST1020: comment on exported function ExportedFunc2 should be of the form "ExportedFunc2 ..."`
14+
// nolint:golint
15+
func ExportedFunc2() {
16+
}
17+
18+
// nolint:stylecheck
19+
func IgnoreAll() {
20+
}

0 commit comments

Comments
 (0)