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

     1  package golinters // nolint:dupl
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	structcheckAPI "github.com/golangci/check/cmd/structcheck"
     8  	"github.com/golangci/golangci-lint/pkg/lint/linter"
     9  	"github.com/golangci/golangci-lint/pkg/result"
    10  )
    11  
    12  type Structcheck struct{}
    13  
    14  func (Structcheck) Name() string {
    15  	return "structcheck"
    16  }
    17  
    18  func (Structcheck) Desc() string {
    19  	return "Finds an unused struct fields"
    20  }
    21  
    22  func (s Structcheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
    23  	issues := structcheckAPI.Run(lintCtx.Program, lintCtx.Settings().Structcheck.CheckExportedFields)
    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  		res = append(res, result.Issue{
    31  			Pos:        i.Pos,
    32  			Text:       fmt.Sprintf("%s is unused", formatCode(i.FieldName, lintCtx.Cfg)),
    33  			FromLinter: s.Name(),
    34  		})
    35  	}
    36  	return res, nil
    37  }