Learning center

Does a finding matter if it sits in code your build never compiles?

It matters exactly when some configuration you ship compiles that file, so list the files each shipped target compiles before you dismiss it. A finding in a file that no shipped target compiles can be dismissed, with that list recorded as the reason.

Why the same tree gives different findings#

A compiled language decides per build which source files belong to a package. In Go that decision comes from build constraints. The go command documentation at go1.24.7 defines one as "a condition under which a file should be included in the package", and adds a second rule based on names: a file whose name ends in _GOOS, _GOARCH or both "is considered to have an implicit build constraint requiring those terms". The C equivalent is an #ifdef block or a platform file listed only in some build rules.

Analyzers that run through the build see one configuration at a time. go vet accepts the build flags "that control package resolution and execution", -tags among them (its usage line is go vet [build flags] [-vettool prog] [vet flags] [packages]), and it checks the files that the current GOOS, GOARCH and -tags select and nothing else. A finding can therefore appear for one person and not for another, from the same commit, and both runs are correct.

A finding that only one configuration sees#

The constructed module below has a portable file, a Linux watcher, a BSD and macOS watcher, and an old export helper behind a custom tag:

watch/watch.go          (no constraint)
watch/watch_linux.go    (implicit: linux)
watch/watch_bsd.go      //go:build darwin || freebsd
watch/export_legacy.go  //go:build legacy_export

export_legacy.go contains a format mismatch:

//go:build legacy_export

package watch

import "log"

// ExportLegacy writes the old CSV manifest. Kept for the 2023 migration.
func ExportLegacy(paths []string) {
	log.Printf("exporting %d paths", paths)
}

With Go 1.24.7 on a Linux host, the default vet run is clean, and the report appears only when the tag is set:

$ go vet ./...
$ echo $?
0
$ go vet -tags legacy_export ./...
# example.test/syncer/watch
watch/export_legacy.go:9:2: log.Printf format %d has arg paths of wrong type []string

The -tags flag, per the same documentation, is "a comma-separated list of additional build tags to consider satisfied during the build." The finding is real code with a real mistake. Whether it matters depends on whether any build you ship sets that tag.

List which configurations compile the file#

go list answers that without building anything. Its GoFiles field holds the files the current configuration compiles, and IgnoredGoFiles holds the ".go source files ignored due to build constraints":

$ go list -f '{{.ImportPath}} GoFiles={{.GoFiles}} IgnoredGoFiles={{.IgnoredGoFiles}}' ./watch
example.test/syncer/watch GoFiles=[watch.go watch_linux.go] IgnoredGoFiles=[export_legacy.go watch_bsd.go]

Two files sit outside the Linux build. The next fact is which configurations you ship. In this fixture they are in release.sh, which builds linux/amd64, linux/arm64 and darwin/arm64 with no -tags flag. Running go list once per shipped target gives the answer for each file:

$ for t in linux/amd64 linux/arm64 darwin/arm64; do
>   printf '%-13s ' $t; GOOS=${t%/*} GOARCH=${t#*/} go list -f '{{.GoFiles}}' ./watch
> done
linux/amd64   [watch.go watch_linux.go]
linux/arm64   [watch.go watch_linux.go]
darwin/arm64  [watch.go watch_bsd.go]

export_legacy.go is in none of them. A search of the build scripts and CI files for legacy_export or -tags returns nothing (grep exits 1). That is the recorded reason for dismissal: "no shipped target sets legacy_export; see the release matrix." Deleting the file is also a fair fix, since code that no build compiles keeps producing findings that no user can hit.

One case reverses this. If the package is a library, the people who import it choose their own tags and targets. A build tag in a library is part of what it offers, so a finding behind a tag your own builds never set can still reach a user's binary.

When a configuration you ship compiles it#

watch_bsd.go looked like the same situation from a Linux workstation: listed in IgnoredGoFiles, absent from the clean vet run. It contains a context whose cancel function is thrown away:

//go:build darwin || freebsd

// (package clause and imports trimmed)

// Start begins kqueue-based watching with a startup deadline.
func Start(parent context.Context, open func(context.Context) error) error {
	ctx, _ := context.WithTimeout(parent, 5*time.Second)
	return open(ctx)
}

The release matrix includes darwin/arm64, and the go list loop above shows that target compiling watch_bsd.go. Vet for that target, run from the same Linux host, reports it:

$ GOOS=darwin GOARCH=arm64 go vet ./...
# example.test/syncer/watch
watch/watch_bsd.go:12:7: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak

The Linux version of Start calls defer cancel(), so a Linux-only CI run would never have raised this. The macOS binary you publish contains it. This finding is a fix, and the gap it exposed is a CI gap: vet, like the compiler, needs to run once per shipped target.

The vet documentation in cmd/vet/doc.go is explicit that its checks are heuristics: "the tool does not check every possible problem and depends on unreliable heuristics". The configuration question comes before that one. A heuristic that never runs on a file cannot report anything about it.

The same check for C and C++#

For C, the question is which preprocessor branches each configuration compiles. This constructed function prints a byte count two ways:

void report(const char *name, long bytes)
{
#ifdef WITH_METRICS
  printf("%s: %d bytes\n", name, bytes);
#else
  printf("%s: %ld bytes\n", name, bytes);
#endif
}

GCC 13.3.0 with -Wall is silent by default and warns only when the define is set:

$ gcc -Wall -c report.c; echo $?
0
$ gcc -Wall -DWITH_METRICS -c report.c
report.c: In function 'report':
report.c:6:16: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long int' [-Wformat=]
...

The preprocessor shows which line each configuration hands to the compiler. GCC's invoke.texi for 13.3.0 describes -E as "Stop after the preprocessing stage; do not run the compiler proper." Run it with each shipped configuration's flags:

$ gcc -E -P report.c | grep 'bytes\\n'
  printf("%s: %ld bytes\n", name, bytes);
$ gcc -E -P -DWITH_METRICS report.c | grep 'bytes\\n'
  printf("%s: %d bytes\n", name, bytes);

Then check whether any shipped build passes -DWITH_METRICS, in the same way the Go example searched the release script for its tag. A branch that no shipped build selects is in the same position as export_legacy.go.

Decide from the shipped configurations#

What the finding saysEvidence to collectDecision
Finding in a file behind a custom tag or #ifdefgo list (or gcc -E with each configuration's flags) for every shipped target, and a search of build scripts for the tag or defineDismiss if no shipped configuration compiles it; record the matrix; consider deleting the code
Finding in a platform file (_darwin.go, _windows.go)Whether that platform is in your release matrixFix if you ship it; add an analyzer run for that target to CI
Clean analyzer run on one hostWhich configuration that run usedTreat it as clean for that configuration only
Finding behind a tag in a library others importWhether the tag is documented or used by importersFix it; importers choose their own tags
Finding appears only with every tag forced onWhether that combination of tags can be built at allCheck the combination with go list -tags; dismiss only combinations nothing builds

For a finding about code that is compiled but may never be called, the guide to dead-code findings and dynamic use covers the next question. When a clean scan may have skipped files for other reasons, see the guide to clean scans that skipped files.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading