Skip to content

Commit 3faf0bc

Browse files
authored
Merge pull request #215 from rmohr/comments-before-package
Calculate correct position with comments before the package keyword
2 parents 98d3f84 + 875db03 commit 3faf0bc

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

errcheck/analyzer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package errcheck
33
import (
44
"fmt"
55
"go/ast"
6-
"go/token"
76
"reflect"
87
"regexp"
98

@@ -66,7 +65,7 @@ func runAnalyzer(pass *analysis.Pass) (interface{}, error) {
6665

6766
for _, err := range v.errors {
6867
pass.Report(analysis.Diagnostic{
69-
Pos: token.Pos(int(f.Pos()) + err.Pos.Offset),
68+
Pos: pass.Fset.File(f.Pos()).Pos(err.Pos.Offset),
7069
Message: "unchecked error",
7170
})
7271
}

errcheck/analyzer_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package errcheck
2+
3+
import (
4+
"golang.org/x/tools/go/analysis/analysistest"
5+
"path/filepath"
6+
"testing"
7+
)
8+
9+
func TestAnalyzer(t *testing.T) {
10+
packageDir := filepath.Join(analysistest.TestData(), "src/a/")
11+
_ = analysistest.Run(t, packageDir, Analyzer)
12+
}

errcheck/testdata/src/a/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ensure that the package keyword is not equal to file beginning
2+
// to test correct position calculations.
13
package a
24

35
import (

0 commit comments

Comments
 (0)