Bug 244133 - [1.5][compiler] false positive for "missing return result" when switching enum
Summary: [1.5][compiler] false positive for "missing return result" when switching enum
Status: CLOSED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: 3.5 M2   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-08-14 05:32 EDT by Görge Albrecht CLA
Modified: 2008-08-18 03:29 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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...