MISRA-C 2004 规则解读(181S-200S)


181 S:No space between if, while, for and expresn. (if, switch, while, for)关键字后请跟上一个空格:

INT_t32_t main(void)
{
   INT_t32_t i=3;
   if( 1==2 ) {2;}       /* not compliant */
   switch(i){            /* not compliant */
   }
   switch (i){
   }
   while(i==0){2;}      /* not compliant */  
   do{2;}while(i==0);   /* not compliant */
   for(i=0;i>2;i++){2;} /* not compliant */
}

182 S:No space after semi colon in for expression. for循环表达式中分号后打一个空格,看上去更整洁:

for ( i = 0; i < 5; i++ ){ i++;}    /*compliant */

183 S:No newline after semi colon. 分号后再起一行,便于阅读:

static void static_183(void)
{
  INT_t32_t i; INT_t32_t j;   /* not compliant */

  for ( i = 0; i < 5; i++ )
  { i++;}  /* not compliant */
}

184 S:Spaces round -> or [] operators. -> 或 [] 操作符连接处不能有空格:

void static_184(struct tag * astr, 
                INT_t32_t b []     /* not compliant */ )
{
  astr ->f = 2; /* not compliant */
}

185 S:Space between unary operator and operand. 单目运算符和操作对象之间不应当有空格:

void proc( INT_t * g){
   * g = 77;
   *g = * g + 77;

}

INT_t main(void){

    INT_t i=9;
    INT_t j=19;
    INT_t * p;

    i = - j;
    p = & j;
}

186 S:Space missing before or after binary operator.二进制运算符之间缺少空格:

INT_t main(void){
   INT_t i = 9;
   INT_t j =8;
   INT_t k= 7;
   UINT_t x = 3;
   UINT_t y = 17;

   i= j;
   i = j /k;
   i = j <<k;
   i = j && k;
}

187 S:Tab CHAR_tacter in source. 源文件中使用空格而不是Tab键缩进。

188 S:{ or } not on line by itself. 为了提高可读性,花括号的使用需要注意:

struct tagnotok { CHAR_t a; } data; /* not compliant */

struct
{
  CHAR_t x;
} moredata;

void proc_ok()
{
;
}

void static_188 (void){   /* not compliant */
   ;
}

189 S:Input line exceeds limit. 每一行的字符数不能过多。

190 S:{ … } contents not indented by N spaces. 花括号后一行缩进N个空格,N值可以配置。

191 S:Space between function name and parenthesis. 函数名和括号之间不应当有空格:

void notokproc1 () /* not compliant */
{
}

192 S:Static not on separate line in function defn.为了提高代码可读性,inline, static 和 const修饰的函数,修饰符和参数可以分两行写。

193 S:Inline not on separate line in function defn.同192S,内连函数定义中修饰符和函数定义可以分两行写:

inline void one::mem1(void) /* not compliant */

194 S:Const not on separate line in member defn.同192S,const修饰符和函数定义可以分两行写。

195 S:Function return type needs a new line. 为了提高代码可读性,函数定义中返回值类型和参数可以分两行写。

196 S:Access specifier missing (added by Testbed). 类定义中成员缺少Public、Protected或Private修饰符。

197 S:Access specifiers in invalid order. 访问权限修饰符在类定义时应当遵循特定的顺序,首先是Public类型,然后是Protected类型,最后是Private类型:

class class_197{
   public:
   INT_t_16 d;
   private:   /* not compliant */
   INT_t32_t a;
   protected: /* not compliant */
   INT_t32_t q;
};

198 S:Use of privacy statement in struct.结构体中成员不应当修饰为privacy类型。

199 S:Use of anonymous namespace. 避免使用匿名命名空间。

200 S:Define used for numeric constant. C++中建议const代替宏定义常量,C中建议使用宏代替const定义常量。

猜你喜欢

转载自blog.csdn.net/u013992766/article/details/51172517
今日推荐