Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1712,10 +1712,8 @@ static std::vector<ValueFlow::LifetimeToken> getLifetimeTokens(const Token* tok,
for (ValueFlow::LifetimeToken& lt : getLifetimeTokens(returnTok, escape, errorPath, pred, settings, depth - returns.size())) {
const Token* argvarTok = lt.token;
const Variable* argvar = argvarTok->variable();
if (!argvar)
continue;
const Token* argTok = nullptr;
if (argvar->isArgument() && (argvar->isReference() || argvar->isRValueReference())) {
if (argvar && argvar->isArgument() && (argvar->isReference() || argvar->isRValueReference())) {
const int n = getArgumentPos(argvar, f);
if (n < 0)
return std::vector<ValueFlow::LifetimeToken> {};
Expand Down
29 changes: 29 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4356,6 +4356,35 @@ class TestAutoVariables : public TestFixture {
" *p = ret;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct A {\n"
" int val=42;\n"
" const A& Get() const & {\n"
" return *this;\n"
" }\n"
"};\n"
"const A *ptr;\n"
"int main() {\n"
" ptr = &A().Get();\n"
" return ptr->val;\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:4] -> [test.cpp:9] -> [test.cpp:9] -> [test.cpp:9] -> [test.cpp:10]: (error) Using pointer that is a temporary.\n",
errout_str());

check("struct A {\n"
" int val = 42;\n"
" const A& Get() const {\n"
" return *this;\n"
" }\n"
"};\n"
"int main() {\n"
" const A& r = A().Get();\n"
" return r.val;\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:8] -> [test.cpp:4] -> [test.cpp:8] -> [test.cpp:9]: (error) Using reference to dangling temporary.\n",
errout_str());
}

void invalidLifetime() {
Expand Down