How can I tell gcc to warn (or fail) on switch/case statements without a break?

后端 未结 6 1338
有刺的猬
有刺的猬 2021-01-17 17:50

I have a complicated switch statement, and I forgot to put a break at the end of one of the cases. This is quite legal, and as a resu

6条回答
  •  不知归路
    2021-01-17 18:16

    This check is available in Cppcheck, a free static analyser for C and C++ code. The check is currently marked "experimental", so you will need to use the --experimental command line switch to turn it on.

    This check warns against a nonempty case clause that falls through to the next case without a control flow statement such as break, continue, return, etc, unless there is a comment with wording such as // fall through immediately preceding the next case.

    You can get an idea for the kinds of constructs this handles by having a look at the switchFallThroughCase test cases in the source code.

提交回复
热议问题