blob: cdd24e958d9f115773f2de131d69342b7d459485 [file] [log] [blame]
Alan Donovanaac7fb62023-07-03 18:26:41 -04001// Copyright 2023 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5/*
6The deadcode command reports unreachable functions in Go programs.
7
8Usage: deadcode [flags] package...
9
10The deadcode command loads a Go program from source then uses Rapid
11Type Analysis (RTA) to build a call graph of all the functions
12reachable from the program's main function. Any functions that are not
13reachable are reported as dead code, grouped by package.
14
15Packages are expressed in the notation of 'go list' (or other
16underlying build system if you are using an alternative
17golang.org/x/go/packages driver). Only executable (main) packages are
18considered starting points for the analysis.
19
20The -test flag causes it to analyze test executables too. Tests
21sometimes make use of functions that would otherwise appear to be dead
22code, and public API functions reported as dead with -test indicate
23possible gaps in your test coverage. Bear in mind that an Example test
24function without an "Output:" comment is merely documentation:
25it is dead code, and does not contribute coverage.
26
27The -filter flag restricts results to packages that match the provided
28regular expression; its default value is the module name of the first
29package. Use -filter= to display all results.
30
31Example: show all dead code within the gopls module:
32
33 $ deadcode -test golang.org/x/tools/gopls/...
34
35The analysis can soundly analyze dynamic calls though func values,
36interface methods, and reflection. However, it does not currently
37understand the aliasing created by //go:linkname directives, so it
38will fail to recognize that calls to a linkname-annotated function
39with no body in fact dispatch to the function named in the annotation.
40This may result in the latter function being spuriously reported as dead.
41
42In any case, just because a function is reported as dead does not mean
43it is unconditionally safe to delete it. For example, a dead function
44may be referenced (by another dead function), and a dead method may be
45required to satisfy an interface (that is never called).
46Some judgement is required.
47
48The analysis is valid only for a single GOOS/GOARCH/-tags configuration,
49so a function reported as dead may be live in a different configuration.
50Consider running the tool once for each configuration of interest.
51Use the -line flag to emit a line-oriented output that makes it
52easier to compute the intersection of results across all runs.
53
54THIS TOOL IS EXPERIMENTAL and its interface may change.
55At some point it may be published at cmd/deadcode.
56In the meantime, please give us feedback at github.com/golang/go/issues.
57*/
58package main