You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

duplicated-imports.go 821B

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