Fix #11625 FP constVariable when returning by reference (#4921)

This commit is contained in:
chrchr-github 2023-03-30 07:24:36 +02:00 committed by GitHub
parent 3836367d95
commit 4d72e0f5b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -1548,8 +1548,11 @@ void CheckOther::checkConstPointer()
const Token* const gparent = parent->astParent();
if (Token::Match(gparent, "%cop%") && !gparent->isUnaryOp("&") && !gparent->isUnaryOp("*"))
continue;
if (Token::simpleMatch(gparent, "return"))
continue;
if (Token::simpleMatch(gparent, "return")) {
const Function* function = gparent->scope()->function;
if (function && (!Function::returnsReference(function) || Function::returnsConst(function)))
continue;
}
else if (Token::Match(gparent, "%assign%") && parent == gparent->astOperand2()) {
bool takingRef = false, nonConstPtrAssignment = false;
const Token *lhs = gparent->astOperand1();

View File

@ -3066,6 +3066,13 @@ private:
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("int& g(int* p, int& r) {\n" // #11625
" if (p)\n"
" return *p;\n"
" return r;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void constParameterCallback() {