Menu ▾ ▴

unusedPrivateFunction and unusedFunction false positives

4 days ago
4 days ago
  • Nikita Leontiev

    Nikita Leontiev - 4 days ago

    cppcheck 2.22.0 generates unusedPrivateFunction and unusedFunction for the following code:

    #define CASE(msg, handler) \
        case msg: CASE_##msg((handler))
    #define MSG1 1
    #define CASE_MSG1(handler) handler()
    
    class Object
    {
    private:
        int m_num;
    public:
        Object() : m_num(0) {}
        static void msg_proc(int msg, void *context)
        {
            switch (msg)
            {
                CASE(MSG1, reinterpret_cast<Object*>(context)->msg1_handler);
            }
        }
    private:
        void msg1_handler()
        {
            m_num = 1;
        }
    };
    
    int main()
    {
        Object obj;
        Object::msg_proc(MSG1, reinterpret_cast<void*>(&obj));
        return 0;
    }
    
    test\main.cpp:20:7: style: Unused private function: 'Object::msg1_handler' [unusedPrivateFunction]
     void msg1_handler()
          ^
    test\main.cpp:20:7: note: Unused private function: 'Object::msg1_handler'
     void msg1_handler()
          ^
    test\main.cpp:20:7: note: Unused private function: 'Object::msg1_handler'
     void msg1_handler()
          ^
    test\main.cpp:20:7: style: The function 'msg1_handler' is never used. [unusedFunction]
     void msg1_handler()
          ^
    

    Without parentheses around handler, warnings are not generated:

    #define CASE(msg, handler) \
        case msg: CASE_##msg(handler)
    
     
  • CHR

    CHR - 4 days ago
     

Log in to post a comment.