Merge pull request #2698 from pfultz2/const-param-casts

Fix issue 9778: False positive: constParameter when returning non-const reference cast
This commit is contained in:
Daniel Marjamäki 2020-06-27 06:45:30 +02:00 committed by GitHub
commit 72bdeb9307
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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"