unused: fix handling of packages in whole program mode
introduced an incorrect fix for
whole program mode. The fix was incorrect because it assumed that all
packages were relevant. However, even in whole program mode, we do not
care about dependencies that aren't being explicitly checked.
@@ -833,6 +833,18 @@ func isIrrelevant(obj interface{}) bool {
return false
}
+func (g *Graph) isInterestingPackage(pkg *types.Package) bool {
+ if g.wholeProgram {
+ for _, opkg := range g.job.Program.InitialPackages {
+ if opkg.Types == pkg {
+ return true
+ }
+ }
+ return false
+ }
+ return pkg == g.pkg.Pkg
+}
+
func (g *Graph) see(obj interface{}) {
if isIrrelevant(obj) {
return
@@ -840,7 +852,7 @@ func (g *Graph) see(obj interface{}) {
assert(obj != nil)
if obj, ok := obj.(types.Object); ok && obj.Pkg() != nil {
- if obj.Pkg() != g.pkg.Pkg {
+ if !g.isInterestingPackage(obj.Pkg()) {
return
}
}
@@ -861,18 +873,16 @@ func (g *Graph) use(used, by interface{}, reason string) {
if _, ok := by.(*types.Func); ok {
assert(g.pkg.Prog.FuncValue(by.(*types.Func)) == nil)
}
- if !g.wholeProgram {
if obj, ok := used.(types.Object); ok && obj.Pkg() != nil {
- if obj.Pkg() != g.pkg.Pkg {
+ if !g.isInterestingPackage(obj.Pkg()) {
return
}
}
if obj, ok := by.(types.Object); ok && obj.Pkg() != nil {
- if obj.Pkg() != g.pkg.Pkg {
+ if !g.isInterestingPackage(obj.Pkg()) {
return
}
}
- }
usedNode, new := g.node(used)
assert(!new)
if by == nil {
