How can I tell gcc to warn (or fail) on switch/case statements without a break?(我如何告诉 gcc 在 switch/case 语句上不中断地发出警告(或失败)?)
问题描述
我有一个复杂的 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
这篇关于我如何告诉 gcc 在 switch/case 语句上不中断地发出警告(或失败)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我如何告诉 gcc 在 switch/case 语句上不中断地发出警告(或失败)?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
