From 468d2334eac195e045c1df2b9aa5dd856f540d50 Mon Sep 17 00:00:00 2001 From: Stephan Renatus Date: Thu, 31 May 2018 09:11:51 +0200 Subject: [PATCH] change isGenerated heuristic to match more generated files As observed in #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 --- pkg/result/processors/nolint.go | 19 ++++++++------ pkg/result/processors/nolint_test.go | 25 +++++++++++++------ .../testdata/nolint_autogenerated_alt_hdr.go | 6 +++++ .../testdata/nolint_autogenerated_alt_hdr2.go | 8 ++++++ 4 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 pkg/result/processors/testdata/nolint_autogenerated_alt_hdr.go create mode 100644 pkg/result/processors/testdata/nolint_autogenerated_alt_hdr2.go diff --git a/pkg/result/processors/nolint.go b/pkg/result/processors/nolint.go index 2614d2e2..5a249bb2 100644 --- a/pkg/result/processors/nolint.go +++ b/pkg/result/processors/nolint.go @@ -47,21 +47,26 @@ func (p *Nolint) Process(issues []result.Issue) ([]result.Issue, error) { } var ( - genHdr = []byte("// Code generated ") - genFtr = []byte(" DO NOT EDIT.") + genHdr = []byte("// Code generated") + genFtr = []byte("DO NOT EDIT") ) -// isGenerated reports whether the source file is generated code -// according the rules from https://golang.org/s/generatedcode. +// isGenerated reports whether the source file is generated code. +// Using a bit laxer rules than https://golang.org/s/generatedcode to +// match more generated code. func isGenerated(src []byte) bool { sc := bufio.NewScanner(bytes.NewReader(src)) + var hdr, ftr bool for sc.Scan() { b := sc.Bytes() - if bytes.HasPrefix(b, genHdr) && bytes.HasSuffix(b, genFtr) && len(b) >= len(genHdr)+len(genFtr) { - return true + if bytes.HasPrefix(b, genHdr) { + hdr = true + } + if bytes.Contains(b, genFtr) { + ftr = true } } - return false + return hdr && ftr } func (p *Nolint) getOrCreateFileData(i *result.Issue) (*fileData, error) { diff --git a/pkg/result/processors/nolint_test.go b/pkg/result/processors/nolint_test.go index 6a1b2288..22091db9 100644 --- a/pkg/result/processors/nolint_test.go +++ b/pkg/result/processors/nolint_test.go @@ -37,13 +37,22 @@ func TestNolint(t *testing.T) { processAssertSame(t, p, newNolintFileIssue(1, "golint")) // no directive } -func TestNoIssuesInAutogeneratedFile(t *testing.T) { - i := result.Issue{ - Pos: token.Position{ - Filename: filepath.Join("testdata", "nolint_autogenerated.go"), - Line: 4, - }, +func TestNoIssuesInAutogeneratedFiles(t *testing.T) { + files := []string{ + "nolint_autogenerated.go", + "nolint_autogenerated_alt_hdr.go", + "nolint_autogenerated_alt_hdr2.go", + } + for _, file := range files { + t.Run(file, func(t *testing.T) { + i := result.Issue{ + Pos: token.Position{ + Filename: filepath.Join("testdata", file), + Line: 4, + }, + } + p := NewNolint(token.NewFileSet()) + processAssertEmpty(t, p, i) + }) } - p := NewNolint(token.NewFileSet()) - processAssertEmpty(t, p, i) } diff --git a/pkg/result/processors/testdata/nolint_autogenerated_alt_hdr.go b/pkg/result/processors/testdata/nolint_autogenerated_alt_hdr.go new file mode 100644 index 00000000..a88b8049 --- /dev/null +++ b/pkg/result/processors/testdata/nolint_autogenerated_alt_hdr.go @@ -0,0 +1,6 @@ +// Code generated by protoc-gen-foo +// source: bar.proto +// DO NOT EDIT!!! +package testdata + +var v int diff --git a/pkg/result/processors/testdata/nolint_autogenerated_alt_hdr2.go b/pkg/result/processors/testdata/nolint_autogenerated_alt_hdr2.go new file mode 100644 index 00000000..7b973d11 --- /dev/null +++ b/pkg/result/processors/testdata/nolint_autogenerated_alt_hdr2.go @@ -0,0 +1,8 @@ +// Code generated by go-bindata. +// sources: +// bar.baz +// x/y.z +// DO NOT EDIT! +package testdata + +var v int