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

     1  package golinters
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"go/ast"
     7  
     8  	"github.com/golangci/golangci-lint/pkg/lint/linter"
     9  	"github.com/golangci/golangci-lint/pkg/result"
    10  	"github.com/golangci/prealloc"
    11  )
    12  
    13  type Prealloc struct{}
    14  
    15  func (Prealloc) Name() string {
    16  	return "prealloc"
    17  }
    18  
    19  func (Prealloc) Desc() string {
    20  	return "Finds slice declarations that could potentially be preallocated"
    21  }
    22  
    23  func (lint Prealloc) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
    24  	var res []result.Issue
    25  
    26  	s := &lintCtx.Settings().Prealloc
    27  	for _, f := range lintCtx.ASTCache.GetAllValidFiles() {
    28  		hints := prealloc.Check([]*ast.File{f.F}, s.Simple, s.RangeLoops, s.ForLoops)
    29  		for _, hint := range hints {
    30  			res = append(res, result.Issue{
    31  				Pos:        f.Fset.Position(hint.Pos),
    32  				Text:       fmt.Sprintf("Consider preallocating %s", formatCode(hint.DeclaredSliceName, lintCtx.Cfg)),
    33  				FromLinter: lint.Name(),
    34  			})
    35  		}
    36  	}
    37  
    38  	return res, nil
    39  }