Skip to content

Commit 7f48cc8

Browse files
authored
Fix lint errors on files with //line directive (#1065)
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 #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: #998
1 parent 279b6d6 commit 7f48cc8

File tree

14 files changed

+78
-8
lines changed

14 files changed

+78
-8
lines changed

pkg/golinters/dupl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func NewDupl() *goanalysis.Linter {
3434
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
3535
var fileNames []string
3636
for _, f := range pass.Files {
37-
pos := pass.Fset.Position(f.Pos())
37+
pos := pass.Fset.PositionFor(f.Pos(), false)
3838
fileNames = append(fileNames, pos.Filename)
3939
}
4040

pkg/golinters/gofmt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func NewGofmt() *goanalysis.Linter {
3131
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
3232
var fileNames []string
3333
for _, f := range pass.Files {
34-
pos := pass.Fset.Position(f.Pos())
34+
pos := pass.Fset.PositionFor(f.Pos(), false)
3535
fileNames = append(fileNames, pos.Filename)
3636
}
3737

pkg/golinters/goimports.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func NewGoimports() *goanalysis.Linter {
3232
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
3333
var fileNames []string
3434
for _, f := range pass.Files {
35-
pos := pass.Fset.Position(f.Pos())
35+
pos := pass.Fset.PositionFor(f.Pos(), false)
3636
fileNames = append(fileNames, pos.Filename)
3737
}
3838

pkg/golinters/gomodguard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func NewGomodguard() *goanalysis.Linter {
5454
}
5555

5656
for _, file := range pass.Files {
57-
files = append(files, pass.Fset.Position(file.Pos()).Filename)
57+
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
5858
}
5959

6060
processor, err := gomodguard.NewProcessor(processorCfg, log.New(os.Stderr, "", 0))

pkg/golinters/ineffassign.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func NewIneffassign() *goanalysis.Linter {
3131
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
3232
var fileNames []string
3333
for _, f := range pass.Files {
34-
pos := pass.Fset.Position(f.Pos())
34+
pos := pass.Fset.PositionFor(f.Pos(), false)
3535
fileNames = append(fileNames, pos.Filename)
3636
}
3737

pkg/golinters/lll.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func NewLLL() *goanalysis.Linter {
9292
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
9393
var fileNames []string
9494
for _, f := range pass.Files {
95-
pos := pass.Fset.Position(f.Pos())
95+
pos := pass.Fset.PositionFor(f.Pos(), false)
9696
fileNames = append(fileNames, pos.Filename)
9797
}
9898

pkg/golinters/misspell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func NewMisspell() *goanalysis.Linter {
102102

103103
var fileNames []string
104104
for _, f := range pass.Files {
105-
pos := pass.Fset.Position(f.Pos())
105+
pos := pass.Fset.PositionFor(f.Pos(), false)
106106
fileNames = append(fileNames, pos.Filename)
107107
}
108108

pkg/golinters/wsl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func NewWSL() *goanalysis.Linter {
5252
)
5353

5454
for _, file := range pass.Files {
55-
files = append(files, pass.Fset.Position(file.Pos()).Filename)
55+
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
5656
}
5757

5858
wslErrors, _ := wsl.NewProcessorWithConfig(processorCfg).

test/run_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ func TestLineDirectiveProcessedFilesFullLoading(t *testing.T) {
137137
r.ExpectExitCode(exitcodes.IssuesFound).ExpectOutputEq(output + "\n")
138138
}
139139

140+
func TestLintFilesWithLineDirective(t *testing.T) {
141+
testshared.NewLintRunner(t).Run("-Edupl", "--disable-all", "--config=testdata/linedirective/dupl.yml", getTestDataDir("linedirective")).
142+
ExpectHasIssue("21-23 lines are duplicate of `testdata/linedirective/hello.go:25-27` (dupl)")
143+
testshared.NewLintRunner(t).Run("-Egofmt", "--disable-all", "--no-config", getTestDataDir("linedirective")).
144+
ExpectHasIssue("File is not `gofmt`-ed with `-s` (gofmt)")
145+
testshared.NewLintRunner(t).Run("-Egoimports", "--disable-all", "--no-config", getTestDataDir("linedirective")).
146+
ExpectHasIssue("File is not `goimports`-ed (goimports)")
147+
testshared.NewLintRunner(t).
148+
Run("-Egomodguard", "--disable-all", "--config=testdata/linedirective/gomodguard.yml", getTestDataDir("linedirective")).
149+
ExpectHasIssue("import of package `github.com/ryancurrah/gomodguard` is blocked because the module is not " +
150+
"in the allowed modules list. (gomodguard)")
151+
testshared.NewLintRunner(t).Run("-Eineffassign", "--disable-all", "--no-config", getTestDataDir("linedirective")).
152+
ExpectHasIssue("ineffectual assignment to `x` (ineffassign)")
153+
testshared.NewLintRunner(t).Run("-Elll", "--disable-all", "--config=testdata/linedirective/lll.yml", getTestDataDir("linedirective")).
154+
ExpectHasIssue("line is 57 characters (lll)")
155+
testshared.NewLintRunner(t).Run("-Emisspell", "--disable-all", "--no-config", getTestDataDir("linedirective")).
156+
ExpectHasIssue("is a misspelling of `language` (misspell)")
157+
testshared.NewLintRunner(t).Run("-Ewsl", "--disable-all", "--no-config", getTestDataDir("linedirective")).
158+
ExpectHasIssue("block should not start with a whitespace (wsl)")
159+
}
160+
140161
func TestSkippedDirsNoMatchArg(t *testing.T) {
141162
dir := getTestDataDir("skipdirs", "skip_me", "nested")
142163
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config", "--skip-dirs", dir, "-Egolint", dir)

test/testdata/linedirective/dupl.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
dupl:
3+
threshold: 10

0 commit comments

Comments
 (0)