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
}

 

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/focus-z/p/11986911.html