我如何告诉 gcc 在 switch/case 语句上不中断地发出警告(或失败)? [英] How can I tell gcc to warn (or fail) on switch/case statements without a break?

查看:50
本文介绍了我如何告诉 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 语句上不中断地发出警告(或失败)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆