Don't remove unused imports directly
Closed this issue · 0 comments
mvdan commented
If we import package foo
and we remove a node containing the only use of that imported package, like foo.Bar
, we will remove the import.
This is not a safe assumption - for example, perhaps foo
's init()
is what we're after. We should instead rewrite the import into a blank import (import _ "foo"
).
Perhaps it's worth using go/types
to check whether the imported package has an init()
at all. If it doesn't, is it safe to skip this step?
In any case, removing imports won't happen often and the tool is more useful when it's safe (useful) than when it's fast.