Fix: goheader linter can throw nil pointer exception in case of a source file has not issues (#1209)

* fix potential nil pointer exception

Signed-off-by: denis-tingajkin <denis.tingajkin@xored.com>

* add test to cover

Signed-off-by: denis-tingajkin <denis.tingajkin@xored.com>
This commit is contained in:
Denis Tingaikin 2020-07-05 15:32:00 +07:00 committed by GitHub
parent afa9be632b
commit dfa0013583
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 0 deletions

View File

@ -51,6 +51,9 @@ func NewGoHeader() *goanalysis.Linter {
var res []goanalysis.Issue var res []goanalysis.Issue
for _, file := range pass.Files { for _, file := range pass.Files {
i := a.Analyze(file) i := a.Analyze(file)
if i == nil {
continue
}
issue := result.Issue{ issue := result.Issue{
Pos: token.Position{ Pos: token.Position{
Line: i.Location().Line + 1, Line: i.Location().Line + 1,

View File

@ -193,6 +193,9 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
skipMultilineComment(scanner) skipMultilineComment(scanner)
continue continue
} }
if strings.TrimSpace(line) == "" {
continue
}
if !strings.HasPrefix(line, "//") { if !strings.HasPrefix(line, "//") {
return rc return rc
} }

View File

@ -1,4 +1,5 @@
/*MY TITLE!*/ // ERROR "Expected:TITLE., Actual: TITLE!" /*MY TITLE!*/ // ERROR "Expected:TITLE., Actual: TITLE!"
//args: -Egoheader //args: -Egoheader
//config_path: testdata/configs/go-header.yml //config_path: testdata/configs/go-header.yml
package testdata package testdata

5
test/testdata/go-header_good.go vendored Normal file
View File

@ -0,0 +1,5 @@
/*MY TITLE.*/
//args: -Egoheader
//config_path: testdata/configs/go-header.yml
package testdata