PC Lint 9.00L版查看以下代码:

  typedef unsigned char boolean_t; //This is actually in a system header file.
  ...
  /* The rest is in the .c file I'm working on. */
  boolean_t booleanVal
  ...
  uint8_t maskedVal;
  maskedVal = 0; //real code has this assigned based on a bitwise-and
  booleanVal = ( maskedVal != 0U );

并给出此错误:
booleanVal = ( maskedVal != 0U );
                                ^
"LINT: <filename> Note 960: Violates MISRA 2004
Required Rule 10.1, Implicit conversion of integer to smaller type"

我已在.lnt文件中使用-strong(B, boolean_t )将布尔类型声明为强布尔类型。
那么,当我将一个清晰的布尔表达式赋给一个清晰的boolen变量时,PC Lint为什么抱怨要转换整数呢?

最佳答案

( maskedVal != 0U )的结果是int然而,即使它是01MISRA抱怨说,它是被强制到更小的unsigned char您的自制布尔类型。
不要发明自己的布尔类型。使用int或现代C实现中可用的形式布尔类型。

10-01 11:52