Skip to content

x/tools/cmd/deadcode: add new command #63501

@adonovan

Description

@adonovan

The golang.org/x/tools/internal/cmd/deadcode command reports dead code, that is, functions not reachable by a sequence of calls from main, even through reflection, based on an RTA-based call graph. We have found it useful for identifying orphaned bits of code in x/tools and we imagine others might find it useful too. We propose to publish it at cmd/deadcode in the same repo.

This is an example of the tool in action, showing functions in x/tools not reachable from gopls' main or any of its tests, and suppressing functions in generated source files.

xtools$ deadcode -test ./gopls/...
golang.org/x/tools/gopls/internal/lsp
	openClientEditor

golang.org/x/tools/gopls/internal/lsp/cache
	(fileLoadScope).aScope
	(packageLoadScope).aScope
	(moduleLoadScope).aScope
	(viewLoadScope).aScope

golang.org/x/tools/gopls/internal/lsp/debug/log
	(Level).Log
	(Level).Logf

golang.org/x/tools/gopls/internal/lsp/filecache
	SetBudget

golang.org/x/tools/gopls/internal/lsp/regtest
	(State).outstandingWork

golang.org/x/tools/gopls/internal/lsp/source
	WidestPackageForFile

golang.org/x/tools/gopls/internal/lsp/template
	(*Parsed).WriteNode
	(wrNode).writeNode

xtools$ deadcode -test -json ./gopls/...
[
	{
		"Path": "golang.org/x/tools/gopls/internal/lsp",
		"Funcs": [
			{
				"Name": "golang.org/x/tools/gopls/internal/lsp.openClientEditor",
				"RelName": "openClientEditor",
				"Posn": "/Users/adonovan/w/xtools/gopls/internal/lsp/command.go:1178:6",
				"Generated": false
			}
		]
	},
...

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions