diff --git a/errcheck/analyzer.go b/errcheck/analyzer.go index 8b444c7..68593cc 100644 --- a/errcheck/analyzer.go +++ b/errcheck/analyzer.go @@ -3,7 +3,6 @@ package errcheck import ( "fmt" "go/ast" - "go/token" "reflect" "regexp" @@ -66,7 +65,7 @@ func runAnalyzer(pass *analysis.Pass) (interface{}, error) { for _, err := range v.errors { pass.Report(analysis.Diagnostic{ - Pos: token.Pos(int(f.Pos()) + err.Pos.Offset), + Pos: pass.Fset.File(f.Pos()).Pos(err.Pos.Offset), Message: "unchecked error", }) } diff --git a/errcheck/analyzer_test.go b/errcheck/analyzer_test.go new file mode 100644 index 0000000..7a55be0 --- /dev/null +++ b/errcheck/analyzer_test.go @@ -0,0 +1,12 @@ +package errcheck + +import ( + "golang.org/x/tools/go/analysis/analysistest" + "path/filepath" + "testing" +) + +func TestAnalyzer(t *testing.T) { + packageDir := filepath.Join(analysistest.TestData(), "src/a/") + _ = analysistest.Run(t, packageDir, Analyzer) +} diff --git a/errcheck/testdata/src/a/main.go b/errcheck/testdata/src/a/main.go index 50034d9..a531c55 100644 --- a/errcheck/testdata/src/a/main.go +++ b/errcheck/testdata/src/a/main.go @@ -1,3 +1,5 @@ +// ensure that the package keyword is not equal to file beginning +// to test correct position calculations. package a import (