c++ - Visual Studio 允许为 bool 传入枚举
问题描述
使用 VS 2019 并且我知道今天应该使用“枚举类”,但是我正在使用的代码库有很多以下面的样式 A 定义的枚举。问题是程序员(我)调用 DoSomething(...) 传递枚举而不是 bool 并且 VS 不会抱怨警告或错误。
我已经尝试提高警告级别,但 VS 仍然没有抓住它。没有重写很多代码,有什么方法可以强制 VS 将其视为错误?
typedef enum A {a1,a2} A;
enum class B {b1, b2};
void DoSomething(bool aBool){}
int main()
{
DoSomething(a1); //compiler does NOT catches a1 is not of type bool
DoSomething(B::b1); //compiler catches B::b1 is not of type bool
}
解决方案
感谢您的建议。最好的解决方案是重构为“枚举类”。就我的情况而言,第二好的方法是使用 Clang-Tidy 来捕捉这个错误。我在项目属性->代码分析页面做了两处改动;
- 启用 Clang-Tidy。它的默认值为否
- 启用特定的 Clang-Tidy 选项。所有选项的列表在这里
我原来的帖子中的代码现在将在构建后显示一个警告,这是我最初所期望的。
int main()
{
DoSomething(a1); //Warning readability-implicit-bool-conversion implicit conversion 'A' -> bool
}
推荐阅读
- php - PDOStatement::execute(): SQLSTATE[HY093]: 参数号无效:参数没有...定义?
- javascript - 我使用“withObservables()”方法成功显示了我的数据,但它仍然对我在 WatermelonDB 中的组件没有反应
- java - 如何在 ArrayList 中合并两个相似的项目?
- arrays - 将二维数组传递给函数
- javascript - 当浏览器进行屏幕缩放时,ScreenX 和 ScreenY 不会将元素与光标对齐
- java - 计算节点上的代理数
- python - 从具有相同类 bs4 beautifulSoup 的不同元素中提取多个字符串
- command-line - jq 从文件中读取过滤器导致无效的语法错误
- r - 将某些数字与模数函数一起使用时,模数警告和不正确的结果可能会完全丧失准确性
- c - 打印给定年份和月份的日历