Descriptioncmd/vet: add an "unused" module
The algorithm is straightforward although some corner cases are tricky.
We use the go/types Object map to iterate over the identifiers. A used
identifier will appear at least twice: once at the declaration, once at the use.
An unused identifier will appear only once: at its declaration. Thus we
reference count the identifiers and those with only one appearance
are errors. Moreover, by luck, that one instance is the correct place
to flag the error.
Tricky cases:
- implicit declarations such as in import clauses and type switches.
- handled through the Implicits map.
- methods used only through interfaces
- done by cross-checking methods and interfaces
- fields in nonce structs (these appear often in tests)
- not handled yet
The nonce struct case is unsolved yet because the right heuristic is not
clear.
This check is enabled by default. It's a little noisy, especially inside tests,
but it's already found lots of cruft in the standard library so I think it's
worth turning on. Heuristics will improve to reduce noise.
Fixes issue 4827.
Patch Set 1 #
Total comments: 3
Patch Set 2 : diff -r 2d447f803f2f https://code.google.com/p/go.tools #
Total comments: 30
Patch Set 3 : diff -r 6698ca2900e2 https://code.google.com/p/go.tools #
Total comments: 4
Patch Set 4 : diff -r 400755f06d55 https://code.google.com/p/go.tools #
Total comments: 3
MessagesTotal messages: 15
|