Menu

false positive on uninitialized variable

2019-08-28
2019-08-29
  • Benjamin Combee

    Benjamin Combee - 2019-08-28
    extern bool fn();
    extern bool fn2();
    
    void foo() {
        bool result;
        {
            auto exit = [=]{ };
            result = fn() ? fn() : fn2();
        }
        if (result) {
        }
    }
    

    gives

    /home/bcombee/result_test.cpp:10:9:error:uninitvar:Uninitialized variable: result
    /home/bcombee/result_test.cpp:7:0:style:unreadVariable:Variable 'exit' is assigned a value that is never used.

    however, result is defined since one of the two sides of the conditional will assign to it. Removing the "auto exit" line will result in passing code.

     
  • versat

    versat - 2019-08-29

    Thanks for reporting this.
    The false positive also goes away if i change auto exit = [=]{ }; to for example auto exit = 1;. So it could be that the lambda expression confuses Cppcheck here.

     
  • versat

    versat - 2019-08-29

    I have not found a ticket describing this FP already so i created one: https://trac.cppcheck.net/ticket/9303

     

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.