Menu

Flase Positive unused struct member in header file [2.14-dev]

2024-02-13
2024-02-13
  • Sven Wienand

    Sven Wienand - 2024-02-13

    Hi all, i compiled the current 2.14 version and found that it created unused struct member errors in comparison to my execution before with 2.12. I made some tests and narrowed it down, that it only happens if i define the struct in the header file.

    Build Windows Version.

    Example Code
    main2.h

    typedef struct Test {
        int test1;
        int test2;
    } testStruct_t;
    

    main2.c

    #include "main2.h"
    
    void main(void)
    {
        testStruct_t test;
        test.test1 = 5;
    }
    

    Output from 2.12:
    Not sure why 2.12 does not show the unused member here because in my main program i definatly get correct unused member warnings.

    $ cppcheck --enable=all --dump src/main2.c src/main2.h
    cppcheck: Windows 64-bit binaries currently default to the 'win64' platform. Starting with Cppcheck 2.13 they will default to 'native' instead. Please specify '--platform=win64' explicitly if you rely on this.
    Checking src\main2.c ...
    src\main2.c:7:16: style: Variable 'test.test1' is assigned a value that is never used. [unreadVariable]
        test.test1 = 5;
                   ^
    1/2 files checked 56% done
    Checking src\main2.h ...
    2/2 files checked 100% done
    Active checkers: 134/565
    

    Output from 2.14-dev:
    test2 is corectly detected as unused but test1 is not.

    $ cppcheck --enable=all --dump src/main2.c src/main2.h
    Checking src\main2.c ...
    src\main2.c:6:16: style: Variable 'test.test1' is assigned a value that is never used. [unreadVariable]
        test.test1 = 5;
                   ^
    1/2 files checked 55% done
    Checking src\main2.h ...
    src\main2.h:2:9: style: struct member 'Test::test1' is never used. [unusedStructMember]
        int test1;
            ^
    src\main2.h:3:9: style: struct member 'Test::test2' is never used. [unusedStructMember]
        int test2;
            ^
    2/2 files checked 100% done
    nofile:0:0: information: Active checkers: 106/592 (use --checkers-report=<filename> to see details) [checkersReport]
    

    Putting both into one file in 2.14-dec the test is fully correct.

    $ cppcheck --enable=all --dump src/main2.c
    Checking src\main2.c ...
    src\main2.c:4:9: style: struct member 'Test::test2' is never used. [unusedStructMember]
        int test2;
            ^
    src\main2.c:10:16: style: Variable 'test.test1' is assigned a value that is never used. [unreadVariable]
        test.test1 = 5;
                   ^
    nofile:0:0: information: Active checkers: 106/592 (use --checkers-report=<filename> to see details) [checkersReport]
    

    Somehow in between versions this broke.
    I am not exactly sure (as i am on my work pc now and not on the home pc where i builded the version but the base commit is likely 503c109a1c569b3f4da76582634d226432debda8). Currently can not confirm it on the main due to not able to rebuild but checking commit history does not look to have any changes regarding this.

     

    Last edit: Sven Wienand 2024-02-13
  • CHR

    CHR - 2024-02-13

    I don't think warnings about unused members from a header are desirable, since your program might not be the only one which consumes that header.
    Edit: Why are you explicitly checking headers on their own?

     

    Last edit: CHR 2024-02-13
  • Sven Wienand

    Sven Wienand - 2024-02-13

    I am checking header as guys in my company said that they once had problems with the headers not fully checked. And they started checking heads explicitely. I also noticed that in the dump file of the c file the included headers are parsed. Am a correct that the full header file will be put as tokens within the c file? Exactly like i would check them their own?

    I don't think warnings about unused members from a header are desirable

    Yeah if i understand you correctly it is exactly what i was proposing. The unusedMember warning in the header file is a false positive. So the unused member warning shouldn't be there in the 2.14 development version. And this warning wasn't there in 2.12. Therefore some changes caused this false positive. Or did i miss understand you?

     
  • CHR

    CHR - 2024-02-13

    Am a correct that the full header file will be put as tokens within the c file? Exactly like i would check them their own?

    Yes.

    I would say 2.14dev works as expected, it is told to check the header on its own, and the unused members are reported. Not sure what happens in 2.12.

     
  • Sven Wienand

    Sven Wienand - 2024-02-13

    okay understand your argument. Based on your feedback i will stop checking heads directly

    Case Closed

     

    Last edit: Sven Wienand 2024-02-13

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.