Skip to content

Commit

Permalink
Fix many linters ignoring Cgo files
Browse files Browse the repository at this point in the history
This change fixes a bug caused that many linters ignored all files that are
using Cgo. It was introduced by PR #1065, which assumed that all files
referenced by //line directives are non-Go files, which is not true. In the case
of Cgo they point to the original Go files which are used by Cgo as templates to
generate other Go files.

The fix replaces all calls of Pass.Fset.PositionFor with a function
getFileNames() that first calls Pass.Fset.PositionFor with adjustment, and only
if it results in a non-Go file it calls Pass.Fset.PositionFor again without
adjustment.

Fixes: #2910

Signed-off-by: Sven Anderson <sven@anderson.de>
  • Loading branch information
ansiwen committed Jul 30, 2022
1 parent 5d6e9d7 commit ce4f33f
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 53 deletions.
6 changes: 1 addition & 5 deletions pkg/golinters/dupl.go
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
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
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
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
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
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
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
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
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
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
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
4 changes: 3 additions & 1 deletion test/run_test.go
Expand Up @@ -106,6 +106,8 @@ func TestCgoWithIssues(t *testing.T) {
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 @@ -129,7 +131,7 @@ func TestLineDirectiveProcessedFilesLiteLoading(t *testing.T) {
}

func TestSortedResults(t *testing.T) {
var testCases = []struct {
testCases := []struct {
opt string
want string
}{
Expand Down

0 comments on commit ce4f33f

Please sign in to comment.