Skip to content

Commit

Permalink
Fix lint errors on files with //line directive
Browse files Browse the repository at this point in the history
If the target files contains `//line` directive and it indicates
a non-go file, the linter is going to handle it as a go file,
which results in failure.
The cause of this issue is that the linters (`Analyzer`s) are using
`pass.Fset.Position()`. This func returns the adjusted position using
`//line` directive.
The example project reported in golangci#998 has `//line` directive that
indicates other non-go file.
According to the description of "Compiler Directives”
(https://golang.org/cmd/compile/#hdr-Compiler_Directives),
line directives is mainly used for reporting original positions to
the generators or something.
On linters of golangci-lint, `pass.Fset.Position()` is used just to
aggregate file names; we don't have to adjust positions.
This changes `Analyzer`s that use `pass.Fset.Position()` to aggregate
file names to use `pass.Fset.PositionFor()` with `adjusted == false`.

Relates: golangci#998
  • Loading branch information
ksoichiro committed May 3, 2020
1 parent 02a4077 commit af34430
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/golinters/dupl.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewDupl() *goanalysis.Linter {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.Position(f.Pos())
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewGofmt() *goanalysis.Linter {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.Position(f.Pos())
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/goimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewGoimports() *goanalysis.Linter {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.Position(f.Pos())
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gomodguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewGomodguard() *goanalysis.Linter {
}

for _, file := range pass.Files {
files = append(files, pass.Fset.Position(file.Pos()).Filename)
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
}

processor, err := gomodguard.NewProcessor(processorCfg, log.New(os.Stderr, "", 0))
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/ineffassign.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewIneffassign() *goanalysis.Linter {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.Position(f.Pos())
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/lll.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func NewLLL() *goanalysis.Linter {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.Position(f.Pos())
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/misspell.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func NewMisspell() *goanalysis.Linter {

var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.Position(f.Pos())
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewWSL() *goanalysis.Linter {
)

for _, file := range pass.Files {
files = append(files, pass.Fset.Position(file.Pos()).Filename)
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
}

wslErrors, _ := wsl.NewProcessorWithConfig(processorCfg).
Expand Down

0 comments on commit af34430

Please sign in to comment.