fix: filter files (#5272) · golangci/golangci-lint@afa0e27 (original) (raw)

13 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -12,6 +12,16 @@ func GetFilePosition(pass *analysis.Pass, f *ast.File) token.Position {
12 12 return GetFilePositionFor(pass.Fset, f.Pos())
13 13 }
14 14
15 +func GetGoFilePosition(pass *analysis.Pass, f *ast.File) (token.Position, bool) {
16 +position := GetFilePositionFor(pass.Fset, f.Pos())
17 +
18 +if filepath.Ext(position.Filename) == ".go" {
19 +return position, true
20 + }
21 +
22 +return position, false
23 +}
24 +
15 25 func GetFilePositionFor(fset *token.FileSet, p token.Pos) token.Position {
16 26 pos := fset.PositionFor(p, true)
17 27
Original file line number Diff line number Diff line change
@@ -34,7 +34,10 @@ func New(settings *config.GodoxSettings) *goanalysis.Linter {
34 34
35 35 func runGodox(pass *analysis.Pass, settings *config.GodoxSettings) {
36 36 for _, file := range pass.Files {
37 -position := goanalysis.GetFilePosition(pass, file)
37 +position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
38 +if !isGoFile {
39 +continue
40 + }
38 41
39 42 messages := godox.Run(file, pass.Fset, settings.Keywords...)
40 43 if len(messages) == 0 {
Original file line number Diff line number Diff line change
@@ -45,7 +45,10 @@ func runGofmt(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GoF
45 45 }
46 46
47 47 for _, file := range pass.Files {
48 -position := goanalysis.GetFilePosition(pass, file)
48 +position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
49 +if !isGoFile {
50 +continue
51 + }
49 52
50 53 diff, err := gofmtAPI.RunRewrite(position.Filename, settings.Simplify, rewriteRules)
51 54 if err != nil { // TODO: skip
Original file line number Diff line number Diff line change
@@ -61,7 +61,10 @@ func New(settings *config.GofumptSettings) *goanalysis.Linter {
61 61
62 62 func runGofumpt(lintCtx *linter.Context, pass *analysis.Pass, diff differ, options format.Options) error {
63 63 for _, file := range pass.Files {
64 -position := goanalysis.GetFilePosition(pass, file)
64 +position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
65 +if !isGoFile {
66 +continue
67 + }
65 68
66 69 input, err := os.ReadFile(position.Filename)
67 70 if err != nil {
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
1 +//golangcitest:args -Egofumpt
2 +//golangcitest:config_path testdata/gofumpt-fix.yml
3 +//golangcitest:expected_exitcode 0
4 +package p
5 +
6 +/*
7 + #include <stdio.h>
8 + #include <stdlib.h>
9 +
10 + void myprint(char* s) {
11 + printf("%d\n", s);
12 + }
13 +*/
14 +import "C"
15 +
16 +import "fmt"
17 +
18 +func GofmtNotExtra(bar string, baz string) {
19 +fmt.Print(bar, baz)
20 +}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
1 +//golangcitest:args -Egofumpt
2 +//golangcitest:config_path testdata/gofumpt-fix.yml
3 +//golangcitest:expected_exitcode 0
4 +package p
5 +
6 +/*
7 + #include <stdio.h>
8 + #include <stdlib.h>
9 +
10 + void myprint(char* s) {
11 + printf("%d\n", s);
12 + }
13 +*/
14 +import "C"
15 +
16 +import "fmt"
17 +
18 +func GofmtNotExtra(bar, baz string) {
19 +fmt.Print(bar, baz)
20 +}
Original file line number Diff line number Diff line change
@@ -63,9 +63,8 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error {
63 63 a := goheader.New(goheader.WithTemplate(template), goheader.WithValues(values))
64 64
65 65 for _, file := range pass.Files {
66 -position := goanalysis.GetFilePosition(pass, file)
67 -
68 -if !strings.HasSuffix(position.Filename, ".go") {
66 +position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
67 +if !isGoFile {
69 68 continue
70 69 }
71 70
Original file line number Diff line number Diff line change
@@ -43,7 +43,10 @@ func New(settings *config.GoImportsSettings) *goanalysis.Linter {
43 43
44 44 func runGoImports(lintCtx *linter.Context, pass *analysis.Pass) error {
45 45 for _, file := range pass.Files {
46 -position := goanalysis.GetFilePosition(pass, file)
46 +position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
47 +if !isGoFile {
48 +continue
49 + }
47 50
48 51 diff, err := goimportsAPI.Run(position.Filename)
49 52 if err != nil { // TODO: skip
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
1 +//golangcitest:args -Egoimports
2 +//golangcitest:expected_exitcode 0
3 +package p
4 +
5 +/*
6 + #include <stdio.h>
7 + #include <stdlib.h>
8 +
9 + void myprint(char* s) {
10 + printf("%d\n", s);
11 + }
12 +*/
13 +import "C"
14 +
15 +import (
16 +"os"
17 +"fmt"
18 +)
19 +
20 +func goimports(a, b int) int {
21 +if a != b {
22 +return 1
23 + }
24 +return 2
25 +}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
1 +//golangcitest:args -Egoimports
2 +//golangcitest:expected_exitcode 0
3 +package p
4 +
5 +/*
6 + #include <stdio.h>
7 + #include <stdlib.h>
8 +
9 + void myprint(char* s) {
10 + printf("%d\n", s);
11 + }
12 +*/
13 +import "C"
14 +
15 +func goimports(a, b int) int {
16 +if a != b {
17 +return 1
18 + }
19 +return 2
20 +}
Original file line number Diff line number Diff line change
@@ -55,7 +55,11 @@ func runLll(pass *analysis.Pass, settings *config.LllSettings) error {
55 55 }
56 56
57 57 func getLLLIssuesForFile(pass *analysis.Pass, file *ast.File, maxLineLen int, tabSpaces string) error {
58 -position := goanalysis.GetFilePosition(pass, file)
58 +position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
59 +if !isGoFile {
60 +return nil
61 + }
62 +
59 63 nonAdjPosition := pass.Fset.PositionFor(file.Pos(), false)
60 64
61 65 f, err := os.Open(position.Filename)
Original file line number Diff line number Diff line change
@@ -91,11 +91,14 @@ func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replac
91 91 }
92 92
93 93 func runMisspellOnFile(lintCtx *linter.Context, pass *analysis.Pass, file *ast.File, replacer *misspell.Replacer, mode string) error {
94 -filename := goanalysis.GetFilePosition(pass, file).Filename
94 +position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
95 +if !isGoFile {
96 +return nil
97 + }
95 98
96 -fileContent, err := lintCtx.FileCache.GetFileBytes(filename)
99 +fileContent, err := lintCtx.FileCache.GetFileBytes(position.Filename)
97 100 if err != nil {
98 -return fmt.Errorf("can't get file %s contents: %w", filename, err)
101 +return fmt.Errorf("can't get file %s contents: %w", position.Filename, err)
99 102 }
100 103
101 104 // `r.ReplaceGo` doesn't find issues inside strings: it searches only inside comments.
Original file line number Diff line number Diff line change
@@ -35,7 +35,10 @@ func runNestIf(pass *analysis.Pass, settings *config.NestifSettings) {
35 35 }
36 36
37 37 for _, file := range pass.Files {
38 -position := goanalysis.GetFilePosition(pass, file)
38 +position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
39 +if !isGoFile {
40 +continue
41 + }
39 42
40 43 issues := checker.Check(file, pass.Fset)
41 44 if len(issues) == 0 {