1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-23 22:04:49 +02:00
Files
revive/rule/duplicated_imports.go
Oleksandr Redko 395f7902d3 refactor: replace failure Category raw string with constant (#1196)
* refactor: replace Category raw strings with constants

* Add type FailureCategory; add comments for constants
2025-01-18 12:16:19 +01:00

40 lines
862 B
Go

package rule
import (
"fmt"
"github.com/mgechev/revive/lint"
)
// DuplicatedImportsRule looks for packages that are imported two or more times.
type DuplicatedImportsRule struct{}
// Apply applies the rule to given file.
func (*DuplicatedImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
impPaths := map[string]struct{}{}
for _, imp := range file.AST.Imports {
path := imp.Path.Value
_, ok := impPaths[path]
if ok {
failures = append(failures, lint.Failure{
Confidence: 1,
Failure: fmt.Sprintf("Package %s already imported", path),
Node: imp,
Category: lint.FailureCategoryImports,
})
continue
}
impPaths[path] = struct{}{}
}
return failures
}
// Name returns the rule name.
func (*DuplicatedImportsRule) Name() string {
return "duplicated-imports"
}