github.com/firelizzard18/golangci-lint@v1.10.1/pkg/golinters/gas.go (about)

     1  package golinters
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"go/token"
     7  	"io/ioutil"
     8  	"log"
     9  	"strconv"
    10  
    11  	"github.com/GoASTScanner/gas"
    12  	"github.com/GoASTScanner/gas/rules"
    13  	"github.com/golangci/golangci-lint/pkg/lint/linter"
    14  	"github.com/golangci/golangci-lint/pkg/result"
    15  )
    16  
    17  type Gas struct{}
    18  
    19  func (Gas) Name() string {
    20  	return "gas"
    21  }
    22  
    23  func (Gas) Desc() string {
    24  	return "Inspects source code for security problems"
    25  }
    26  
    27  func (lint Gas) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
    28  	gasConfig := gas.NewConfig()
    29  	enabledRules := rules.Generate()
    30  	logger := log.New(ioutil.Discard, "", 0)
    31  	analyzer := gas.NewAnalyzer(gasConfig, logger)
    32  	analyzer.LoadRules(enabledRules.Builders())
    33  
    34  	analyzer.ProcessProgram(lintCtx.Program)
    35  	issues, _ := analyzer.Report()
    36  	if len(issues) == 0 {
    37  		return nil, nil
    38  	}
    39  
    40  	res := make([]result.Issue, 0, len(issues))
    41  	for _, i := range issues {
    42  		text := fmt.Sprintf("%s: %s", i.RuleID, markIdentifiers(i.What)) // TODO: use severity and confidence
    43  		var r *result.Range
    44  		line, err := strconv.Atoi(i.Line)
    45  		if err != nil {
    46  			r = &result.Range{}
    47  			if n, rerr := fmt.Sscanf(i.Line, "%d-%d", &r.From, &r.To); rerr != nil || n != 2 {
    48  				lintCtx.Log.Warnf("Can't convert gas line number %q of %v to int: %s", i.Line, i, err)
    49  				continue
    50  			}
    51  			line = r.From
    52  		}
    53  
    54  		res = append(res, result.Issue{
    55  			Pos: token.Position{
    56  				Filename: i.File,
    57  				Line:     line,
    58  			},
    59  			Text:       text,
    60  			LineRange:  r,
    61  			FromLinter: lint.Name(),
    62  		})
    63  	}
    64  
    65  	return res, nil
    66  }