Menu

False positive Using object that is a temporary. [danglingTemporaryLifetime] or what am I missing ?

2024-03-28
2024-04-08
  • Rob Deckers

    Rob Deckers - 2024-03-28

    I think we have a false positive, but I am not 100% sure.
    We are compiling for C++20

    CppCheck notification

    Cppcheck 2.13.0
    error: Using object that is a temporary. [danglingTemporaryLifetime]
        auto er_result = er({expected});
                             ^
    note: Return object.
        return {.strMsg = message.c_str()
               ^
    note: Passed to reference.
    ErrorInterface::ErrorResponseElement elem(const std::string& message,
                                                                                            ^
    note: Pointer to container is created here.
                .strMsg = message.c_str(),
                                       ^
    note: Passed to reference.
    ErrorInterface::ErrorResponseElement elem(const std::string& message,
                                                                                            ^
    note: Passed to constructor of 'ErrorResponseElement'.
                .strMsg = message.c_str(),
                        ^
    note: Passed to 'elem'.
        const auto expected = elem("Metallica");
                                      ^
    note: Temporary created here.
        const auto expected = elem("Metallica");
                                      ^
    note: Using object that is a temporary.
        auto er_result = er({expected});
    

    This is a simplification of the code, but I hope it provides enough context.

    struct ErrorResponseElement {
        StaticString<55> strMsg;
    };
    
    /** Vector of response elements. */
    using ErrorResponseElements = std::vector<ErrorResponseElement>;
    
    /** Factory function top create a TestErrorResponse. */
    TestErrorResponse er(ErrorResponseElements elements)
    {
        return TestErrorResponse{std::move(elements)};
    }
    
    /** Factory function to create an ErrorResponseElement. */
    ErrorInterface::ErrorResponseElement elem(const std::string& message)
    {
        return {.strMsg = message.c_str()};
    }
    
    TEST_F(DiagnosticsAdapterTest, ReportScoped)
    {
        const auto expected = elem("Metallica");
        auto er_result = er({expected});
    }
    
     

    Last edit: Rob Deckers 2024-03-28
  • Daniel Marjamäki

    hmm I fail to reproduce. I copied your example code to fp1.cpp and used this command:

    $ ../.cppcheck/cppcheck-2.13.0 --enable=all --library=googletest fp1.cpp
    Checking fp1.cpp ...
    fp1.cpp:20:0: style: The function '__DiagnosticsAdapterTest_ReportScoped' is never used. [unusedFunction]
    TEST_F(DiagnosticsAdapterTest, ReportScoped)
    ^
    nofile:0:0: information: Active checkers: 161/592 (use --checkers-report=<filename> to see details) [checkersReport]
    

    Should I tweak my command somehow?

     

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.