| Summary: | [1.5] switch + enum needs default case | ||
|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | David Barri <japgolly> |
| Component: | Core | Assignee: | JDT-Core-Inbox <jdt-core-inbox> |
| Status: | RESOLVED INVALID | QA Contact: | |
| Severity: | normal | ||
| Priority: | P3 | ||
| Version: | 3.1 | ||
| Target Milestone: | --- | ||
| Hardware: | PC | ||
| OS: | Windows XP | ||
| Whiteboard: | |||
Compiled:
public class X {
public enum TextUnitString {
unitcount, unit, units, x_units,
}
public String getString(TextUnitString type) {
switch (type) {
case unit :
return "word.unit";
case units :
return "word.units";
case x_units :
return "word.x_units";
case unitcount :
return "word.unitcount";
}
}
}
with javac and got error:
X.java:17: missing return statement
}
^
1 error
Please reopen if you still disagree.
Confirmed. I don't know why javac didn't give any errors yesterday. I guess I must've forgotten to save the file after I removed the default line. Sorry + thanks. No problem. We could at least warn when finding a non necessary default case, and already have a defect for this. |
public enum TextUnitString { unitcount, unit, units, x_units, } public String getString(TextUnitString type) { switch (type) { case unit: return res.getString("word.unit"); case units: return res.getString("word.units"); case x_units: return res.getString("word.x_units"); case unitcount: return res.getString("word.unitcount"); } } ---- The above code compiles perfectly with javac but in eclipse it gives the error: "This method must return a result of type String". A workaround is to add the following line to the switch case. default: return null;