Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 97790

Summary: [1.5] switch + enum needs default case
Product: [Eclipse Project] JDT Reporter: David Barri <japgolly>
Component: CoreAssignee: 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:

Description David Barri CLA 2005-06-01 02:30:48 EDT
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;
Comment 1 Philipe Mulet CLA 2005-06-01 12:45:34 EDT
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. 
Comment 2 David Barri CLA 2005-06-01 21:33:26 EDT
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.
Comment 3 Philipe Mulet CLA 2005-06-02 03:18:23 EDT
No problem.
We could at least warn when finding a non necessary default case, and already
have a defect for this.