Skip to content

Commit 947dae1

Browse files
authored
Unknown linter breaks //nolint (#1497)
* Unknown linter breaks //nolint * Testing if nolint directive for unknown linter silences violation on the same line
1 parent b1755c1 commit 947dae1

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

pkg/result/processors/nolint.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,14 @@ func (p *Nolint) extractInlineRangeFromComment(text string, g ast.Node, fset *to
250250
var linters []string
251251
text = strings.Split(text, "//")[0] // allow another comment after this comment
252252
linterItems := strings.Split(strings.TrimPrefix(text, "nolint:"), ",")
253-
var gotUnknownLinters bool
254253
for _, linter := range linterItems {
255254
linterName := strings.ToLower(strings.TrimSpace(linter))
256255

257256
lcs := p.dbManager.GetLinterConfigs(linterName)
258257
if lcs == nil {
259258
p.unknownLintersSet[linterName] = true
260-
gotUnknownLinters = true
259+
linters = append(linters, linterName)
260+
nolintDebugf("unknown linter %s on line %d", linterName, fset.Position(g.Pos()).Line)
261261
continue
262262
}
263263

@@ -266,10 +266,6 @@ func (p *Nolint) extractInlineRangeFromComment(text string, g ast.Node, fset *to
266266
}
267267
}
268268

269-
if gotUnknownLinters {
270-
return buildRange(nil) // ignore all linters to not annoy user
271-
}
272-
273269
nolintDebugf("%d: linters are %s", fset.Position(g.Pos()).Line, linters)
274270
return buildRange(linters)
275271
}

pkg/result/processors/nolint_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,28 @@ func TestNolintInvalidLinterName(t *testing.T) {
149149
p.Finish()
150150
}
151151

152+
func TestNolintInvalidLinterNameWithViolationOnTheSameLine(t *testing.T) {
153+
log := getMockLog()
154+
log.On("Warnf", "Found unknown linters in //nolint directives: %s", "foobar")
155+
issues := []result.Issue{
156+
{
157+
Pos: token.Position{
158+
Filename: filepath.Join("testdata", "nolint_apply_to_unknown.go"),
159+
Line: 4,
160+
},
161+
FromLinter: "gofmt",
162+
},
163+
}
164+
165+
p := newTestNolintProcessor(log)
166+
processedIssues, err := p.Process(issues)
167+
p.Finish()
168+
169+
assert.Len(t, processedIssues, 1)
170+
assert.Equal(t, issues, processedIssues)
171+
assert.NoError(t, err)
172+
}
173+
152174
func TestNolintAliases(t *testing.T) {
153175
p := newTestNolintProcessor(getMockLog())
154176
for _, line := range []int{47, 49, 51} {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package testdata
2+
3+
func bar() {
4+
_ = 0 //nolint: foobar
5+
}

0 commit comments

Comments
 (0)