Unknown linter breaks //nolint (#1497)
* Unknown linter breaks //nolint * Testing if nolint directive for unknown linter silences violation on the same line
This commit is contained in:
parent
b1755c16ea
commit
947dae101e
pkg/result/processors
@ -250,14 +250,14 @@ func (p *Nolint) extractInlineRangeFromComment(text string, g ast.Node, fset *to
|
|||||||
var linters []string
|
var linters []string
|
||||||
text = strings.Split(text, "//")[0] // allow another comment after this comment
|
text = strings.Split(text, "//")[0] // allow another comment after this comment
|
||||||
linterItems := strings.Split(strings.TrimPrefix(text, "nolint:"), ",")
|
linterItems := strings.Split(strings.TrimPrefix(text, "nolint:"), ",")
|
||||||
var gotUnknownLinters bool
|
|
||||||
for _, linter := range linterItems {
|
for _, linter := range linterItems {
|
||||||
linterName := strings.ToLower(strings.TrimSpace(linter))
|
linterName := strings.ToLower(strings.TrimSpace(linter))
|
||||||
|
|
||||||
lcs := p.dbManager.GetLinterConfigs(linterName)
|
lcs := p.dbManager.GetLinterConfigs(linterName)
|
||||||
if lcs == nil {
|
if lcs == nil {
|
||||||
p.unknownLintersSet[linterName] = true
|
p.unknownLintersSet[linterName] = true
|
||||||
gotUnknownLinters = true
|
linters = append(linters, linterName)
|
||||||
|
nolintDebugf("unknown linter %s on line %d", linterName, fset.Position(g.Pos()).Line)
|
||||||
continue
|
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)
|
nolintDebugf("%d: linters are %s", fset.Position(g.Pos()).Line, linters)
|
||||||
return buildRange(linters)
|
return buildRange(linters)
|
||||||
}
|
}
|
||||||
|
@ -149,6 +149,28 @@ func TestNolintInvalidLinterName(t *testing.T) {
|
|||||||
p.Finish()
|
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) {
|
func TestNolintAliases(t *testing.T) {
|
||||||
p := newTestNolintProcessor(getMockLog())
|
p := newTestNolintProcessor(getMockLog())
|
||||||
for _, line := range []int{47, 49, 51} {
|
for _, line := range []int{47, 49, 51} {
|
||||||
|
5
pkg/result/processors/testdata/nolint_apply_to_unknown.go
vendored
Normal file
5
pkg/result/processors/testdata/nolint_apply_to_unknown.go
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package testdata
|
||||||
|
|
||||||
|
func bar() {
|
||||||
|
_ = 0 //nolint: foobar
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user