Skip to content
Open
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
5 changes: 4 additions & 1 deletion lib/checkautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,10 @@ void CheckAutoVariablesImpl::checkVarLifetimeScope(const Token * start, const To
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(tok, mSettings, true)) {
if (!printInconclusive && lt.inconclusive)
continue;
const Variable* var = lt.token->variable();
const Token* varTok = lt.token;
while (varTok->isCast())
varTok = varTok->isBinaryOp() ? varTok->astOperand2() : varTok->astOperand1();
const Variable* var = varTok->variable();
if (var && !var->isGlobal() && !var->isStatic() && !var->isReference() && !var->isRValueReference() &&
isInScope(var->nameToken(), tok->scope())) {
errorReturnReference(tok, lt.errorPath, lt.inconclusive);
Expand Down
9 changes: 9 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class TestAutoVariables : public TestFixture {
TEST_CASE(returnReference28);
TEST_CASE(returnReference29);
TEST_CASE(returnReference30); // #15094
TEST_CASE(returnReference31);
TEST_CASE(returnReferenceFunction);
TEST_CASE(returnReferenceContainer);
TEST_CASE(returnReferenceLiteral);
Expand Down Expand Up @@ -1790,6 +1791,14 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void returnReference31() // #15291
{
check("int& g(int i) {\n"
" return (int&)i;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:12]: (error) Reference to local variable returned. [returnReference]\n", errout_str());
}

void returnReferenceFunction() {
check("int& f(int& a) {\n"
" return a;\n"
Expand Down
Loading