Skip to content

Commit 468d233

Browse files
committed
change isGenerated heuristic to match more generated files
As observed in golangci#30, there's tools out there that don't comply 100% with the referenced golang convention. With this change, golangci-lint will skip some more of those generated files. Signed-off-by: Stephan Renatus <[email protected]>
1 parent 21ff49c commit 468d233

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

pkg/result/processors/nolint.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,26 @@ func (p *Nolint) Process(issues []result.Issue) ([]result.Issue, error) {
4747
}
4848

4949
var (
50-
genHdr = []byte("// Code generated ")
51-
genFtr = []byte(" DO NOT EDIT.")
50+
genHdr = []byte("// Code generated")
51+
genFtr = []byte("DO NOT EDIT")
5252
)
5353

54-
// isGenerated reports whether the source file is generated code
55-
// according the rules from https://golang.org/s/generatedcode.
54+
// isGenerated reports whether the source file is generated code.
55+
// Using a bit laxer rules than https://golang.org/s/generatedcode to
56+
// match more generated code.
5657
func isGenerated(src []byte) bool {
5758
sc := bufio.NewScanner(bytes.NewReader(src))
59+
var hdr, ftr bool
5860
for sc.Scan() {
5961
b := sc.Bytes()
60-
if bytes.HasPrefix(b, genHdr) && bytes.HasSuffix(b, genFtr) && len(b) >= len(genHdr)+len(genFtr) {
61-
return true
62+
if bytes.HasPrefix(b, genHdr) {
63+
hdr = true
64+
}
65+
if bytes.Contains(b, genFtr) {
66+
ftr = true
6267
}
6368
}
64-
return false
69+
return hdr && ftr
6570
}
6671

6772
func (p *Nolint) getOrCreateFileData(i *result.Issue) (*fileData, error) {

pkg/result/processors/nolint_test.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,22 @@ func TestNolint(t *testing.T) {
3737
processAssertSame(t, p, newNolintFileIssue(1, "golint")) // no directive
3838
}
3939

40-
func TestNoIssuesInAutogeneratedFile(t *testing.T) {
41-
i := result.Issue{
42-
Pos: token.Position{
43-
Filename: filepath.Join("testdata", "nolint_autogenerated.go"),
44-
Line: 4,
45-
},
40+
func TestNoIssuesInAutogeneratedFiles(t *testing.T) {
41+
files := []string{
42+
"nolint_autogenerated.go",
43+
"nolint_autogenerated_alt_hdr.go",
44+
"nolint_autogenerated_alt_hdr2.go",
45+
}
46+
for _, file := range files {
47+
t.Run(file, func(t *testing.T) {
48+
i := result.Issue{
49+
Pos: token.Position{
50+
Filename: filepath.Join("testdata", file),
51+
Line: 4,
52+
},
53+
}
54+
p := NewNolint(token.NewFileSet())
55+
processAssertEmpty(t, p, i)
56+
})
4657
}
47-
p := NewNolint(token.NewFileSet())
48-
processAssertEmpty(t, p, i)
4958
}

pkg/result/processors/testdata/nolint_autogenerated_alt_hdr.go

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/result/processors/testdata/nolint_autogenerated_alt_hdr2.go

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)