Skip to content

Commit a040363

Browse files
committed
Fixed cppcheck-opensource#5860: Don't show returnTempReference for calculations on unknown types
1 parent adf38fc commit a040363

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

lib/checkautovariables.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,34 @@ bool CheckAutoVariables::returnTemporary(const Token *tok) const
327327

328328
//---------------------------------------------------------------------------
329329

330+
static bool astHasAutoResult(const Token *tok)
331+
{
332+
if (tok->astOperand1() && !astHasAutoResult(tok->astOperand1()))
333+
return false;
334+
if (tok->astOperand2() && !astHasAutoResult(tok->astOperand2()))
335+
return false;
336+
337+
if (tok->isOp())
338+
return true;
339+
340+
if (tok->isLiteral())
341+
return true;
342+
343+
if (tok->isName()) {
344+
// TODO: check function calls, struct members, arrays, etc also
345+
if (!tok->variable())
346+
return false;
347+
if (tok->variable()->isStlType() && !Token::Match(tok->astParent(), "<<|>>"))
348+
return true;
349+
if (tok->variable()->isClass() || tok->variable()->isPointer() || tok->variable()->isReference()) // TODO: Properly handle pointers/references to classes in symbol database
350+
return false;
351+
352+
return true;
353+
}
354+
355+
return false;
356+
}
357+
330358
void CheckAutoVariables::returnReference()
331359
{
332360
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
@@ -377,7 +405,7 @@ void CheckAutoVariables::returnReference()
377405
}
378406

379407
// Return reference to a literal or the result of a calculation
380-
else if (tok2->astOperand1() && (tok2->astOperand1()->isCalculation() || tok2->next()->isLiteral())) {
408+
else if (tok2->astOperand1() && (tok2->astOperand1()->isCalculation() || tok2->next()->isLiteral()) && astHasAutoResult(tok2->astOperand1())) {
381409
errorReturnTempReference(tok2);
382410
}
383411
}

lib/templatesimplifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok)
254254
// Skip casts
255255
if (tok->str() == "(") {
256256
tok = tok->link();
257-
if(tok)
257+
if (tok)
258258
tok = tok->next();
259259
}
260260

test/testautovariables.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,16 @@ class TestAutoVariables : public TestFixture {
867867
"}");
868868
ASSERT_EQUALS("[test.cpp:2]: (error) Reference to temporary returned.\n", errout.str());
869869

870+
check("std::ostream& operator<<(std::ostream& out, const std::string& path) {\n"
871+
" return out << path;\n"
872+
"}");
873+
ASSERT_EQUALS("", errout.str());
874+
875+
check("Unknown1& operator<<(Unknown1 out, Unknown2 path) {\n"
876+
" return out << path;\n"
877+
"}");
878+
ASSERT_EQUALS("", errout.str());
879+
870880
check("int& a(int b) {\n"
871881
" return 2*(b+1);\n"
872882
"}");

0 commit comments

Comments
 (0)