exhaustive

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2021 License: BSD-2-Clause Imports: 12 Imported by: 14

README

exhaustive Godoc

The exhaustive package and the related command line program (found in cmd/exhaustive) can be used to check exhaustiveness of enum switch statements in Go code.

Install the command:

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

For documentation, see the package comment at pkg.go.dev. It describes the flags, the definition of enum, and the definition of exhaustiveness used by this package.

For the changelog, see CHANGELOG in the wiki.

The package provides an Analyzer that follows the guidelines in the go/analysis package; this should make it possible to integrate with external analysis driver programs.

Known issues

The analyzer's behavior is undefined for enum types that are type aliases. See issue #13.

Example

Given the enum

package token

type Token int

const (
	Add Token = iota
	Subtract
	Multiply
	Quotient
	Remainder
)

and the switch statement

package calc

import "token"

func processToken(t token.Token) {
	switch t {
	case token.Add: ...
	case token.Subtract: ...
	case token.Multiply: ...
	default: ...
	}
}

running exhaustive

exhaustive ./calc/...

will print

calc.go:6:2: missing cases in switch of type token.Token: Quotient, Remainder

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

View Source
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.

View Source
const IgnoreDirectivePrefix = "//exhaustive:ignore"

IgnoreDirectivePrefix is used to exclude checking of specific switch statements. See package comment for details.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:      "exhaustive",
	Doc:       "check exhaustiveness of enum switch statements",
	Run:       run,
	Requires:  []*analysis.Analyzer{inspect.Analyzer},
	FactTypes: []analysis.Fact{&enumsFact{}},
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd
exhaustive
Command exhaustive checks exhaustiveness of enum switch statements.
Command exhaustive checks exhaustiveness of enum switch statements.

Jump to

Keyboard shortcuts

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