您的位置:首页 > 其它

MISRA-C 2004 规则解读(21S-40S)

2016-04-17 11:38 543 查看
21 S:Number of parameters does not match. 函数定义的入参的个数与使用时传入的入参个数不一致。

22 S:Use of obsolete language feature ( use = - ). 不应当使用早期C语言中存在的语法规则,如=- =* =+等运算符。

23 S:Procedure is not called in text analysed. 在main函数的源文件中,如果某个函数不被本文件调用,该函数放在其他的源文件中比较合适。

24 S:Use of Noanalysis annotation. 使用标记某段源代码,这段代码不会被进行静态规则检查。

25 S:Null case(s) in switch statement. 避免某个case语句为空而执行到了下一个case:

INT_32 static_25(INT_32 p_1)
{
INT_32 i = 0, j = 0;

switch (p_1)
{
case 0:
j = 0;
break;
case 1:
case 2:  /* not compliant */
j=i;
break;
default:
i = j + 1;
}

return i + j;
}


26 S:Infinite loop used.避免使用死循环:

for(;;)
while(1) {...}
do .. while(1)


27 S:Void procedure with return statement. 函数返回值被定义为void,却仍然有返回值,此条在VS2008中不能编译通过。

28 S:Duplicated Base Classes in a Derived class. 某个类有两个父类,在两个父类中存在同名函数,当子类调用该函数时会造成混乱:

class Base
{
public:
void func ( void );
};

class Sub1: public Base { /* ... */ };
class Sub2: public Base { /* ... */ };

/********************************************************
* Standard 28 S : Duplicated Base Classes in a Derived class.
********************************************************/

class derived : public Sub1, public Sub2 { /* ... */ }; // not compliant

void static_028 ( void )
{
derived d;
d.func ();  // ambiguous whether Sub1::Base::func()
//                or Sub2::Base::func()
}


29 S:Use of += or -= operators found. 避免使用+= 或者 -=运算符。

30 S:Deprecated usage of ++ or – operators found. 避免在表达式中使用连+或-符号:

void static_30(void)
{
INT32_t x = 1u;
INT32_t y = 2u;

BOOL flag = FALSE;

if (flag == FALSE)
{
x++;        /* compliant */
}

x = x + y++;   /* not compliant */
func( x++ );   /* not compliant */
}


31 S:Use of break statement in loop. 避免在循环中使用break语句,建议用置标识的方法跳出循环。

32 S:Use of continue statement.避免在循环中使用continue语句,建议用置标识的方法控制循环。

33 S:Use of ternary operator found.避免使用三目运算符。

34 S:No parameters declared in proc specification. 在函数声明时无参数,在函数实现时却有参数,在VS2008下编译不通过。

35 S:Static procedure is not called in text analysed. 函数被声明为static类型,却没有在本文件调用,那么static修饰符是不必要的。

36 S:Function has no return statement. 函数不为void类型却无返回值。

37 S:Procedure parameter has a type but no identifier. 避免函数参数有类型却忽略变量名:

void static_37 ( UINT32_t ); /* not compliant */

void static_37 ( UINT32_t )  /* not compliant - constraint error */
{
/*do something*/
}


38 S:Use of static class member. 不建议对类中的变量和函数使用static修饰符。

39 S:Unsuitable type for loop variable. 有些类型不适合当作控制循环的变量,如float类型:

void static_39( void )
{
FLOAT32_t f = 0.0F;

for (f = 0.0F; f < 10.0F; f = f + 1.0F) /* not compliant */
{
/* ... */ ;
}
}


40 S:Loop index is not declared locally. 避免控制循环条件的变量不在本函数内声明,有可能引起循环判断的条件失控:

INT32_t loop_standards (INT32_t p_1)
{
INT32_t r = 10;

for (global_f = 0; global_f < 10; global_f = global_f + 1) /* not compliant */
{
r--;
}
return r;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MISRA-C