Find file History
dominikh cmd/megacheck: allow per-tool control of non-zero exiting
When running tools individually, their non-zero exit can be suppressed,
for example by using '|| true' in a shell script. When using megacheck,
all tools were sharing the same exit code. This is a problem when using
gosimple in CI because it usually shouldn't cause CI to fail. The old
behaviour made it impossible to run gosimple, together with other tools,
via megacheck.

Add '<tool>.exit-non-zero' flags for each tool, to control whether
problems found by said tool result in megacheck exiting non-zero.

Default to false for gosimple.

Closes gh-219
Latest commit 15d446f Nov 8, 2017
Failed to load latest commit information. Add top-level README Jan 24, 2017
main.go cmd/megacheck: allow per-tool control of non-zero exiting Nov 10, 2017


unused checks Go code for unused constants, variables, functions and types.


go get


unused -help

Usage Tips

  • When running unused on multiple packages, it will first try to check them all at once, because that's faster. If any of the packages don't compile, however, unused will check each package individually.

    The first step can, depending on the number of packages, use a lot of memory. For the entire standard library, it uses roughly 800 MB. For a GOPATH with thousands of packages, it can quickly use several gigabytes. If that is an issue, consider using something like this instead:

    for pkg in $(go list your_selection); do unused "$pkg"; done

    This will effectively skip the first step and always check every package individually.

What counts as used/unused?

unused checks for unused constants, functions, types and optionally struct fields. They will be considered used or unused under the following conditions:

  • Unexported package-level objects will be reported as unused if there are no explicit references to them.

  • Unexported methods will be reported as unused if there are no explicit references to them and if they don't implement any interfaces.

  • The main function is considered as used if it's in the main package.

  • init functions are always considered as used.

  • Exported objects in function scope are treated like unexported objects.

  • Exported functions in tests are treated like unexported functions, unless they're test, benchmark or example functions.

  • Struct fields will be considered as unused if there are no explicit references to them. Unkeyed composite literals with >=1 elements mark all fields of the struct as used.

  • Neither the checks for methods nor for struct fields are aware of the reflect package and may thus produce false positives.

Whole program analysis

Optionally via the -exported flag, unused can analyse all arguments as a single program and report unused exported identifiers. This can be useful for checking "internal" packages, or large software projects that do not export an API to the public, but use exported methods between components.

Do note that in the whole-program analysis, all arguments must type-check. It is not possible to check packages individually in this mode.


$ time unused cmd/go
/usr/lib/go/src/cmd/go/build.go:1327:6: func hasString is unused
/usr/lib/go/src/cmd/go/build.go:2328:6: func toolVerify is unused
/usr/lib/go/src/cmd/go/generate.go:375:21: func identLength is unused
/usr/lib/go/src/cmd/go/get.go:474:5: var goTag is unused
/usr/lib/go/src/cmd/go/get.go:513:6: func cmpGoVersion is unused
/usr/lib/go/src/cmd/go/go_test.go:426:23: func grepCountStdout is unused
/usr/lib/go/src/cmd/go/go_test.go:432:23: func grepCountStderr is unused
/usr/lib/go/src/cmd/go/main.go:406:5: var logf is unused
/usr/lib/go/src/cmd/go/main.go:431:6: func runOut is unused
/usr/lib/go/src/cmd/go/pkg.go:91:2: field forceBuild is unused
/usr/lib/go/src/cmd/go/pkg.go:688:2: const toRoot is unused
/usr/lib/go/src/cmd/go/testflag.go:278:6: func setIntFlag is unused
unused cmd/go  3.33s user 0.25s system 447% cpu 0.799 total
$ time unused $(go list | grep -v /vendor/)
/home/dominikh/prj/src/ var noop is unused
/home/dominikh/prj/src/ const interval is unused
/home/dominikh/prj/src/ field m is unused
/home/dominikh/prj/src/ const nerveNodePrefix is unused
/home/dominikh/prj/src/ const serversetNodePrefix is unused
/home/dominikh/prj/src/ const ingestedSamplesCap is unused
/home/dominikh/prj/src/ var errSkippedScrape is unused
/home/dominikh/prj/src/ field providers is unused
/home/dominikh/prj/src/ field error is unused
/home/dominikh/prj/src/ field error is unused
/home/dominikh/prj/src/ field error is unused
/home/dominikh/prj/src/ field error is unused
/home/dominikh/prj/src/ var illegalCharsRE is unused
/home/dominikh/prj/src/ field child is unused
/home/dominikh/prj/src/ field zkEvents is unused
unused $(go list | grep -v /vendor/)  5.70s user 0.43s system 535% cpu 1.142 total
$ time unused -exported
/home/dominikh/prj/src/ const limit is unused
/home/dominikh/prj/src/ func tryDeepEqual is unused
/home/dominikh/prj/src/ func Errorf is unused
/home/dominikh/prj/src/ func Fprintf is unused
/home/dominikh/prj/src/ func Log is unused
/home/dominikh/prj/src/ func Logf is unused
/home/dominikh/prj/src/ func Logln is unused
/home/dominikh/prj/src/ func Print is unused
/home/dominikh/prj/src/ func Printf is unused
/home/dominikh/prj/src/ func Println is unused
/home/dominikh/prj/src/ func Sprintf is unused
/home/dominikh/prj/src/ func wrap is unused
unused -exported  1.23s user 0.19s system 253% cpu 0.558 total