exhaustive

package module
v0.18.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: BSD-2-Clause, BSD-3-Clause Imports: 1 Imported by: 26 modules, 21 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

The exhaustive static analysis checks that switch statements in Go source code are exhaustive. The analysis can check expression switches, if the switch expression type is an enumerated type, and type switches.

The complete analysis is implemented in multiple passes; see the packages under the 'passes' directory of the module. Package enumerated finds declarations of enumerated types and enumerated constants. Package exhaustive scans the syntax tree of Go packages for switch statements and checks that the switch statements are exhaustive.

Documentation

The analysis is documented in package comments. The documentation can be read using the 'go doc' command. For example:

go doc github.com/nishanths/exhaustive/passes/exhaustive

The documentation is also available on pkg.go.dev.

https://pkg.go.dev/github.com/nishanths/exhaustive/passes/enumerated

https://pkg.go.dev/github.com/nishanths/exhaustive/passes/exhaustive

https://pkg.go.dev/github.com/nishanths/exhaustive/cmd/exhaustive

Usage

The exhaustive command runs the exhaustive static analysis on the specified packages and reports diagnostics. The command can be installed using 'go install'.

go install github.com/nishanths/exhaustive/cmd/exhaustive@latest

Usage:

exhaustive [flags] [packages]

For more information about specifying packages, see 'go help packages'. For more information about flags, run 'exhaustive -h'. The flags are described in greater detail in the documentation for package enumerated and package exhaustive.

The exhaustive command can be used with the -vettool flag of 'go vet'. See 'go help vet' for more information.

go install github.com/nishanths/exhaustive/cmd/exhaustive@latest
go vet -vettool=$(which exhaustive)

The packages in the module can also be imported and used from external analysis driver programs. See https://golang.org/x/tools/go/analysis for details.

Examples

Given the expression switch

package a

type vcs int

const (
	bazaar vcs = iota
	fossil
	git
	mercurial
	subversion
	darcs
)

func f(v vcs) {
	switch v {
	case bazaar:
	case fossil:
	case git:
	case mercurial:
	case subversion:
	}
}

the analysis reports the following diagnostic

$ exhaustive
a.go:15:2: missing cases in expression switch: darcs
$

Though it is not so in the above example, in general the enumerated type declarations and the switch statement can be in different packages.

Given the type switch

package b

import "go/types"

func f(t types.Type) bool {
	switch t.(type) {
	case *types.Array,
		*types.Chan,
		*types.Interface,
		*types.Map,
		*types.Pointer,
		*types.Signature,
		*types.Slice,
		*types.Struct,
		*types.Tuple,
		*types.Union:
		return false
	case *types.Alias, *types.Named:
		return true
	default:
		panic("internal error: unhandled type")
	}
}

the diagnostic (as of go1.27) is

$ exhaustive -check=typeswitch
b.go:6:2: missing cases in type switch: *types.Basic, *types.TypeParam
$

Documentation

Overview

Deprecated: This package moved to github.com/nishanths/exhaustive/passes/exhaustive. New code should use the package at that import path.

Index

Constants

This section is empty.

Variables

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd
exhaustive command
The exhaustive command runs the exhaustive static analysis on the specified packages and reports diagnostics.
The exhaustive command runs the exhaustive static analysis on the specified packages and reports diagnostics.
internal
passes
enumerated
Package enumerated defines a static analysis that finds declarations of enumerated types and enumerated constants.
Package enumerated defines a static analysis that finds declarations of enumerated types and enumerated constants.
exhaustive
Package exhaustive defines a static analysis that checks that switch statements in Go source code are exhaustive.
Package exhaustive defines a static analysis that checks that switch statements in Go source code are exhaustive.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL