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

Bug 363663

Summary: [checker]Initialisation in a switch
Product: [Tools] CDT Reporter: Mathias De MarĂ© <mathias.demare>
Component: cdt-codanAssignee: CDT Codan Inbox <cdt-codan-inbox>
Status: NEW --- QA Contact: Elena Laskavaia <elaskavaia.cdt>
Severity: enhancement    
Priority: P3 CC: cdtdoug, sarahehabm, yevshif, zeratul976
Version: 8.1.0   
Target Milestone: ---   
Hardware: All   
OS: All   
See Also: https://git.eclipse.org/r/57362
Whiteboard:
Attachments:
Description Flags
Added a new CDT Codan checker for this semantic error
none
Added a test unit for the InitializationInCaseChecker
none
Fix issue in the InitializationInCaseChecker
none
Add more test cases none

Description Mathias De Maré CLA 2011-11-13 14:57:00 EST
Build Identifier: 

Combined declaration and initialisation in a switch statement is not allowed if the scope is not changed.

This is because each of the 'case' entries in the switch represents a label. This means you can jump over the statements in a case, which is not allowed for declaration + initialization (see C++ 2003, 6.7-3).

The solution is to add a new scope in the case.

Error case:
switch(a) {
case 0:
    int j = 2; <-- error
default:
        break;
}

Working case:
switch(a) {
case 0:
    {
        int j = 2;
    }
default:
        break;
}

Reproducible: Always
Comment 1 Sarah Mostafa CLA 2015-09-23 10:46:50 EDT
Created attachment 256793 [details]
Added a new CDT Codan checker for this semantic error
Comment 2 Sarah Mostafa CLA 2015-09-23 10:48:25 EDT
Created attachment 256794 [details]
Added a test unit for the InitializationInCaseChecker
Comment 3 Nathan Ridge CLA 2015-09-23 11:11:40 EDT
Thanks for writing a patch and test, Sarah! Could you submit them for review via Gerrit as described here [1]?

[1] https://wiki.eclipse.org/CDT/git#Using_Gerrit_for_CDT
Comment 4 Sarah Mostafa CLA 2015-09-29 20:39:08 EDT
Hello Nathan. I'll make sure I set everything up and submit them for review on Gerrit
Comment 5 Sarah Mostafa CLA 2015-09-29 20:40:35 EDT
Created attachment 256929 [details]
Fix issue in the InitializationInCaseChecker
Comment 6 Sarah Mostafa CLA 2015-09-29 20:41:27 EDT
Created attachment 256930 [details]
Add more test cases
Comment 7 Eclipse Genie CLA 2015-10-04 17:15:58 EDT
New Gerrit change created: https://git.eclipse.org/r/57362
Comment 8 Nathan Ridge CLA 2015-12-03 15:24:08 EST
Sarah, thank you for submitting the patch to Gerrit. I saw Elena reviewed it and had some review comments; do you plan to address them?