Skip to content

Commit 1774bf2

Browse files
committed
Fix #148: fix false-positive from unused if cgo is used
1 parent 95b0757 commit 1774bf2

File tree

2 files changed

+15
-33
lines changed

2 files changed

+15
-33
lines changed

pkg/golinters/govet.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/golangci/golangci-lint/pkg/lint/linter"
1515
"github.com/golangci/golangci-lint/pkg/logutils"
1616
"github.com/golangci/golangci-lint/pkg/result"
17+
"github.com/golangci/golangci-lint/pkg/result/processors"
1718
"github.com/golangci/golangci-lint/pkg/timeutils"
1819
govetAPI "github.com/golangci/govet"
1920
)
@@ -216,14 +217,27 @@ func runGoCommand(ctx context.Context, log logutils.Log, args ...string) error {
216217
return nil
217218
}
218219

220+
func filterFiles(files []*ast.File, fset *token.FileSet) []*ast.File {
221+
newFiles := make([]*ast.File, 0, len(files))
222+
for _, f := range files {
223+
if !processors.IsCgoFilename(fset.Position(f.Pos()).Filename) {
224+
newFiles = append(newFiles, f)
225+
}
226+
}
227+
228+
return newFiles
229+
}
230+
219231
func (g Govet) runOnSourcePackages(_ context.Context, lintCtx *linter.Context) ([]govetAPI.Issue, error) {
220232
// TODO: check .S asm files: govet can do it if pass dirs
221233
var govetIssues []govetAPI.Issue
222234
for _, pkg := range lintCtx.Program.InitialPackages() {
223235
if len(pkg.Files) == 0 {
224236
continue
225237
}
226-
issues, err := govetAPI.Analyze(pkg.Files, lintCtx.Program.Fset, pkg,
238+
239+
filteredFiles := filterFiles(pkg.Files, lintCtx.Program.Fset)
240+
issues, err := govetAPI.Analyze(filteredFiles, lintCtx.Program.Fset, pkg,
227241
lintCtx.Settings().Govet.CheckShadowing)
228242
if err != nil {
229243
return nil, err

pkg/lint/load.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ package lint
22

33
import (
44
"fmt"
5-
"go/ast"
65
"go/build"
76
"go/parser"
8-
"go/token"
97
"os"
108
"path/filepath"
119
"strings"
1210
"time"
1311

1412
"github.com/golangci/golangci-lint/pkg/goutils"
1513
"github.com/golangci/golangci-lint/pkg/logutils"
16-
"github.com/golangci/golangci-lint/pkg/result/processors"
1714

1815
"github.com/golangci/golangci-lint/pkg/config"
1916
"github.com/golangci/golangci-lint/pkg/lint/astcache"
@@ -260,30 +257,6 @@ func separateNotCompilingPackages(lintCtx *linter.Context) {
260257
}
261258
}
262259

263-
func removeFakePkgFiles(info *loader.PackageInfo, fset *token.FileSet) {
264-
newFiles := make([]*ast.File, 0, len(info.Files))
265-
for _, f := range info.Files {
266-
if !processors.IsCgoFilename(fset.Position(f.Pos()).Filename) {
267-
newFiles = append(newFiles, f)
268-
}
269-
}
270-
info.Files = newFiles
271-
}
272-
273-
func removeFakePackages(prog *loader.Program) {
274-
if prog.Created != nil {
275-
for _, info := range prog.Created {
276-
removeFakePkgFiles(info, prog.Fset)
277-
}
278-
}
279-
280-
if prog.Imported != nil {
281-
for _, info := range prog.Imported {
282-
removeFakePkgFiles(info, prog.Fset)
283-
}
284-
}
285-
}
286-
287260
//nolint:gocyclo
288261
func LoadContext(linters []linter.Config, cfg *config.Config, log logutils.Log) (*linter.Context, error) {
289262
// 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)
322295
ssaProg = buildSSAProgram(prog, log)
323296
}
324297

325-
if prog != nil {
326-
// It's important to do it after SSA building
327-
removeFakePackages(prog)
328-
}
329-
330298
astLog := log.Child("astcache")
331299
var astCache *astcache.Cache
332300
if prog != nil {

0 commit comments

Comments
 (0)