Skip to content

Commit

Permalink
cgo: fix linters ignoring Cgo files (golangci#3025)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansiwen authored and SeigeC committed Apr 4, 2023
1 parent 39b06d1 commit 7cf0ae5
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 54 deletions.
6 changes: 1 addition & 5 deletions pkg/golinters/dupl.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ func NewDupl(settings *config.DuplSettings) *goanalysis.Linter {
}

func runDupl(pass *analysis.Pass, settings *config.DuplSettings) ([]goanalysis.Issue, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
fileNames := getFileNames(pass)

issues, err := duplAPI.Run(fileNames, settings.Threshold)
if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions pkg/golinters/gci.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ func NewGci(settings *config.GciSettings) *goanalysis.Linter {
}

func runGci(pass *analysis.Pass, lintCtx *linter.Context, cfg *gcicfg.Config, lock *sync.Mutex) ([]goanalysis.Issue, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
fileNames := getFileNames(pass)

var diffs []string
err := diffFormattedFilesToArray(fileNames, *cfg, &diffs, lock)
Expand Down
6 changes: 1 addition & 5 deletions pkg/golinters/gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ func NewGofmt(settings *config.GoFmtSettings) *goanalysis.Linter {
}

func runGofmt(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GoFmtSettings) ([]goanalysis.Issue, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
fileNames := getFileNames(pass)

var issues []goanalysis.Issue

Expand Down
6 changes: 1 addition & 5 deletions pkg/golinters/gofumpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ func NewGofumpt(settings *config.GofumptSettings) *goanalysis.Linter {
}

func runGofumpt(lintCtx *linter.Context, pass *analysis.Pass, diff differ, options format.Options) ([]goanalysis.Issue, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
fileNames := getFileNames(pass)

var issues []goanalysis.Issue

Expand Down
6 changes: 1 addition & 5 deletions pkg/golinters/goimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ func NewGoimports(settings *config.GoImportsSettings) *goanalysis.Linter {
}

func runGoiImports(lintCtx *linter.Context, pass *analysis.Pass) ([]goanalysis.Issue, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
fileNames := getFileNames(pass)

var issues []goanalysis.Issue

Expand Down
7 changes: 1 addition & 6 deletions pkg/golinters/gomodguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ func NewGomodguard(settings *config.GoModGuardSettings) *goanalysis.Linter {
}

analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var files []string
for _, file := range pass.Files {
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
}

gomodguardIssues := processor.ProcessFiles(files)
gomodguardIssues := processor.ProcessFiles(getFileNames(pass))

mu.Lock()
defer mu.Unlock()
Expand Down
6 changes: 1 addition & 5 deletions pkg/golinters/lll.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ func NewLLL(settings *config.LllSettings) *goanalysis.Linter {
}

func runLll(pass *analysis.Pass, settings *config.LllSettings) ([]goanalysis.Issue, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
fileNames := getFileNames(pass)

spaces := strings.Repeat(" ", settings.TabWidth)

Expand Down
7 changes: 1 addition & 6 deletions pkg/golinters/misspell.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@ func NewMisspell(settings *config.MisspellSettings) *goanalysis.Linter {
}

func runMisspell(lintCtx *linter.Context, pass *analysis.Pass, replacer *misspell.Replacer) ([]goanalysis.Issue, error) {
var fileNames []string

for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
fileNames := getFileNames(pass)

var issues []goanalysis.Issue
for _, filename := range fileNames {
Expand Down
6 changes: 1 addition & 5 deletions pkg/golinters/revive.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,7 @@ func NewRevive(settings *config.ReviveSettings) *goanalysis.Linter {
}

func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.ReviveSettings) ([]goanalysis.Issue, error) {
var files []string
for _, file := range pass.Files {
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
}
packages := [][]string{files}
packages := [][]string{getFileNames(pass)}

conf, err := getReviveConfig(settings)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions pkg/golinters/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package golinters

import (
"fmt"
"path/filepath"
"strings"

"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
)

Expand All @@ -22,3 +25,17 @@ func formatCodeBlock(code string, _ *config.Config) string {

return fmt.Sprintf("```\n%s\n```", code)
}

func getFileNames(pass *analysis.Pass) []string {
var fileNames []string
for _, f := range pass.Files {
fileName := pass.Fset.PositionFor(f.Pos(), true).Filename
ext := filepath.Ext(fileName)
if ext != "" && ext != ".go" {
// position has been adjusted to a non-go file, revert to original file
fileName = pass.Fset.PositionFor(f.Pos(), false).Filename
}
fileNames = append(fileNames, fileName)
}
return fileNames
}
6 changes: 1 addition & 5 deletions pkg/golinters/wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,11 @@ func NewWSL(settings *config.WSLSettings) *goanalysis.Linter {
}

func runWSL(pass *analysis.Pass, conf *wsl.Configuration) []goanalysis.Issue {
var files = make([]string, 0, len(pass.Files))
for _, file := range pass.Files {
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
}

if conf == nil {
return nil
}

files := getFileNames(pass)
wslErrors, _ := wsl.NewProcessorWithConfig(*conf).ProcessFiles(files)
if len(wslErrors) == 0 {
return nil
Expand Down
8 changes: 6 additions & 2 deletions test/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestTestsAreLintedByDefault(t *testing.T) {
}

func TestCgoOk(t *testing.T) {
testshared.NewLintRunner(t).Run("--no-config", "--enable-all", "-D", "nosnakecase", getTestDataDir("cgo")).ExpectNoIssues()
testshared.NewLintRunner(t).Run("--no-config", "--enable-all", "-D", "nosnakecase,gci", getTestDataDir("cgo")).ExpectNoIssues()
}

func TestCgoWithIssues(t *testing.T) {
Expand All @@ -104,6 +104,10 @@ func TestCgoWithIssues(t *testing.T) {
ExpectHasIssue("Printf format %t has arg cs of wrong type")
r.Run("--no-config", "--disable-all", "-Estaticcheck", getTestDataDir("cgo_with_issues")).
ExpectHasIssue("SA5009: Printf format %t has arg #1 of wrong type")
r.Run("--no-config", "--disable-all", "-Egofmt", getTestDataDir("cgo_with_issues")).
ExpectHasIssue("File is not `gofmt`-ed with `-s` (gofmt)")
r.Run("--no-config", "--disable-all", "-Erevive", getTestDataDir("cgo_with_issues")).
ExpectHasIssue("indent-error-flow: if block ends with a return statement")
}

func TestUnsafeOk(t *testing.T) {
Expand All @@ -127,7 +131,7 @@ func TestLineDirectiveProcessedFilesLiteLoading(t *testing.T) {
}

func TestSortedResults(t *testing.T) {
var testCases = []struct {
testCases := []struct {
opt string
want string
}{
Expand Down
12 changes: 12 additions & 0 deletions test/testdata/cgo_with_issues/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ func Example() {
fmt.Printf("bad format %t", cs)
C.free(unsafe.Pointer(cs))
}

func notFormattedForGofmt() {
}

func errorForRevive(p *int) error {
if p == nil {
return nil
} else {
return nil
}
}

0 comments on commit 7cf0ae5

Please sign in to comment.