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

Bug 132917

Summary: "Enum type constant not covered on 'switch'" check allows default to cover all cases
Product: [Eclipse Project] JDT Reporter: Jason Bennett <jasonab>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: VERIFIED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: jarthana, mail, stephan.herrmann
Version: 3.2   
Target Milestone: 4.3 M5   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Jason Bennett CLA 2006-03-22 19:17:45 EST
I've tried to convert some of my code that uses IF statements with enums to switch statements, in order to have Eclipse make sure I didn't miss them, as well as to make it easy to throw exception if I don't handle a particular enum case.

In my code, the default clause throws exception, assuming that I have an enum value that is unknown. This Eclipse check, however, allows the default clause to cover for any unhandled enum values in the switch statement, meaning that I will never be warned by Eclipse if I don't handle a particular enum value.

I'm a little surprised at this, since it means that Eclipse expects enum switches to not have a default clause, which is generally considered bad style for any switch statement.

I would prefer for Eclipse to not take default into account when evaluating this check.
Comment 1 Philipe Mulet CLA 2006-03-23 09:30:34 EST
So if you have a default, how could you enter it if you had coverage for all constants already ?

Post 3.2, we may introduce additional option to handle this situation, if this becomes a popular request.
Comment 2 Jason Bennett CLA 2006-03-23 13:20:09 EST
Here's my problem:

if I add another value to an enum, I want to know which switch statements are affected, hence the eclipse warning. OTOH, if I miss a switch, I don't want the code to just fall through and go on, I want it to throw a critical error. Hence my need for eclipse to warn me AND to have a default to catch anything missed.
Comment 3 Eclipse Webmaster CLA 2009-08-30 02:37:43 EDT
As of now 'LATER' and 'REMIND' resolutions are no longer supported.
Please reopen this bug if it is still valid for you.
Comment 4 Zach Shelton CLA 2013-01-15 12:47:33 EST
I believe this is still a problem. The issue comes into play particularly when code is maintained by separate developers with different compiler error/warning settings.

Assume that I create am enum with two cases and a switch statement somewhere that uses that enum. If my compiler settings are such that I get an error or warning if an enum value is not covered, then the default case in the switch is effectively "unnecessary". In fact I MUST NOT include a default case if I want to be able to get this warning.

Now another developer checks out my project and adds a third value to the enum. His compiler settings are set at the default setting to 'ignore' this potential issue. He doesn't happen to touch the code where my switch was, because his need for the additional enum value was elsewhere in the codebase. He compiles and builds the project. 

Suddenly the switch where I did not include a default case statement has unpredictable behavior. If I open the updated project in my IDE, I'll get the warning, but other developers may not. That is why it is usually recommended to always include a default case in a switch so that unpredictable outcomes can be avoided. So I go back and add a default case to my switch and apologize to the team for my sloppy code. And now that I have a default case I don't get a warning the next time someone adds another enum value, which makes this feature 
lose value.

Recommendation: add a boolean flag to this check to specify whether the default case statement is considered sufficient to cover all enum values.
Comment 5 Stephan Herrmann CLA 2013-01-15 12:56:42 EST
We have fixed for Juno two bugs in this area:

Bug 265744 - Enum switch should warn about missing default
Bug 374605 - Unreasonable warning for enum-based switch statements

In the sequel of these two bugs I wrote some help text at

http://help.eclipse.org/topic/org.eclipse.jdt.doc.user/tasks/task-improve_code_quality.htm?cp=1_3_9

I believe with these changes we have all the bells and whistles needed to detect any problem relating to incomplete switch statements.

Specifically:

> Recommendation: add a boolean flag to this check to specify whether the 
> default case statement is considered sufficient to cover all enum values.

this is addressed by the tag comment //$CASES-OMITTED$ (see the help text).
Comment 6 Stephan Herrmann CLA 2013-01-15 12:57:46 EST
(In reply to comment #5)
> In the sequel of these two bugs I wrote some help text at
> 
> http://help.eclipse.org/topic/org.eclipse.jdt.doc.user/tasks/task-
> improve_code_quality.htm?cp=1_3_9

Or directly at
http://help.eclipse.org/topic/org.eclipse.jdt.doc.user/tasks/task-ensuring_switch_completeness.htm
Comment 7 Zach Shelton CLA 2013-01-15 14:27:47 EST
I have confirmed that this works as desired in Juno. Good work, and good documentation too. I appreciate the addition of the //$CASES-OMITTED$ tag.

Unfortunately, I am stuck with an older release used by the team I am working with. But that is my problem, not an eclipse problem.
Comment 8 Stephan Herrmann CLA 2013-01-15 14:36:43 EST
(In reply to comment #7)

Thanks for your feedback.
Comment 9 Jay Arthanareeswaran CLA 2013-02-04 11:04:40 EST
Verified for 4.3 M5