MISRA-C 2004 规则解读(121S-140S)


121 S:Use of boolean expression in switch. 避免在switch case语句中使用BOOL类型:

void static_121(void)
{
   BOOL flag = FALSE;

   switch (flag)  /* not compliant */
   {
      case TRUE:
          break;
      case FALSE:
          break;
      default:
          break;
  }
}

122 S:Use of abort, exit, etc. 避免使用语法abort, exit, etc:

/********************************************************
 * Standard 122 S : Use of abort, exit, etc.
 ********************************************************/ 
void static_122(BOOL flag)
{
   if (flag)
   {
      abort();
   }
   exit(0);
}

123 S:Use of underlying enum representation value. 枚举类型只允许在同类型之间比较:

void static_123(void)
{
   enum E_type { Enum1, Enum2 , Enum3};

   UINT32_t ui;

   ui = Enum1;  /* not compliant */

   /* ... */ 
}

124 S:Use of prefix ++ or –. 尽量少使用自增或自减运算符。

125 S:Use of ## or # in a macro. 为避免意外的结果发生,不要在宏定义中使用##或#。

126 S:A #if has no #endif in the same file.#if和#endif需要成套使用。

127 S:Array has no bounds specified. 数组声明时候信息需要完整:

void static_127( INT32_t par[] ) /* not compliant, unless modifier 286 set */
{
   INT32_t array[] = {0,1,2};    /* not compliant, unless MISRA-C:2004 used */

   /* ... */ 
}

128 S:Parameter has same name as global variable. 函数参数名避免与全局变量重名。

129 S:Parameter has same name as type or tag. 函数参数名避免与结构体重名。

130 S:Included file is not permitted. 某些头文件在测试工具中规定不允许使用。

131 S:Name reused in inner scope. 避免内部变量与外部变量重名。

132 S:Assignment operator in boolean expression.bool表达式中避免出现变量赋值语句:

void static_132(void)
{
   BOOL flag = FALSE;

   if (flag = FALSE) /* not compliant -  should be (flag == FALSE) */ 
   {
      /* ... */ 
   }
}

133 S:Assignment operator in RHS of && or ||.运算符两侧避免出现赋值语句:

void static_133(void)
{
   BOOL flag = FALSE;
   INT32_t y = 0, x = 0;

   if ( flag &&
        ((x = y) == 0) /* not compliant */
      )
   {
      /* ... */
   }
}

134 S:Volatile variable in complex expression. 避免使用Volatile修饰变量,更不要在复杂表达式中使用Volatile变量。

135 S:Parameter list is KR. 某些编译器支持函数参数声明时只声明名称而不定义类型,在使用时再定义类型,应当避免这种老式用法:

void static_135(  p_1,  p_2) /* not compliant */
UINT32_t p_1;
UINT32_t p_2;
{
   /* ... */ 
}

136 S:Bit operator with boolean operand. bool表达式中避免使用位操作符,因为位操作符(|或者&)容易与连接符(||或者&&)混淆。

137 S:Bit operator acting on boolean value. 同136S

138 S:Anonymous bit field used in structure.避免在结构体位域中使用匿名方法:

struct  bad {UINT32_t x:1,          :1; UINT32_t z:1;}
struct good {UINT32_t x:1; UINT32_t y:1; UINT32_t z:1;}

139 S:Construct leads to infeasible code.指在代码中某个逻辑导致部分语句不可达,例如两个常量的比较,结果是恒真或假的,又比如两个并列的条件是耦合的:

typedef enum { LANE_0 = 0, LANE_1 = 1, LANE_LAST = 3 } lane_t;
extern lane_t get_lane ( void );

/********************************************************
 * Standard 139 S : Construct leads to infeasible code.
 ********************************************************/

void static_139( void )
{
   lane_t lane = get_lane();
   if ( (lane > LANE_0) && ( lane <= LANE_LAST))
      /* not compliant - False branch of 'lane <= LANE_LAST' never reached */
      { /* ... */ }

   if (defval)
      /* not compliant - True branch never reached*/
      { /* ... */ }


   if (0) { /* NEVER EXECUTED */ } else {...};
}

140 S:Infeasible loop condition found. 避免在循环中出现恒真或恒假的条件:

#define defval 0

/********************************************************
 * Standard 140 S : Infeasible loop condition found.
 ********************************************************/ 

void static_140(void)
{
   while (0)          /* not compliant */
      { /* ... */ }

   while( defval )   /* not compliant */
      { /* ... */ }
}
  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值