Menu

Inconsistent assertWithSideEffect

2026-05-28
2026-05-28
  • Nikita Leontiev

    Nikita Leontiev - 2026-05-28

    cppcheck 2.20.0 generates assertWithSideEffect for the following code:
    flag.h

    class Flag
    {
    private:
        long m_flag;
    public:
        Flag() : m_flag(1) {}
        bool is_set();
    };
    

    flag.cpp

    #include <windows.h>
    #include "flag.h"
    
    bool Flag::is_set()
    {
        return InterlockedCompareExchange(&m_flag, 0, 0)?true:false;
    }
    

    main.cpp

    #include <assert.h>
    #include "flag.h"
    
    int main()
    {
        assert(Flag().is_set());
    }
    
    test\main.cpp:6:16: warning: Assert statement calls a function which may have desired side effects: 'is_set'. [assertWithSideEffect]
     assert(Flag().is_set());
                   ^
    

    Moving code to a single source:

    #include <assert.h>
    #include <windows.h>
    
    class Flag
    {
    private:
        long m_flag;
    public:
        Flag() : m_flag(1) {}
        bool is_set()
        {
            return InterlockedCompareExchange(&m_flag, 0, 0)?true:false;
        }
    };
    
    int main()
    {
        assert(Flag().is_set());
    }
    

    assertWithSideEffect is not generated.

     
  • CHR

    CHR - 2026-05-28

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/14796

     

Log in to post a comment.

Auth0 Logo