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

     1  package golinters
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"go/token"
     7  
     8  	"github.com/golangci/golangci-lint/pkg/lint/linter"
     9  	"github.com/golangci/golangci-lint/pkg/result"
    10  	duplAPI "github.com/mibk/dupl"
    11  )
    12  
    13  type Dupl struct{}
    14  
    15  func (Dupl) Name() string {
    16  	return "dupl"
    17  }
    18  
    19  func (Dupl) Desc() string {
    20  	return "Tool for code clone detection"
    21  }
    22  
    23  func (d Dupl) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
    24  	issues, err := duplAPI.Run(lintCtx.PkgProgram.Files(lintCtx.Cfg.Run.AnalyzeTests), lintCtx.Settings().Dupl.Threshold)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	if len(issues) == 0 {
    30  		return nil, nil
    31  	}
    32  
    33  	res := make([]result.Issue, 0, len(issues))
    34  	for _, i := range issues {
    35  		dupl := fmt.Sprintf("%s:%d-%d", i.To.Filename(), i.To.LineStart(), i.To.LineEnd())
    36  		text := fmt.Sprintf("%d-%d lines are duplicate of %s",
    37  			i.From.LineStart(), i.From.LineEnd(),
    38  			formatCode(dupl, lintCtx.Cfg))
    39  		res = append(res, result.Issue{
    40  			Pos: token.Position{
    41  				Filename: i.From.Filename(),
    42  				Line:     i.From.LineStart(),
    43  			},
    44  			LineRange: &result.Range{
    45  				From: i.From.LineStart(),
    46  				To:   i.From.LineEnd(),
    47  			},
    48  			Text:       text,
    49  			FromLinter: d.Name(),
    50  		})
    51  	}
    52  	return res, nil
    53  }