github.com/chenfeining/golangci-lint@v1.0.2-0.20230730162517-14c6c67868df/pkg/result/processors/cgo.go (about)

     1  package processors
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/chenfeining/golangci-lint/pkg/goutil"
     9  	"github.com/chenfeining/golangci-lint/pkg/result"
    10  )
    11  
    12  type Cgo struct {
    13  	goCacheDir string
    14  }
    15  
    16  var _ Processor = Cgo{}
    17  
    18  func NewCgo(goenv *goutil.Env) *Cgo {
    19  	return &Cgo{
    20  		goCacheDir: goenv.Get(goutil.EnvGoCache),
    21  	}
    22  }
    23  
    24  func (p Cgo) Name() string {
    25  	return "cgo"
    26  }
    27  
    28  func (p Cgo) Process(issues []result.Issue) ([]result.Issue, error) {
    29  	return filterIssuesErr(issues, func(i *result.Issue) (bool, error) {
    30  		// some linters (.e.g gosec, deadcode) return incorrect filepaths for cgo issues,
    31  		// also cgo files have strange issues looking like false positives.
    32  
    33  		// cache dir contains all preprocessed files including cgo files
    34  
    35  		issueFilePath := i.FilePath()
    36  		if !filepath.IsAbs(i.FilePath()) {
    37  			absPath, err := filepath.Abs(i.FilePath())
    38  			if err != nil {
    39  				return false, fmt.Errorf("failed to build abs path for %q: %w", i.FilePath(), err)
    40  			}
    41  			issueFilePath = absPath
    42  		}
    43  
    44  		if p.goCacheDir != "" && strings.HasPrefix(issueFilePath, p.goCacheDir) {
    45  			return false, nil
    46  		}
    47  
    48  		if filepath.Base(i.FilePath()) == "_cgo_gotypes.go" {
    49  			// skip cgo warning for go1.10
    50  			return false, nil
    51  		}
    52  
    53  		return true, nil
    54  	})
    55  }
    56  
    57  func (Cgo) Finish() {}