github.com/elek/golangci-lint@v1.42.2-0.20211208090441-c05b7fcb3a9a/pkg/result/processors/cgo.go (about)

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