c++ - 如何解决此 lint 警告 "Implicit binary conversion from int to unsigned int"

标签 c++ lint misra pc-lint

这是代码:

test.cpp

unsigned short x;
bool y;
if ((x==1)&& y)
{
  ...
}
else
{
  ...
}

我收到一条 lint 消息:

Note 912 Implicit binary conversion from int
to unsigned int [MISRA Rule 48]
为什么?以及如何避免这种情况?

最佳答案

您正在比较 x(默认为 unsigned Short)和 1(默认为 int)。因此你得到了隐式二进制转换的东西。

给你的编译器一个提示,表明你确实想要将x与另一个无符号值进行比较:

if ((x==1U) && y)

关于c++ - 如何解决此 lint 警告 "Implicit binary conversion from int to unsigned int",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35123841/

相关文章:

css - 如何安装和使用我为 CLI 编写的自定义 CSLint 规则?

c - 米斯拉 C : 2004: typedefs that indicate size and signedness should be used in place of the basic types

c++ - 向 C++ 软件添加 GUI 的最佳语言

c++ - setbuf() 会影响 cout 吗?

sass - Stylelint 禁用规则, "no-descending-specificity"null 不起作用

c - 如何将 C 代码制作成 MISRA C :2012 compliance?

c - 米斯拉 11.3 : cast from int to pointer

c++ - Int into Array C++ 请帮忙

c++ - 如何表达常量的类型?

Android Studio 自动检查被禁用