www.github.com/golangci/golangci-lint.git@v1.10.1/pkg/golinters/deadcode.go (about)

     1  package golinters
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	deadcodeAPI "github.com/golangci/go-misc/deadcode"
     8  	"github.com/golangci/golangci-lint/pkg/lint/linter"
     9  	"github.com/golangci/golangci-lint/pkg/result"
    10  )
    11  
    12  type Deadcode struct{}
    13  
    14  func (Deadcode) Name() string {
    15  	return "deadcode"
    16  }
    17  
    18  func (Deadcode) Desc() string {
    19  	return "Finds unused code"
    20  }
    21  
    22  func (d Deadcode) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
    23  	issues, err := deadcodeAPI.Run(lintCtx.Program)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	if len(issues) == 0 {
    29  		return nil, nil
    30  	}
    31  
    32  	res := make([]result.Issue, 0, len(issues))
    33  	for _, i := range issues {
    34  		res = append(res, result.Issue{
    35  			Pos:        i.Pos,
    36  			Text:       fmt.Sprintf("%s is unused", formatCode(i.UnusedIdentName, lintCtx.Cfg)),
    37  			FromLinter: d.Name(),
    38  		})
    39  	}
    40  	return res, nil
    41  }