Documentation
¶
Overview ¶
Package exhaustive provides an analyzer that checks exhaustiveness of enum switch statements in Go code.
Definition of enum ¶
The Go language spec does not provide an explicit definition for enums. For the purpose of this analyzer, an enum type is a package-level named type whose underlying type is an integer (includes byte and rune), a float, or a string type. An enum type must have associated with it one or more package-level constants of the named type in the same package. These constants constitute the enum's members.
In the code snippet below, Biome is an enum type with 3 members.
type Biome int const ( Tundra Biome = 1 Savanna Biome = 2 Desert Biome = 3 )
Enum member values may also be specified using iota; they don't necessarily have to be explicit values, like in the snippet. Enum members don't necessarily have to all be defined in the same const block.
Definition of exhaustiveness ¶
An enum switch statement is exhaustive if all of the enum's members are listed in the switch statement's cases.
For an enum type defined in the same package as the switch statement, both exported and unexported enum members must be present in order to consider the switch statement exhaustive. For an enum type defined in an external package, it is sufficient for just the exported enum members to be present in order to consider the switch statement exhaustive.
Notable flags ¶
The notable flags used by the analyzer are:
-default-signifies-exhaustive
If enabled, the presence of a "default" case in switch statements satisfies exhaustiveness, even if all enum members are not listed.
-check-generated
If enabled, switch statements in generated Go source files are also checked. Otherwise switch statements in generated files are ignored by default.
-ignore-enum-members <regex>
Specifies a regular expression; enum members matching the regular expression are ignored. Ignored enum members don't have to be present in switch statements to satisfy exhaustiveness. The regular expression is matched against enum member names inclusive of the enum package import path, e.g. "github.com/foo/bar.Tundra", where the enum package import path is "github.com/foo/bar" and the enum member name is "Tundra".
Skipping analysis ¶
If the following comment:
//exhaustive:ignore
is associated with a switch statement, the analyzer skips inspection of the switch statement and no diagnostics are reported. Note the lack of whitespace between the comment marker ("//") and the comment text.
Additionally, no diagnostics are reported for switch statements in generated files unless the -check-generated flag is enabled. See https://golang.org/s/generatedcode for the definition of generated file.
Additionally, see the -ignore-enum-members flag, which can be used to ignore specific enum members.
Index ¶
Constants ¶
const ( DefaultSignifiesExhaustiveFlag = "default-signifies-exhaustive" CheckGeneratedFlag = "check-generated" IgnoreEnumMembersFlag = "ignore-enum-members" IgnorePatternFlag = "ignore-pattern" // Deprecated: see IgnoreEnumMembersFlag instead. CheckingStrategyFlag = "checking-strategy" // Deprecated. )
Flag names used by the analyzer. They are exported for use by analyzer driver programs.
const IgnoreDirectivePrefix = "//exhaustive:ignore"
IgnoreDirectivePrefix is used to exclude checking of specific switch statements. See package comment for details.
Variables ¶
Functions ¶
This section is empty.
Types ¶
This section is empty.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
cmd
|
|
exhaustive
Command exhaustive checks exhaustiveness of enum switch statements.
|
Command exhaustive checks exhaustiveness of enum switch statements. |