Menu

Inconsistent constVariablePointer

2026-03-04
2026-03-04
  • Nikita Leontiev

    Nikita Leontiev - 2026-03-04

    cppcheck 2.20.0 generates constVariablePointer for the following code:

    #include <windows.h>
    
    class CColor
    {
    private:
        int m_color;
    public:
        CColor() : m_color(1) {}
        int get_color() const { return m_color; }
    };
    
    class CWrapper
    {
    private:
        CColor *m_color;
    public:
        explicit CWrapper(CColor* color) : m_color(color) {}
        CColor* get_color() const { return m_color; }
    };
    
    int test(const CWrapper & wrap)
    {
        CColor *color = wrap.get_color();
        return GetSysColor(color?color -> get_color():1);
    }
    
    cppcheck.exe --enable=all --library=windows .
    
    test\main.cpp:23:10: style: Variable 'color' can be declared as pointer to const [constVariablePointer]
     CColor *color = wrap.get_color();
             ^
    

    After removing --library=windows arg, warning is not generated.
    With the slightly modified test function, warning appears again:

    int test(const CWrapper & wrap)
    {
        ...
        return GetSysColor(color -> get_color());
    }
    
     
  • CHR

    CHR - 2026-03-04

    So you would like to see the warning without --library=windows, right?
    I have created this ticket: https://trac.cppcheck.net/ticket/14559

     

    Last edit: CHR 2026-03-04
  • Nikita Leontiev

    Nikita Leontiev - 2026-03-04

    So you would like to see the warning without --library=windows, right?

    Yes

     

Log in to post a comment.

MongoDB Logo MongoDB