@@ -5,75 +5,85 @@ switch statements in Go code.
5
5
Definition of enum
6
6
7
7
The Go language spec does not provide an explicit definition for enums. For the
8
- purpose of this analyzer, an enum type is a package-level named type whose
8
+ purpose of this analyzer, an enum type is a named (defined) type whose
9
9
underlying type is an integer (includes byte and rune), a float, or a string
10
- type. An enum type must have associated with it one or more package-level
11
- constants of the named type in the same package. These constants constitute the
12
- enum's members.
10
+ type. An enum type must have associated with it one or more constants of the
11
+ named type. These constants constitute the enum's members.
13
12
14
- In the code snippet below, Biome is an enum type with 3 members.
13
+ In the example below, Biome is an enum type with 3 members.
15
14
16
- type Biome int
15
+ type Biome int
17
16
18
- const (
19
- Tundra Biome = 1
20
- Savanna Biome = 2
21
- Desert Biome = 3
22
- )
17
+ const (
18
+ Tundra Biome = 1
19
+ Savanna Biome = 2
20
+ Desert Biome = 3
21
+ )
23
22
24
- Enum member values may also be specified using iota; they don't necessarily have
25
- to be explicit values, like in the snippet. Enum members don't necessarily have
26
- to all be defined in the same const block.
23
+ For a constant to be an enum member, it must be declared in the same scope as
24
+ the enum type. That said, enum member constants don't necessarily have to all be
25
+ declared in the same const block. Enum member constant values may be specified
26
+ using iota or using explicit values (like in the example).
27
+
28
+ The analyzer's behavior is undefined for type aliases. This may change in the
29
+ future.
27
30
28
31
Definition of exhaustiveness
29
32
30
33
An enum switch statement is exhaustive if all of the enum's members are listed
31
34
in the switch statement's cases.
32
35
33
36
For an enum type defined in the same package as the switch statement, both
34
- exported and unexported enum members must be present in order to consider the
35
- switch statement exhaustive. For an enum type defined in an external package, it
36
- is sufficient for just the exported enum members to be present in order to
37
- consider the switch statement exhaustive.
38
-
39
- Notable flags
40
-
41
- The notable flags used by the analyzer are:
42
-
43
- -default-signifies-exhaustive
44
-
45
- If enabled, the presence of a "default" case in switch statements satisfies
46
- exhaustiveness, even if all enum members are not listed.
47
-
48
- -check-generated
49
-
50
- If enabled, switch statements in generated Go source files are also checked.
51
- Otherwise switch statements in generated files are ignored by default.
52
-
53
- -ignore-enum-members <regex>
54
-
55
- Specifies a regular expression; enum members matching the regular expression are
56
- ignored. Ignored enum members don't have to be present in switch statements to
57
- satisfy exhaustiveness. The regular expression is matched against enum member
58
- names inclusive of the enum package import path, e.g.
59
- "github.com/foo/bar.Tundra", where the enum package import path is
60
- "github.com/foo/bar" and the enum member name is "Tundra".
37
+ exported and unexported enum members must be listed to satisfy exhaustiveness.
38
+ For an enum type defined in an external package, it is sufficient that only the
39
+ exported enum members be listed to satisfy exhaustiveness.
40
+
41
+ Flags
42
+
43
+ The notable flags used by the analyzer are below. All of these flags are
44
+ optional.
45
+
46
+ Flag name Type Default value
47
+ -check-generated bool false
48
+ -default-signifies-exhaustive bool false
49
+ -ignore-enum-members string (none)
50
+ -package-scope-only bool false
51
+
52
+ If the -check-generated flag is enabled, switch statements in generated Go
53
+ source files are also checked. Otherwise switch statements in generated files
54
+ are ignored by default.
55
+
56
+ If the default-signifies-exhaustive flag is enabled, the presence of a "default"
57
+ case in switch statements satisfies exhaustiveness, even if all enum members are
58
+ not listed. It is recommended that you do not enable this flag; enabling it
59
+ generally defeats the purpose of exhaustiveness checking.
60
+
61
+ The -ignore-enum-members flag specifies a regular expression, in the syntax
62
+ accepted by the Go regexp package. Enum members matching the regular expression
63
+ are ignored, i.e. these enum member names don't have to be listed in switch
64
+ statements to satisfy exhaustiveness. The specified regular expression is
65
+ matched against an enum member name inclusive of the enum package import path:
66
+ for example, "example.com/pkg.Tundra" where where the import path is
67
+ "example.com/pkg" and the enum member name is "Tundra".
68
+
69
+ If the -package-scope-only flag is enabled, the analyzer only finds enums
70
+ defined in package scope, and consequently only switch statements that switch
71
+ on package-scoped enums will be checked for exhaustiveness. By default, the
72
+ analyzer finds enums defined in all scopes, and checks switch statements that
73
+ switch on all these enums.
61
74
62
75
Skipping analysis
63
76
64
- If the following comment:
65
-
66
- //exhaustive:ignore
67
-
68
- is associated with a switch statement, the analyzer skips inspection of the
69
- switch statement and no diagnostics are reported. Note the lack of whitespace
77
+ To skip analysis of a specific switch statement, associate the following
78
+ comment with the switch statement. Note the lack of whitespace
70
79
between the comment marker ("//") and the comment text.
71
80
72
- Additionally, no diagnostics are reported for switch statements in generated
73
- files unless the -check-generated flag is enabled. See
74
- https://golang.org/s/generatedcode for the definition of generated file .
81
+ //exhaustive:ignore
82
+
83
+ To ignore specific enum members, see the -ignore-enum-members flag .
75
84
76
- Additionally, see the -ignore-enum-members flag, which can be used
77
- to ignore specific enum members.
85
+ By default, the analyzer skips analysis of switch statements in generated
86
+ Go source files. Use the -check-generated flag to change this behavior.
87
+ See https://golang.org/s/generatedcode for the definition of generated file.
78
88
*/
79
89
package exhaustive
0 commit comments