-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathMissingEnumInSwitch.qhelp
More file actions
47 lines (34 loc) · 1.66 KB
/
MissingEnumInSwitch.qhelp
File metadata and controls
47 lines (34 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>A <code>switch</code> statement that is based on a variable with an <code>enum</code> type should either have a default case
or handle all possible constants of that <code>enum</code> type. Handling all but one or two
<code>enum</code> constants is usually a coding mistake.</p>
</overview>
<recommendation>
<p>If there are only a handful of missing cases, add them to the end of the
<code>switch</code> statement. If there are many cases that do not need to be handled individually,
add a default case to handle them.</p>
<p>If there are some <code>enum</code> constants that should never occur in this particular part of
the code, then program defensively by adding cases for those constants and explicitly throwing an
exception (rather than just having no cases for those constants).</p>
</recommendation>
<example>
<p>In the following example, the case for 'YES' is missing. Therefore, if <code>answer</code> is
'YES', an exception is thrown at run time. To fix this, a case for 'YES' should be added.</p>
<sample src="MissingEnumInSwitch.java" />
</example>
<references>
<li>
Help - Eclipse Platform:
<a href="https://help.eclipse.org/2020-12/advanced/content.jsp?topic=/org.eclipse.jdt.doc.user/reference/preferences/java/compiler/ref-preferences-errors-warnings.htm">Java Compiler Errors/Warnings Preferences</a>.
</li>
<li>
Java Language Specification:
<a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-8.html#jls-8.9">8.9 Enum Types</a>,
<a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-14.html#jls-14.11">14.11 The switch Statement</a>.
</li>
</references>
</qhelp>