cgo: fix linters ignoring Cgo files (#3025)

This commit is contained in:
Sven Anderson 2022-07-30 22:26:33 +02:00 committed by GitHub
parent 886fbd71a8
commit 846fab81b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 45 additions and 54 deletions

View File

@ -55,11 +55,7 @@ func NewDupl(settings *config.DuplSettings) *goanalysis.Linter {
} }
func runDupl(pass *analysis.Pass, settings *config.DuplSettings) ([]goanalysis.Issue, error) { func runDupl(pass *analysis.Pass, settings *config.DuplSettings) ([]goanalysis.Issue, error) {
var fileNames []string fileNames := getFileNames(pass)
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
issues, err := duplAPI.Run(fileNames, settings.Threshold) issues, err := duplAPI.Run(fileNames, settings.Threshold)
if err != nil { if err != nil {

View File

@ -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) { func runGci(pass *analysis.Pass, lintCtx *linter.Context, cfg *gcicfg.Config, lock *sync.Mutex) ([]goanalysis.Issue, error) {
var fileNames []string fileNames := getFileNames(pass)
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
var diffs []string var diffs []string
err := diffFormattedFilesToArray(fileNames, *cfg, &diffs, lock) err := diffFormattedFilesToArray(fileNames, *cfg, &diffs, lock)

View File

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

View File

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

View File

@ -55,11 +55,7 @@ func NewGoimports(settings *config.GoImportsSettings) *goanalysis.Linter {
} }
func runGoiImports(lintCtx *linter.Context, pass *analysis.Pass) ([]goanalysis.Issue, error) { func runGoiImports(lintCtx *linter.Context, pass *analysis.Pass) ([]goanalysis.Issue, error) {
var fileNames []string fileNames := getFileNames(pass)
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
var issues []goanalysis.Issue var issues []goanalysis.Issue

View File

@ -73,12 +73,7 @@ func NewGomodguard(settings *config.GoModGuardSettings) *goanalysis.Linter {
} }
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var files []string gomodguardIssues := processor.ProcessFiles(getFileNames(pass))
for _, file := range pass.Files {
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
}
gomodguardIssues := processor.ProcessFiles(files)
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()

View File

@ -56,11 +56,7 @@ func NewLLL(settings *config.LllSettings) *goanalysis.Linter {
} }
func runLll(pass *analysis.Pass, settings *config.LllSettings) ([]goanalysis.Issue, error) { func runLll(pass *analysis.Pass, settings *config.LllSettings) ([]goanalysis.Issue, error) {
var fileNames []string fileNames := getFileNames(pass)
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
spaces := strings.Repeat(" ", settings.TabWidth) spaces := strings.Repeat(" ", settings.TabWidth)

View File

@ -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) { func runMisspell(lintCtx *linter.Context, pass *analysis.Pass, replacer *misspell.Replacer) ([]goanalysis.Issue, error) {
var fileNames []string fileNames := getFileNames(pass)
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
var issues []goanalysis.Issue var issues []goanalysis.Issue
for _, filename := range fileNames { for _, filename := range fileNames {

View File

@ -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) { func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.ReviveSettings) ([]goanalysis.Issue, error) {
var files []string packages := [][]string{getFileNames(pass)}
for _, file := range pass.Files {
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
}
packages := [][]string{files}
conf, err := getReviveConfig(settings) conf, err := getReviveConfig(settings)
if err != nil { if err != nil {

View File

@ -2,8 +2,11 @@ package golinters
import ( import (
"fmt" "fmt"
"path/filepath"
"strings" "strings"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/config"
) )
@ -22,3 +25,17 @@ func formatCodeBlock(code string, _ *config.Config) string {
return fmt.Sprintf("```\n%s\n```", code) 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
}

View File

@ -67,15 +67,11 @@ func NewWSL(settings *config.WSLSettings) *goanalysis.Linter {
} }
func runWSL(pass *analysis.Pass, conf *wsl.Configuration) []goanalysis.Issue { 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 { if conf == nil {
return nil return nil
} }
files := getFileNames(pass)
wslErrors, _ := wsl.NewProcessorWithConfig(*conf).ProcessFiles(files) wslErrors, _ := wsl.NewProcessorWithConfig(*conf).ProcessFiles(files)
if len(wslErrors) == 0 { if len(wslErrors) == 0 {
return nil return nil

View File

@ -95,7 +95,7 @@ func TestTestsAreLintedByDefault(t *testing.T) {
} }
func TestCgoOk(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) { func TestCgoWithIssues(t *testing.T) {
@ -104,6 +104,10 @@ func TestCgoWithIssues(t *testing.T) {
ExpectHasIssue("Printf format %t has arg cs of wrong type") ExpectHasIssue("Printf format %t has arg cs of wrong type")
r.Run("--no-config", "--disable-all", "-Estaticcheck", getTestDataDir("cgo_with_issues")). r.Run("--no-config", "--disable-all", "-Estaticcheck", getTestDataDir("cgo_with_issues")).
ExpectHasIssue("SA5009: Printf format %t has arg #1 of wrong type") 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) { func TestUnsafeOk(t *testing.T) {
@ -127,7 +131,7 @@ func TestLineDirectiveProcessedFilesLiteLoading(t *testing.T) {
} }
func TestSortedResults(t *testing.T) { func TestSortedResults(t *testing.T) {
var testCases = []struct { testCases := []struct {
opt string opt string
want string want string
}{ }{

View File

@ -21,3 +21,15 @@ func Example() {
fmt.Printf("bad format %t", cs) fmt.Printf("bad format %t", cs)
C.free(unsafe.Pointer(cs)) C.free(unsafe.Pointer(cs))
} }
func notFormattedForGofmt() {
}
func errorForRevive(p *int) error {
if p == nil {
return nil
} else {
return nil
}
}