Skip to content

Commit baeb0da

Browse files
xieyuschengopherbot
authored andcommitted
cmd/deadcode: respect unused symbols inside all loaded modules
Current deadcode uses the first package module path as a regular expression to filter when check deadcode code for packages. However, when users load multiple modules, the filter will respect one of them only. This CL constructs the filter regex based on all loaded modules rather than use the first privileging the first package. Fixes golang/go#73652 Change-Id: Id7b9eb8274141cd2d6362da01366cbc45c87eebc Reviewed-on: https://go-review.googlesource.com/c/tools/+/671916 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent fdae66b commit baeb0da

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

cmd/deadcode/deadcode.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,17 @@ func main() {
132132

133133
// If -filter is unset, use first module (if available).
134134
if *filterFlag == "<module>" {
135-
if mod := initial[0].Module; mod != nil && mod.Path != "" {
136-
*filterFlag = "^" + regexp.QuoteMeta(mod.Path) + "\\b"
135+
seen := make(map[string]bool)
136+
var patterns []string
137+
for _, pkg := range initial {
138+
if pkg.Module != nil && pkg.Module.Path != "" && !seen[pkg.Module.Path] {
139+
seen[pkg.Module.Path] = true
140+
patterns = append(patterns, regexp.QuoteMeta(pkg.Module.Path))
141+
}
142+
}
143+
144+
if patterns != nil {
145+
*filterFlag = "^(" + strings.Join(patterns, "|") + ")\\b"
137146
} else {
138147
*filterFlag = "" // match any
139148
}

cmd/deadcode/doc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/*
66
The deadcode command reports unreachable functions in Go programs.
77
8-
Usage: deadcode [flags] package...
8+
Usage: deadcode [flags] package...
99
1010
The deadcode command loads a Go program from source then uses Rapid
1111
Type Analysis (RTA) to build a call graph of all the functions
@@ -25,8 +25,8 @@ function without an "Output:" comment is merely documentation:
2525
it is dead code, and does not contribute coverage.
2626
2727
The -filter flag restricts results to packages that match the provided
28-
regular expression; its default value is the module name of the first
29-
package. Use -filter= to display all results.
28+
regular expression; its default value matches the listed packages and any other
29+
packages belonging to the same modules. Use -filter= to display all results.
3030
3131
Example: show all dead code within the gopls module:
3232
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Test deadcode usage under go.work.
2+
3+
deadcode ./svc/... ./lib/...
4+
want "unreachable func: A"
5+
6+
# different order of path under the same go.work should behave the same.
7+
8+
deadcode ./svc/... ./lib/...
9+
want "unreachable func: A"
10+
11+
12+
-- go.work --
13+
go 1.18
14+
15+
use (
16+
./lib
17+
./svc
18+
)
19+
20+
-- lib/go.mod --
21+
module lib.com
22+
23+
go 1.18
24+
25+
-- lib/a/a.go --
26+
package a
27+
28+
func A() {}
29+
30+
-- svc/go.mod --
31+
module svc.com
32+
33+
go 1.18
34+
35+
-- svc/s/main.go --
36+
package main
37+
38+
func main() { println("main") }
39+

0 commit comments

Comments
 (0)