Skip to content

Commit c114969

Browse files
authored
fix: filter Go filenames (#5291)
1 parent c58ddd3 commit c114969

File tree

9 files changed

+65
-33
lines changed

9 files changed

+65
-33
lines changed

pkg/goanalysis/position.go

-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import (
88
"golang.org/x/tools/go/analysis"
99
)
1010

11-
func GetFilePosition(pass *analysis.Pass, f *ast.File) token.Position {
12-
return GetFilePositionFor(pass.Fset, f.Pos())
13-
}
14-
1511
func GetGoFilePosition(pass *analysis.Pass, f *ast.File) (token.Position, bool) {
1612
position := GetFilePositionFor(pass.Fset, f.Pos())
1713

pkg/golinters/dupl/dupl.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package dupl
33
import (
44
"fmt"
55
"go/token"
6-
"strings"
76
"sync"
87

98
duplAPI "github.com/golangci/dupl"
@@ -55,19 +54,7 @@ func New(settings *config.DuplSettings) *goanalysis.Linter {
5554
}
5655

5756
func runDupl(pass *analysis.Pass, settings *config.DuplSettings) ([]goanalysis.Issue, error) {
58-
fileNames := internal.GetFileNames(pass)
59-
60-
var onlyGofiles []string
61-
for _, name := range fileNames {
62-
// Related to Windows
63-
if !strings.HasSuffix(name, ".go") {
64-
continue
65-
}
66-
67-
onlyGofiles = append(onlyGofiles, name)
68-
}
69-
70-
issues, err := duplAPI.Run(onlyGofiles, settings.Threshold)
57+
issues, err := duplAPI.Run(internal.GetGoFileNames(pass), settings.Threshold)
7158
if err != nil {
7259
return nil, err
7360
}

pkg/golinters/gomodguard/gomodguard.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func New(settings *config.GoModGuardSettings) *goanalysis.Linter {
7373
}
7474

7575
analyzer.Run = func(pass *analysis.Pass) (any, error) {
76-
gomodguardIssues := processor.ProcessFiles(internal.GetFileNames(pass))
76+
gomodguardIssues := processor.ProcessFiles(internal.GetGoFileNames(pass))
7777

7878
mu.Lock()
7979
defer mu.Unlock()

pkg/golinters/internal/util.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@ func FormatCode(code string, _ *config.Config) string {
1818
return fmt.Sprintf("`%s`", code)
1919
}
2020

21-
func GetFileNames(pass *analysis.Pass) []string {
21+
func GetGoFileNames(pass *analysis.Pass) []string {
2222
var filenames []string
23+
2324
for _, f := range pass.Files {
24-
filenames = append(filenames, goanalysis.GetFilePosition(pass, f).Filename)
25+
position, b := goanalysis.GetGoFilePosition(pass, f)
26+
if !b {
27+
continue
28+
}
29+
30+
filenames = append(filenames, position.Filename)
2531
}
32+
2633
return filenames
2734
}

pkg/golinters/revive/revive.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func newWrapper(settings *config.ReviveSettings) (*wrapper, error) {
115115
}
116116

117117
func (w *wrapper) run(lintCtx *linter.Context, pass *analysis.Pass) ([]goanalysis.Issue, error) {
118-
packages := [][]string{internal.GetFileNames(pass)}
118+
packages := [][]string{internal.GetGoFileNames(pass)}
119119

120120
failures, err := w.revive.Lint(packages, w.lintingRules, *w.conf)
121121
if err != nil {
@@ -184,17 +184,16 @@ func toIssue(pass *analysis.Pass, object *jsonObject) goanalysis.Issue {
184184
if object.ReplacementLine != "" {
185185
f := pass.Fset.File(token.Pos(object.Position.Start.Offset))
186186

187-
start := f.LineStart(object.Position.Start.Line)
188-
189-
end := goanalysis.EndOfLinePos(f, object.Position.End.Line)
190-
191-
issue.SuggestedFixes = []analysis.SuggestedFix{{
192-
TextEdits: []analysis.TextEdit{{
193-
Pos: start,
194-
End: end,
195-
NewText: []byte(object.ReplacementLine),
196-
}},
197-
}}
187+
// Skip cgo files because the positions are wrong.
188+
if object.GetFilename() == f.Name() {
189+
issue.SuggestedFixes = []analysis.SuggestedFix{{
190+
TextEdits: []analysis.TextEdit{{
191+
Pos: f.LineStart(object.Position.Start.Line),
192+
End: goanalysis.EndOfLinePos(f, object.Position.End.Line),
193+
NewText: []byte(object.ReplacementLine),
194+
}},
195+
}}
196+
}
198197
}
199198

200199
return goanalysis.NewIssue(issue, pass)

pkg/golinters/revive/revive_integration_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ import (
99
func TestFromTestdata(t *testing.T) {
1010
integration.RunTestdata(t)
1111
}
12+
13+
func TestFix(t *testing.T) {
14+
integration.RunFix(t)
15+
}
16+
17+
func TestFixPathPrefix(t *testing.T) {
18+
integration.RunFixPathPrefix(t)
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//golangcitest:args -Erevive
2+
//golangcitest:config_path testdata/revive-fix.yml
3+
//golangcitest:expected_exitcode 0
4+
package in
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"math"
10+
)
11+
12+
func _() error {
13+
return errors.New(fmt.Sprintf("foo: %d", math.MaxInt))
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//golangcitest:args -Erevive
2+
//golangcitest:config_path testdata/revive-fix.yml
3+
//golangcitest:expected_exitcode 0
4+
package in
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"math"
10+
)
11+
12+
func _() error {
13+
return fmt.Errorf("foo: %d", math.MaxInt)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
linters-settings:
2+
revive:
3+
ignore-generated-header: true
4+
severity: warning
5+
rules:
6+
- name: errorf
7+
- name: range

0 commit comments

Comments
 (0)