diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 526555b40..a26b959fc 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -392,6 +392,13 @@ void CheckAutoVariables::errorUselessAssignmentPtrArg(const Token *tok) "Assignment of function parameter has no effect outside the function. Did you forget dereferencing it?", CWE398, Certainty::normal); } +bool CheckAutoVariables::diag(const Token* tokvalue) +{ + if (!tokvalue) + return true; + return !mDiagDanglingTemp.insert(tokvalue).second; +} + //--------------------------------------------------------------------------- static bool isInScope(const Token * tok, const Scope * scope) @@ -603,7 +610,8 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token break; } else if (!tokvalue->variable() && isDeadTemporary(mTokenizer->isCPP(), tokvalue, tok, &mSettings->library)) { - errorDanglingTemporaryLifetime(tok, &val, tokvalue); + if (!diag(tokvalue)) + errorDanglingTemporaryLifetime(tok, &val, tokvalue); break; } } diff --git a/lib/checkautovariables.h b/lib/checkautovariables.h index fd0870c3f..7b58db07a 100644 --- a/lib/checkautovariables.h +++ b/lib/checkautovariables.h @@ -27,6 +27,7 @@ #include "errortypes.h" #include +#include class Settings; class Token; @@ -127,6 +128,11 @@ private: "- suspicious assignment of pointer argument\n" "- useless assignment of function argument\n"; } + + /** returns true if tokvalue has already been diagnosed */ + bool diag(const Token* tokvalue); + + std::set mDiagDanglingTemp; }; /// @} //--------------------------------------------------------------------------- diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 7a89892e8..367a12f32 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -2744,6 +2744,12 @@ private: " return sv;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" // #10993 + " std::string_view v = std::string();\n" + " v.data();\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2] -> [test.cpp:3]: (error) Using object that is a temporary.\n", errout.str()); } void danglingLifetimeUniquePtr()