From 105a35ce110e53a5e8d9a18dd368a8e284a4f42a Mon Sep 17 00:00:00 2001 From: "Maciej \"Iwan\" Iwanowski" Date: Fri, 6 Nov 2020 20:57:24 +0100 Subject: [PATCH 1/2] Unknown linter breaks //nolint --- pkg/result/processors/nolint.go | 8 ++------ test/testdata/nolint_apply_to_unknown.go | 6 ++++++ 2 files changed, 8 insertions(+), 6 deletions(-) create mode 100644 test/testdata/nolint_apply_to_unknown.go diff --git a/pkg/result/processors/nolint.go b/pkg/result/processors/nolint.go index cda1017849d9..9b292eda32ff 100644 --- a/pkg/result/processors/nolint.go +++ b/pkg/result/processors/nolint.go @@ -250,14 +250,14 @@ func (p *Nolint) extractInlineRangeFromComment(text string, g ast.Node, fset *to var linters []string text = strings.Split(text, "//")[0] // allow another comment after this comment linterItems := strings.Split(strings.TrimPrefix(text, "nolint:"), ",") - var gotUnknownLinters bool for _, linter := range linterItems { linterName := strings.ToLower(strings.TrimSpace(linter)) lcs := p.dbManager.GetLinterConfigs(linterName) if lcs == nil { p.unknownLintersSet[linterName] = true - gotUnknownLinters = true + linters = append(linters, linterName) + nolintDebugf("unknown linter %s on line %d", linterName, fset.Position(g.Pos()).Line) continue } @@ -266,10 +266,6 @@ func (p *Nolint) extractInlineRangeFromComment(text string, g ast.Node, fset *to } } - if gotUnknownLinters { - return buildRange(nil) // ignore all linters to not annoy user - } - nolintDebugf("%d: linters are %s", fset.Position(g.Pos()).Line, linters) return buildRange(linters) } diff --git a/test/testdata/nolint_apply_to_unknown.go b/test/testdata/nolint_apply_to_unknown.go new file mode 100644 index 000000000000..ba70e2acb81a --- /dev/null +++ b/test/testdata/nolint_apply_to_unknown.go @@ -0,0 +1,6 @@ +//args: -Egofmt +package testdata + +func bar() { + _ = 0 //nolint: foobar // ERROR "File is not `gofmt`-ed with `-s`" +} From a92b22c0456eb7b432652fa10a641e659c99ff6e Mon Sep 17 00:00:00 2001 From: "Maciej \"Iwan\" Iwanowski" Date: Sat, 7 Nov 2020 20:37:49 +0100 Subject: [PATCH 2/2] Testing if nolint directive for unknown linter silences violation on the same line --- pkg/result/processors/nolint_test.go | 22 +++++++++++++++++++ .../testdata/nolint_apply_to_unknown.go | 5 +++++ test/testdata/nolint_apply_to_unknown.go | 6 ----- 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 pkg/result/processors/testdata/nolint_apply_to_unknown.go delete mode 100644 test/testdata/nolint_apply_to_unknown.go diff --git a/pkg/result/processors/nolint_test.go b/pkg/result/processors/nolint_test.go index b400ad2be0b5..294add383b06 100644 --- a/pkg/result/processors/nolint_test.go +++ b/pkg/result/processors/nolint_test.go @@ -149,6 +149,28 @@ func TestNolintInvalidLinterName(t *testing.T) { p.Finish() } +func TestNolintInvalidLinterNameWithViolationOnTheSameLine(t *testing.T) { + log := getMockLog() + log.On("Warnf", "Found unknown linters in //nolint directives: %s", "foobar") + issues := []result.Issue{ + { + Pos: token.Position{ + Filename: filepath.Join("testdata", "nolint_apply_to_unknown.go"), + Line: 4, + }, + FromLinter: "gofmt", + }, + } + + p := newTestNolintProcessor(log) + processedIssues, err := p.Process(issues) + p.Finish() + + assert.Len(t, processedIssues, 1) + assert.Equal(t, issues, processedIssues) + assert.NoError(t, err) +} + func TestNolintAliases(t *testing.T) { p := newTestNolintProcessor(getMockLog()) for _, line := range []int{47, 49, 51} { diff --git a/pkg/result/processors/testdata/nolint_apply_to_unknown.go b/pkg/result/processors/testdata/nolint_apply_to_unknown.go new file mode 100644 index 000000000000..ffd07ebb23a5 --- /dev/null +++ b/pkg/result/processors/testdata/nolint_apply_to_unknown.go @@ -0,0 +1,5 @@ +package testdata + +func bar() { + _ = 0 //nolint: foobar +} diff --git a/test/testdata/nolint_apply_to_unknown.go b/test/testdata/nolint_apply_to_unknown.go deleted file mode 100644 index ba70e2acb81a..000000000000 --- a/test/testdata/nolint_apply_to_unknown.go +++ /dev/null @@ -1,6 +0,0 @@ -//args: -Egofmt -package testdata - -func bar() { - _ = 0 //nolint: foobar // ERROR "File is not `gofmt`-ed with `-s`" -}