xuexiansen_123
xuexiansen_123

Reputation: 11

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.

Upvotes: 1

Views: 1746

Answers (2)

Daniel Marjamäki
Daniel Marjamäki

Reputation: 3037

This is a false positive of the tool. How can I solve this problem?

I would like to understand why it's a false positive.

The struct members can easily be removed they are redundant.

Upvotes: 0

Firewave
Firewave

Reputation: 411

This is a false positive in Cppcheck. I filed https://trac.cppcheck.net/ticket/10699 for the problem.

Unfortunately the braced initializer is not handled well in all cases. This is being improved. In some cases such warnings go away when you switch to (/) instead but you should not do that.

Upvotes: 0

Related Questions