Bug 207915 - [1.5][compiler] Switch on enum requires default case even if all enum entries are covered
Summary: [1.5][compiler] Switch on enum requires default case even if all enum entries...
Status: VERIFIED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.4 M3   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-10-30 03:19 EDT by Max Gilead CLA
Modified: 2007-10-30 07:17 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Max Gilead CLA 2007-10-30 03:19:22 EDT
Build ID: I20070921-0919 (3.4M2)

Switch on enum requires default case even if all enum entries are covered. I believe this is too strict check. There's already detector for this kind of errors ('Enum type constant not covered on `switch`') which could be used (in the background) in this case to check if all enum constants are covered in switch statement.

public class EnumTest {
  enum MyEnum { A, B }
  final String test;
  public EnumTest(MyEnum e) { // error: The blank final field test may not have been initialized
    switch (e) {
      case A: test = "a"; break;
      case B: test = "a"; break;
//      default: test = "unknown"; // enabling this line fixes above error
    }
  }
}
Comment 1 Philipe Mulet CLA 2007-10-30 07:00:48 EDT
We are strictly following the language spec here. The extra warning you refer to is a courtesy of the Eclipse compiler.

As far as I remember, the spec argument for adding the default is for binary compatibility reasons. If later on you add a new enum constant, the already compiled code is still safe, and will run through the default case. 

Comment 2 Philipe Mulet CLA 2007-10-30 07:05:09 EDT
Added EnumTest#test146
Comment 3 Jerome Lanneluc CLA 2007-10-30 07:17:04 EDT
Verified for 3.4M3 using I20071029-0010