爽弯弯! 2021-04-27 20:38 采纳率: 0%
浏览 24

Cppcheck stop checking unusedStructMember

原提问链接: https://stackoverflow.com/questions/67276661/cppcheck-stop-checking-unusedstructmember

**cppcheck version**:2.3

1.Scan the following code (rsvd.c)
```
typedef struct {
    int a;
    // cppcheck-suppress unusedStructMember
    int b;
    int c;
} test;


int main()
{
    test A;
    A.a = 5;

    return 0;
}
```
run `cppcheck --inline-suppr --enable=all rsvd.c`
The results are as follows(as expected):
> Checking rsvd.c ...
>
> rsvd.c:7:9: style: struct member 'test::c' is never used. [unusedStructMember]
>
>    int c;
>
> rsvd.c:14:9: style: Variable 'A.a' is assigned a value that is never used. [unreadVariable]
>
> A.a = 5;

2.Scan the following code
```
typedef struct {
    int a;
    int b;
    int c;
} test;


int main()
{
    test A = {1, 2, 3};
    return 0;
}
```
The results are as follows(not expected):
> Checking rsvd.c ...
>
> rsvd.c:3:9: style: struct member 'test::a' is never used. [unusedStructMember]
>
>    int a;
>
> rsvd.c:4:9: style: struct member 'test::b' is never used. [unusedStructMember]
>
>    int b;
>
> rsvd.c:5:9: style: struct member 'test::c' is never used. [unusedStructMember]
>
>    int c;
>
> rsvd.c:11:12: style: Variable 'A' is assigned a value that is never used. [unreadVariable]
>
>    test A = {1, 2, 3};

In the preceding code, a value has been assigned to the structure member variable. This is a false positive of the tool. How can I solve this problem? Thank you very much.
 

  • 写回答

1条回答 默认 最新

  • 泡沫o0 2023年度博客之星上海赛道TOP 1 2023-06-11 14:15
    关注

    Cppcheck 的 "unusedStructMember" 检查是一个风格(style)检查,用于检测那些在代码中定义但未被使用的结构体成员。在你的第二段代码中,虽然你为结构体 test 的所有成员赋了值,但是在接下来的代码中并没有使用这些值。这就是为什么 Cppcheck 报告这些成员变量没有被使用的原因。

    解决这个问题的方法可以有以下几种:

    1. 在你的代码中使用这些变量。即使是简单地打印它们,也足够让 Cppcheck 认为它们已被使用。

    2. 如果你认为这个报告是误报,或者你希望忽略这个检查,你可以为 test 结构体的每个成员添加 suppression 注释,像在你的第一段代码中那样做。

    3. 你也可以在运行 Cppcheck 时禁用这个检查,使用参数 --suppress=unusedStructMember

    要理解的关键是,这是一个风格检查,不一定意味着你的代码有错误。Cppcheck 是一个静态分析工具,用于帮助开发者发现可能的问题,但它的报告并不总是表明存在实际问题。具体是否需要采取行动,取决于你对代码的理解和项目的需求。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)