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(); const Token* const gparent = parent->astParent();
if (Token::Match(gparent, "%cop%") && !gparent->isUnaryOp("&") && !gparent->isUnaryOp("*")) if (Token::Match(gparent, "%cop%") && !gparent->isUnaryOp("&") && !gparent->isUnaryOp("*"))
continue; continue;
if (Token::simpleMatch(gparent, "return")) if (Token::simpleMatch(gparent, "return")) {
const Function* function = gparent->scope()->function;
if (function && (!Function::returnsReference(function) || Function::returnsConst(function)))
continue; continue;
}
else if (Token::Match(gparent, "%assign%") && parent == gparent->astOperand2()) { else if (Token::Match(gparent, "%assign%") && parent == gparent->astOperand2()) {
bool takingRef = false, nonConstPtrAssignment = false; bool takingRef = false, nonConstPtrAssignment = false;
const Token *lhs = gparent->astOperand1(); const Token *lhs = gparent->astOperand1();

View File

@ -3066,6 +3066,13 @@ private:
" }\n" " }\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); 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() { void constParameterCallback() {