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

     1  package golinters
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/golangci/golangci-lint/pkg/lint/linter"
     8  	"github.com/golangci/golangci-lint/pkg/result"
     9  	malignedAPI "github.com/golangci/maligned"
    10  )
    11  
    12  type Maligned struct{}
    13  
    14  func (Maligned) Name() string {
    15  	return "maligned"
    16  }
    17  
    18  func (Maligned) Desc() string {
    19  	return "Tool to detect Go structs that would take less memory if their fields were sorted"
    20  }
    21  
    22  func (m Maligned) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
    23  	issues := malignedAPI.Run(lintCtx.Program)
    24  	if len(issues) == 0 {
    25  		return nil, nil
    26  	}
    27  
    28  	res := make([]result.Issue, 0, len(issues))
    29  	for _, i := range issues {
    30  		text := fmt.Sprintf("struct of size %d bytes could be of size %d bytes", i.OldSize, i.NewSize)
    31  		if lintCtx.Settings().Maligned.SuggestNewOrder {
    32  			text += fmt.Sprintf(":\n%s", formatCodeBlock(i.NewStructDef, lintCtx.Cfg))
    33  		}
    34  		res = append(res, result.Issue{
    35  			Pos:        i.Pos,
    36  			Text:       text,
    37  			FromLinter: m.Name(),
    38  		})
    39  	}
    40  	return res, nil
    41  }