github.com/Johnny2210/revive@v1.0.8-0.20210625134200-febf37ccd0f5/rule/duplicated-imports.go (about)

     1  package rule
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mgechev/revive/lint"
     7  )
     8  
     9  // DuplicatedImportsRule lints given else constructs.
    10  type DuplicatedImportsRule struct{}
    11  
    12  // Apply applies the rule to given file.
    13  func (r *DuplicatedImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
    14  	var failures []lint.Failure
    15  
    16  	impPaths := map[string]struct{}{}
    17  	for _, imp := range file.AST.Imports {
    18  		path := imp.Path.Value
    19  		_, ok := impPaths[path]
    20  		if ok {
    21  			failures = append(failures, lint.Failure{
    22  				Confidence: 1,
    23  				Failure:    fmt.Sprintf("Package %s already imported", path),
    24  				Node:       imp,
    25  				Category:   "imports",
    26  			})
    27  			continue
    28  		}
    29  
    30  		impPaths[path] = struct{}{}
    31  	}
    32  
    33  	return failures
    34  }
    35  
    36  // Name returns the rule name.
    37  func (r *DuplicatedImportsRule) Name() string {
    38  	return "duplicated-imports"
    39  }