Bug 244133

Summary: [1.5][compiler] false positive for "missing return result" when switching enum
Product: [Eclipse Project] JDT Reporter: Görge Albrecht <eclipse>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: Olivier_Thomann
Version: 3.4   
Target Milestone: 3.5 M2   
Hardware: PC   
OS: Windows 2000   
Whiteboard:

Description Görge Albrecht CLA 2008-08-14 05:32:05 EDT
The following code shouldn't produce a compiler error "This method must return a result of type String":

 1: public class SwitchTest {
 2:   enum ABC {A, B, C; }
 3:
 4: String switchABC(ABC abc) {
 5:    switch (abc) {
 6:      case A:
 7:        return "A";
 8:      case B:
 9:        return "B";
10:      case C:
11:        return "C";
12:    }
13:  }
14: }

Reason:
In method switchABC all possible cases are covered.

Workaround:
Introduce line 
12a: return ""
but this line can never be reached.
Comment 1 Olivier Thomann CLA 2008-08-14 09:44:47 EDT
This code doesn't compile with javac 1.5, 1.6 and 1.7.
An extra return statement is required.
Closing as INVALID.
Comment 2 Görge Albrecht CLA 2008-08-18 03:29:04 EDT
Sorry. Of course you're right. I should have made the check myself.

Interestingly enough, the following method doesn't need an extra return at the end. (BTW, the default statement isn't reachable either. But this is another issue ;-)

 1: public class SwitchTest {
 2:   enum ABC {A, B, C; }
 3:
 4: String switchABC(ABC abc) {
 5:    switch (abc) {
 6:      case A:
 7:        return "A";
 8:      case B:
 9:        return "B";
10:      case C:
11:        return "C";
12:      default:
13:        throw new RuntimeException("not reachable");
14:    }
15: }

I'll check the cases against the JLS and file a bug/enhancement req against javac...