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()
{
}
// TU1.cpp
int
=
0
int
(
)
{
+
+
}
// TU2.cpp
void
(
)
{
}

 

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 ()
{
}
// TU1.cpp
int
=
0
int
&
(
)
{
static
int
=
0
return
}
int
(
)
{
+
+
(
)
}
// TU2.cpp
void
(
)
{
}

 

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
}
// TU1.cpp
int
=
0
int
&
(
)
{
static
int
=
0
return
}
int
(
)
{
+
+
(
)
}
// TU2.cpp
extern
int
void
(
)
{
+
+
          // 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

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @   清风oo  阅读(417)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2018-12-05 Convert DataFrame string complex i to j python // “Cloning” row or column vectors
点击右上角即可分享
微信分享提示