GCC 7, -Wimplicit-fallthrough warnings, and portable way to clear them?

前端 未结 3 1817
执笔经年
执笔经年 2020-12-09 14:20

We are catching warnings from GCC 7 for implicit fall through in a switch statement. Previously, we cleared them under Clang (that\'s the reason for the comment seen below):

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 15:22

    Clean C solution:

    int r(int a) {
        switch(a) {
        case 0:
            a += 3;
        case 1:
            a += 2;
        default:
            a += a;
        }
        return a;
    }
    

    becomes:

    int h(int a) {
        switch(a) {
        case 0:
            a += 3;
            goto one;
        case 1:
        one:
            a += 2;
            goto others;
        default:
        others:
            a += a;
        }
        return a;
    }
    

    EDIT: Moved the labels after case statements, as suggested by Stéphane Gourichon in comments, to see the fallthrough more easily.

提交回复
热议问题