Menu

unreadVariable false positive

2025-10-10
2025-10-11
  • Nikita Leontiev

    Nikita Leontiev - 2025-10-10

    cppcheck 2.18.0 generates unreadVariable false positive for the following code:

    #include <inttypes.h>
    #include <stdio.h>
    
    void func()
    {
        bool flag = false;
        for (uint8_t i = 0; i < 10; ++i)
        {
            if (i % 2 == 0)
                flag = true;
            else
                flag = false;
            if (flag)
                printf("\n");
        }
    }
    
    test\main.cpp:6:12: style: Variable 'flag' is assigned a value that is never used. [unreadVariable]
     bool flag = false;
               ^
    

    unreadVariable doesn't appear on slightly modified code:

    void func()
    {
        bool flag = false;
        for (uint8_t i = 0; i < 10; ++i)
        {
            if (i % 2 == 0)
            {
                if (i < 5)
                    flag = true;
            }
            else
                flag = false;
            if (flag)
                printf("\n");
        }
    }
    
     
  • CHR

    CHR - 2025-10-11

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

     

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.