| Summary: | "Enum type constant not covered on 'switch'" check allows default to cover all cases | ||
|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Jason Bennett <jasonab> |
| Component: | Core | Assignee: | 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
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. 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. As of now 'LATER' and 'REMIND' resolutions are no longer supported. Please reopen this bug if it is still valid for you. 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. 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). (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 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. (In reply to comment #7) Thanks for your feedback. Verified for 4.3 M5 |