Fix #148: fix false-positive from unused if cgo is used
This commit is contained in:
parent
95b0757a3f
commit
1774bf22a2
@ -14,6 +14,7 @@ import (
|
||||
"github.com/golangci/golangci-lint/pkg/lint/linter"
|
||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||
"github.com/golangci/golangci-lint/pkg/result"
|
||||
"github.com/golangci/golangci-lint/pkg/result/processors"
|
||||
"github.com/golangci/golangci-lint/pkg/timeutils"
|
||||
govetAPI "github.com/golangci/govet"
|
||||
)
|
||||
@ -216,6 +217,17 @@ func runGoCommand(ctx context.Context, log logutils.Log, args ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func filterFiles(files []*ast.File, fset *token.FileSet) []*ast.File {
|
||||
newFiles := make([]*ast.File, 0, len(files))
|
||||
for _, f := range files {
|
||||
if !processors.IsCgoFilename(fset.Position(f.Pos()).Filename) {
|
||||
newFiles = append(newFiles, f)
|
||||
}
|
||||
}
|
||||
|
||||
return newFiles
|
||||
}
|
||||
|
||||
func (g Govet) runOnSourcePackages(_ context.Context, lintCtx *linter.Context) ([]govetAPI.Issue, error) {
|
||||
// TODO: check .S asm files: govet can do it if pass dirs
|
||||
var govetIssues []govetAPI.Issue
|
||||
@ -223,7 +235,9 @@ func (g Govet) runOnSourcePackages(_ context.Context, lintCtx *linter.Context) (
|
||||
if len(pkg.Files) == 0 {
|
||||
continue
|
||||
}
|
||||
issues, err := govetAPI.Analyze(pkg.Files, lintCtx.Program.Fset, pkg,
|
||||
|
||||
filteredFiles := filterFiles(pkg.Files, lintCtx.Program.Fset)
|
||||
issues, err := govetAPI.Analyze(filteredFiles, lintCtx.Program.Fset, pkg,
|
||||
lintCtx.Settings().Govet.CheckShadowing)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -2,10 +2,8 @@ package lint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/build"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@ -13,7 +11,6 @@ import (
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/goutils"
|
||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||
"github.com/golangci/golangci-lint/pkg/result/processors"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/config"
|
||||
"github.com/golangci/golangci-lint/pkg/lint/astcache"
|
||||
@ -260,30 +257,6 @@ func separateNotCompilingPackages(lintCtx *linter.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func removeFakePkgFiles(info *loader.PackageInfo, fset *token.FileSet) {
|
||||
newFiles := make([]*ast.File, 0, len(info.Files))
|
||||
for _, f := range info.Files {
|
||||
if !processors.IsCgoFilename(fset.Position(f.Pos()).Filename) {
|
||||
newFiles = append(newFiles, f)
|
||||
}
|
||||
}
|
||||
info.Files = newFiles
|
||||
}
|
||||
|
||||
func removeFakePackages(prog *loader.Program) {
|
||||
if prog.Created != nil {
|
||||
for _, info := range prog.Created {
|
||||
removeFakePkgFiles(info, prog.Fset)
|
||||
}
|
||||
}
|
||||
|
||||
if prog.Imported != nil {
|
||||
for _, info := range prog.Imported {
|
||||
removeFakePkgFiles(info, prog.Fset)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:gocyclo
|
||||
func LoadContext(linters []linter.Config, cfg *config.Config, log logutils.Log) (*linter.Context, error) {
|
||||
// Set GOROOT to have working cross-compilation: cross-compiled binaries
|
||||
@ -322,11 +295,6 @@ func LoadContext(linters []linter.Config, cfg *config.Config, log logutils.Log)
|
||||
ssaProg = buildSSAProgram(prog, log)
|
||||
}
|
||||
|
||||
if prog != nil {
|
||||
// It's important to do it after SSA building
|
||||
removeFakePackages(prog)
|
||||
}
|
||||
|
||||
astLog := log.Child("astcache")
|
||||
var astCache *astcache.Cache
|
||||
if prog != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user