Menu

False positives unusedStructMember

2022-05-18
2022-05-18
  • Ali Melville

    Ali Melville - 2022-05-18

    Hey.
    I believe that I am encountering a couple of false positives for unusedStructMember in my codebase.
    I am using cppcheck 2.7, and have confirmed that this issue is present in the latest code on Github, as of this morning.

    I am running the following on the command line:

    cppcheck --enable=style test.cpp
    

    and am getting the following output:

    Checking test.cpp ...
    test.cpp:26:3: style: Consider using std::find_if algorithm instead of a raw loop. [useStlAlgorithm]
      {
      ^
    test.cpp:11:11: style: struct member 'BUTTON_TABLE::button' is never used. [unusedStructMember]
     uint32_t button;
              ^
    test.cpp:12:14: style: struct member 'BUTTON_TABLE::string' is never used. [unusedStructMember]
     const char* string;
    

    The below is a simple repro of the issue I am encountering:

    test.cpp

    enum BUTTON : uint32_t
    {
        BUTTON_OK = 1 << 0,
        BUTTON_YES = 1 << 1,
        BUTTON_NO = 1 << 2
    };
    
    struct BUTTON_TABLE
    {
        uint32_t button;
        const char* string;
    };
    
    void create( const uint32_t options, uint32_t& out_button, char*& out_string )
    {
        BUTTON_TABLE buttonTable[] =
        {
            {BUTTON::BUTTON_OK, "OK"},
            {BUTTON::BUTTON_YES, "YES"},
        };
    
        for ( auto && entry : buttonTable )
        {
            if ( options & entry.button )
            {
                out_button = entry.button;
                out_string = entry.string;
    
                break;
            }
        }
    }
    

    I can get around both of these false positives by moving BUTTON_TABLE buttonTable[] outside of the "create" function.
    I can also get around

    test.cpp:11:11: style: struct member 'BUTTON_TABLE::button' is never used. [unusedStructMember]
     uint32_t button;
    

    specifically by changing BUTTON_TABLE::button's type from uint32_t to BUTTON:

    struct BUTTON_TABLE
    {
        BUTTON button;
        const char* string;
    };
    

    Kind Regards

     
  • CHR

    CHR - 2022-05-18

    Thanks for reporting. I have added your example to this ticket: https://trac.cppcheck.net/ticket/10587
    There also is no warning for for (BUTTON_TABLE&& : buttonTable)

     
    👍
    1

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.