MISRA C:2012 8.2 Unused code

0594 case表达式条件是负数

extern int x;

void foo(unsigned int n)
{
    switch ( n )
    {
    case -1:                    /* Messages 0594 and 0277*/
        ++x;
        break;

    default:
        --x;
        break;
    }
}

1460 switch的标签值不在枚举类型之内

enum SIZE { small,  medium,  large };

int paint (enum SIZE box)
{
    switch (box)
    {
    case small:
        return 10;

    case 1:                             /* Message 1470 */
        return 30;

    case 5:                             /* Message 1460 and 1470 */
        return 20;

    default:
        return 0;
    }
}

1503 The function 'name' is defined but is not used within this project.

The object declared here is not referenced at any point within the project. During maintenance it is possible that a function or an object used in the original design, may be replaced or become redundant. If possible, the declaration should be removed in order to reduce the potential for confusion.

Example:


// TU1.cpp
int globalObj = 0;

int main()
{
  ++globalObj; 
}

// TU2.cpp
void bar()
{
}

In order to reduce the number of global variables, it is decided to change this global variable into a local static.

Example:


// TU1.cpp
int globalObj = 0;

int & getGlobalObj()
{
  static int obj = 0;
  return obj;
}

int main()
{
  ++getGlobalObj();
}

// TU2.cpp
void bar ()
{
}

However, incorrectly, the declaration of globalObj has not been removed. At a later stage, a bug fix is required and the developer incorrectly changes TU2 so it now uses globalObj.

Example:


// TU1.cpp
int globalObj = 0;

int & getGlobalObj()
{
  static int obj = 0;
  return obj;
}

int main()
{
  ++getGlobalObj();
}

// TU2.cpp
extern int globalObj;

void bar ()
{
  ++globalObj;          // ERROR: This variable was no longer used in the project
}

 

 2882 “ switch”语句将绕过局部变量的初始化。

extern void func(int p);

extern void foo(int n)
{
    switch (n)                      /* 2882 */
    {
        int x = 3;
    case 1:
        func(x + 1);                /* 2961 */
        break;

    case 2:
        func(x + n);
        break;

    default:
        x = 5;
        func(x + n);
        break;
    }
}

3229 3207 static变量被定义但是没有被使用

static int b;                   /* Message 3229 */
// b 没有在这个文件中使用,应该定义在函数里
void f1(const int n)
{
    b = n;
    return;
}

void f2(const int n)
{
    b = n + 1;
    return;
}
static int s1 = 0;                      /* Message 3207 */
static int s2 = 0;                      /* Message 3229 */

extern void foo(void)
{
    s2 = 5;
    return;
}

3219 在此转换单元中未使用静态函数'%s()'。

A function defined with the 'static' storage-class specifier specifier has internal linkage and therefore cannot be called from outside its own translation unit. This particular function is not called from anywhere within the existing translation.unit either, and so it is currently unreachable and therefore redundant.

static int g;                           /* Message 3207 */


static void foo(void)                   /* Message 3219 */
{
}

 2981  定义该对象时执行的初始化似乎是多余的,因为在通过赋值修改之前未使用对象的值

int foo(void)
{
    int carno = 0;              /* 2981 */

    carno = 1;

    return carno;
}

2982 此赋值是多余的。 在修改之前,永远不会使用此对象的值。

extern int bar(int n);

int foo(void)
{
    int x;

    x = 0;                     /* 2982 */

    x = bar(1);

    return x;
}

2986

原文地址:https://www.cnblogs.com/focus-z/p/11986911.html