c - 为什么 gcc 会引发隐式失败警告?
问题描述
给定代码:
#include <stdlib.h>
enum one {
A, B
};
enum two {
AA
};
int main(int argc, char *argv[])
{
enum one one = atoi(argv[1]);
enum two two = atoi(argv[2]);
if ((one != A && one != B) || two != AA)
return 1;
switch (one) {
case A:
switch (two) {
case AA:
return 2;
}
case B:
return 3;
}
return 0;
}
当我使用 using 编译它时,gcc -Wimplicit-fallthrough test_fallthrough.c
我收到以下警告
test_fallthrough.c: In function 'main':
test_fallthrough.c:21:3: warning: this statement may fall through [-Wimplicit-fallthrough=]
21 | switch (two) {
| ^~~~~~
test_fallthrough.c:25:2: note: here
25 | case B:
| ^~~~
它试图警告什么,我能做些什么使它不发出警告(我宁愿避免添加诸如 之类的评论/* Falls through. */
)
解决方案
break
通常,编译器会在每个主体之后检查语句case
,以确保程序流(失败)没有错误。
在您的情况下,case A
body 没有 a break
,case B
当switch
语句与case A
.
switch (one) {
case A:
switch (two) {
case AA:
return 2;
}
// <------ no break here, flow will continue, or fall-through to next case body
case B:
return 3;
}
return 0;
}
推荐阅读
- javascript - 在javascript中过滤二维数组
- python - PCA 决策面上的标签点 - SVM
- reactjs - 如果 npm start 未运行,则反应站点在域上给出 502 Bad Gateway,nginx
- uml - 您是否允许在类或活动图中使用门户?
- java - 有什么方法可以强制 Maven 只构建阴影 jar?
- terraform - 在 Terraform 中将数字显示为两位数
- ios - 如果在 App Store 中将其更改为未列出的应用程序,iOS 应用程序会受到哪些影响?
- node.js - 如何在多个 Pod 微服务中处理 MQTT 订阅
- windows-server-2019 - 如何在 Windows Server 2019 上安装 WinGet?
- rust - Rust 可选迭代器默认值