Menu

Switch case fall through

aca_broj_1
2015-01-13
2015-01-19
  • aca_broj_1

    aca_broj_1 - 2015-01-13

    I'm trying to get cppcheck 1.68 to warn on case statements without a dedicated break or fall through comment.

    When I add the --experimental argument I get: cppcheck: error: unrecognized command line option: "--experimental". Has it been deprecated? Is there a replacement?

    Thanks,
    Aleks

     
  • Daniel Marjamäki

    ehm.. as far as I know we haven't had a "--experimental" flag. But maybe I'm wrong.

    I don't want to have that warning in cppcheck. I guess we should remove it.

    I really like to warn about unintentional missing breaks. But Cppcheck does it without looking at comments/annotations.

    I don't know if it helps you.. but clang has this warning.

     

    Last edit: Daniel Marjamäki 2015-01-16
  • aca_broj_1

    aca_broj_1 - 2015-01-16

    I could have sworn it was in an earlier version... maybe I confused it with something else.

    I know the functionality is there, and it's even in the test cases, take a look at https://github.com/danmar/cppcheck/blob/master/test/testother.cpp and see switchFallThroughCase
    I would love to have this in cppcheck, 95% of the bugs in my code seem to stem from missing breaks.

     
  • Daniel Marjamäki

    I don't want to have any FP here in this general case:

    switch (x) {
    case 1:
       foo();
    case 2:
       bar();
    };
    

    With the checker switchFallThroughCase a comment must be added to suppress the warning. I don't like that. I think it's a bad idea that comments or annotations must be used to silence all FP.

    I would love to detect more unintentional switch case fallthrough. If you have some good ideas how we can do it feel free to write a suggestion.

    As an inspiration, we warn if a fall through results in useless code. For example the assignment "y = 1;" is useless here:

    switch (x) {
    case 1:
        y = 1;
    case 2:
        y = 7;
        break;
    }
    

    If you check that code you'll get this warning:

    [a.c:6] -> [a.c:8]: (warning) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing?
    
     

    Last edit: Daniel Marjamäki 2015-01-19

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.