Fix issue 9778: False positive: constParameter when returning non-const reference cast

This commit is contained in:
Paul 2020-06-26 15:47:59 -05:00
parent b33326bf51
commit cf475fab51
2 changed files with 11 additions and 1 deletions

View File

@ -1368,8 +1368,10 @@ void CheckOther::checkConstVariable()
if (Function::returnsReference(function)) {
std::vector<const Token*> 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));

View File

@ -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<B&>(x);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void e();\n"
"void g(void);\n"
"void h(void);\n"