cppcheck版本:2.3
1.扫描以下代码(rsvd.c)
typedef struct {
int a;
// cppcheck-suppress unusedStructMember
int b;
int c;
} test;
int main()
{
test A;
A.a = 5;
return 0;
}
运行cppcheck --inline-suppr --enable=all rsvd.c
,结果如下(如预期的):
检查rsvd.c .
rsvd.c:7:9: style: struct成员'test::c‘从未使用过。unusedStructMember
int c;
rsvd.c:14:9:样式:变量'A.a‘被分配给一个从未使用过的值。unreadVariable
A.a = 5;
2.扫描下列代码
typedef struct {
int a;
int b;
int c;
} test;
int main()
{
test A = {1, 2, 3};
return 0;
}
结果如下(未预期):
检查rsvd.c .
rsvd.c:3:9: style: struct成员'test::a‘从未使用过。unusedStructMember
int a;
rsvd.c:4:9: style: struct成员'test::b‘从不使用。unusedStructMember
int b;
rsvd.c:5:9: style: struct成员'test::c‘从未使用过。unusedStructMember
int c;
rsvd.c:11:12:样式:变量'A‘被分配给一个从未使用过的值。unreadVariable
试验A= {1,2,3};
在前面的代码中,已为结构成员变量分配了一个值。这是一个假阳性的工具。我该如何解决这个问题?非常感谢。
发布于 2022-01-09 19:29:25
这在Cppcheck是个假阳性。我为这个问题提交了https://trac.cppcheck.net/ticket/10699文件。
不幸的是,在所有情况下都不能很好地处理带括号的初始化程序。这一情况正在得到改善。在某些情况下,当您转而使用(
/)
时,这些警告就会消失,但是您不应该这样做。
https://stackoverflow.com/questions/67276661
复制相似问题