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 case
s. 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 语句上不中断地发出警告(或失败)?


基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01