Skip to content

internal/imports: remove duplicate unused imports #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions internal/imports/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ type pass struct {
otherFiles []*ast.File // sibling files.

// Intermediate state, generated by load.
existingImports map[string]*ImportInfo
existingImports map[string][]*ImportInfo
allRefs references
missingRefs references

Expand Down Expand Up @@ -317,7 +317,7 @@ func (p *pass) importIdentifier(imp *ImportInfo) string {
func (p *pass) load() ([]*ImportFix, bool) {
p.knownPackages = map[string]*packageInfo{}
p.missingRefs = references{}
p.existingImports = map[string]*ImportInfo{}
p.existingImports = map[string][]*ImportInfo{}

// Load basic information about the file in question.
p.allRefs = collectReferences(p.f)
Expand Down Expand Up @@ -348,7 +348,7 @@ func (p *pass) load() ([]*ImportFix, bool) {
}
}
for _, imp := range imports {
p.existingImports[p.importIdentifier(imp)] = imp
p.existingImports[p.importIdentifier(imp)] = append(p.existingImports[p.importIdentifier(imp)], imp)
}

// Find missing references.
Expand Down Expand Up @@ -387,31 +387,33 @@ func (p *pass) fix() ([]*ImportFix, bool) {

// Found everything, or giving up. Add the new imports and remove any unused.
var fixes []*ImportFix
for _, imp := range p.existingImports {
// We deliberately ignore globals here, because we can't be sure
// they're in the same package. People do things like put multiple
// main packages in the same directory, and we don't want to
// remove imports if they happen to have the same name as a var in
// a different package.
if _, ok := p.allRefs[p.importIdentifier(imp)]; !ok {
fixes = append(fixes, &ImportFix{
StmtInfo: *imp,
IdentName: p.importIdentifier(imp),
FixType: DeleteImport,
})
continue
}
for _, identifierImports := range p.existingImports {
for _, imp := range identifierImports {
// We deliberately ignore globals here, because we can't be sure
// they're in the same package. People do things like put multiple
// main packages in the same directory, and we don't want to
// remove imports if they happen to have the same name as a var in
// a different package.
if _, ok := p.allRefs[p.importIdentifier(imp)]; !ok {
fixes = append(fixes, &ImportFix{
StmtInfo: *imp,
IdentName: p.importIdentifier(imp),
FixType: DeleteImport,
})
continue
}

// An existing import may need to update its import name to be correct.
if name := p.importSpecName(imp); name != imp.Name {
fixes = append(fixes, &ImportFix{
StmtInfo: ImportInfo{
Name: name,
ImportPath: imp.ImportPath,
},
IdentName: p.importIdentifier(imp),
FixType: SetImportName,
})
// An existing import may need to update its import name to be correct.
if name := p.importSpecName(imp); name != imp.Name {
fixes = append(fixes, &ImportFix{
StmtInfo: ImportInfo{
Name: name,
ImportPath: imp.ImportPath,
},
IdentName: p.importIdentifier(imp),
FixType: SetImportName,
})
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions internal/imports/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,19 @@ var _, _ = rand.Read, rand.NewZipf
import "math/rand"

var _, _ = rand.Read, rand.NewZipf
`,
},
{
name: "unused_duplicate_imports_remove",
in: `package main

import (
"errors"

"github.com/pkg/errors"
)
`,
out: `package main
`,
},
}
Expand Down