Menu

False Positive unreadVariable + RAII lock_guard

2021-09-22
2021-09-24
  • Frank Hackemesser

    Hello,

    the following minimal example code gives the warning:
    "Variable 'lock' is assigned a value that is never used CppCheck(unreadVariable)" warning.
    In my opinion this is a false positive, as this is a valid RAII usage of a mutex lock guard.

    #include <mutex>
    
    std::mutex mutex;
    
    std::lock_guard<std::mutex> lock_raii()
    {
        return std::lock_guard<std::mutex>(mutex);
    }
    
    void test()
    {
        {
            auto lock = lock_raii(); // THIS LINE GIVES THE WARNING
            // some protected code
        }
    }
    

    What do you think?

    Regards,
    Frank

     
  • Daniel Marjamäki

    Thanks for reporting this. yes of course it's a false positive. I have created ticket https://trac.cppcheck.net/ticket/10505

     

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.