From cf475fab515bc27fd21326d4b93d5961bc12cc77 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 26 Jun 2020 15:47:59 -0500 Subject: [PATCH] Fix issue 9778: False positive: constParameter when returning non-const reference cast --- lib/checkother.cpp | 4 +++- test/testother.cpp | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index c68c80dfa..b2b912a38 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1368,8 +1368,10 @@ void CheckOther::checkConstVariable() if (Function::returnsReference(function)) { std::vector returns = Function::findReturns(function); if (std::any_of(returns.begin(), returns.end(), [&](const Token* retTok) { - if (retTok->varId() == var->declarationId()) + if (retTok->varId() == var->declarationId()) return true; + while (retTok && retTok->isCast()) + retTok = retTok->astOperand2(); while (Token::simpleMatch(retTok, ".")) retTok = retTok->astOperand2(); const Variable* retVar = getLifetimeVariable(getParentLifetime(retTok)); diff --git a/test/testother.cpp b/test/testother.cpp index 58be8aa99..3b80943d1 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2040,6 +2040,14 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + // #9778 + check("struct A {};\n" + "struct B : A {};\n" + "B& f(A& x) {\n" + " return static_cast(x);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + check("void e();\n" "void g(void);\n" "void h(void);\n"