| 1 | // Copyright 2024 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 | // Package unusedfunc defines an analyzer that checks for unused |
| 6 | // functions and methods |
| 7 | // |
| 8 | // # Analyzer unusedfunc |
| 9 | // |
| 10 | // unusedfunc: check for unused functions, methods, etc |
| 11 | // |
| 12 | // The unusedfunc analyzer reports functions and methods that are |
| 13 | // never referenced outside of their own declaration. |
| 14 | // |
| 15 | // A function is considered unused if it is unexported and not |
| 16 | // referenced (except within its own declaration). |
| 17 | // |
| 18 | // A method is considered unused if it is unexported, not referenced |
| 19 | // (except within its own declaration), and its name does not match |
| 20 | // that of any method of an interface type declared within the same |
| 21 | // package. |
| 22 | // |
| 23 | // The tool may report false positives in some situations, for |
| 24 | // example: |
| 25 | // |
| 26 | // - for a declaration of an unexported function that is referenced |
| 27 | // from another package using the go:linkname mechanism, if the |
| 28 | // declaration's doc comment does not also have a go:linkname |
| 29 | // comment. |
| 30 | // |
| 31 | // (Such code is in any case strongly discouraged: linkname |
| 32 | // annotations, if they must be used at all, should be used on both |
| 33 | // the declaration and the alias.) |
| 34 | // |
| 35 | // - for compiler intrinsics in the "runtime" package that, though |
| 36 | // never referenced, are known to the compiler and are called |
| 37 | // indirectly by compiled object code. |
| 38 | // |
| 39 | // - for functions called only from assembly. |
| 40 | // |
| 41 | // - for functions called only from files whose build tags are not |
| 42 | // selected in the current build configuration. |
| 43 | // |
| 44 | // Since these situations are relatively common in the low-level parts |
| 45 | // of the runtime, this analyzer ignores the standard library. |
| 46 | // See https://go.dev/issue/71686 and https://go.dev/issue/74130 for |
| 47 | // further discussion of these limitations. |
| 48 | // |
| 49 | // The unusedfunc algorithm is not as precise as the |
| 50 | // golang.org/x/tools/cmd/deadcode tool, but it has the advantage that |
| 51 | // it runs within the modular analysis framework, enabling near |
| 52 | // real-time feedback within gopls. |
| 53 | // |
| 54 | // The unusedfunc analyzer also reports unused types, vars, and |
| 55 | // constants. Enums--constants defined with iota--are ignored since |
| 56 | // even the unused values must remain present to preserve the logical |
| 57 | // ordering. |
| 58 | package unusedfunc |