我如何告诉 gcc 在 switch/case 语句上不中断地发出警告(或失败)?

2022-01-19 00:00:00 gcc switch-statement c++ gcc-warning

我有一个复杂的 switch 语句,我忘记在其中一个 case 的末尾放置一个 break.这是非常合法的,结果我陷入了下一个case.

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 result I had a fall-through to the next case.

如果我忽略了 break 语句,有什么方法可以让 gcc 发出警告(甚至更好,失败)?

Is there any way to have gcc warn (or even better, fail) if I neglect to put a break statement?

我意识到有许多有效的用例(我经常在我的代码中使用它们),例如 this question,所以显然这样的警告(或失败)需要一个简单的弃权,以便我可以轻松地说,我确实想在这里失败."

I realize that there are many valid use cases (and I use them often in my code), as exemplified in this question, so obviously such a warning (or failure) would need a simple waiver so that I could easily say, "I do want to fall-through here."

有没有办法告诉 gcc 这样做?

Is there any way to tell gcc to do this?

推荐答案

在 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=7652.不过好像还没有实现

There's a discussion about such a feature (-Wswitch-break) at http://gcc.gnu.org/bugzilla/show_bug.cgi?id=7652. But it doesn't seem to be implemented yet

相关文章