Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 363663 - [checker]Initialisation in a switch
Summary: [checker]Initialisation in a switch
Status: NEW
Alias: None
Product: CDT
Classification: Tools
Component: cdt-codan (show other bugs)
Version: 8.1.0   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: CDT Codan Inbox CLA
QA Contact: Elena Laskavaia CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-13 14:57 EST by Mathias De Maré CLA
Modified: 2015-12-03 15:24 EST (History)
4 users (show)

See Also:


Attachments
Added a new CDT Codan checker for this semantic error (4.48 KB, patch)
2015-09-23 10:46 EDT, Sarah Mostafa CLA
no flags Details | Diff
Added a test unit for the InitializationInCaseChecker (1.40 KB, patch)
2015-09-23 10:48 EDT, Sarah Mostafa CLA
no flags Details | Diff
Fix issue in the InitializationInCaseChecker (5.41 KB, patch)
2015-09-29 20:40 EDT, Sarah Mostafa CLA
no flags Details | Diff
Add more test cases (1.74 KB, patch)
2015-09-29 20:41 EDT, Sarah Mostafa CLA
no flags Details | Diff

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