Menu

False positive: Using reference to dangling temporary. [danglingTempReference]

2022-07-14
2022-07-14
  • Maxim Yanchenko

    Maxim Yanchenko - 2022-07-14

    Absolutely valid C++ code:

    $  cat xxx.cpp
    struct S
    {
       void f();
    };
    
    S* g();
    
    void h()
    {
      const auto& s = g();
      if (s != nullptr)
        s->f();
    }
    

    all cppcheck messages are bogus:

    $  cppcheck/2.8.0/bin/cppcheck xxx.cpp
    Checking xxx.cpp ...
    xxx.cpp:11:7: error: Using reference to dangling temporary. [danglingTempReference]
      if (s != nullptr)
          ^
    xxx.cpp:10:17: note: Assigned to reference.
      const auto& s = g();
                    ^
    xxx.cpp:11:7: note: Using reference to dangling temporary.
      if (s != nullptr)
          ^
    xxx.cpp:12:5: error: Using reference to dangling temporary. [danglingTempReference]
        s->f();
        ^
    xxx.cpp:10:17: note: Assigned to reference.
      const auto& s = g();
                    ^
    xxx.cpp:12:5: note: Using reference to dangling temporary.
        s->f();
        ^
    
     
  • CHR

    CHR - 2022-07-14

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

     

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.