Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1825,18 +1825,31 @@ void CheckOtherImpl::checkConstVariable()
if (function && (Function::returnsReference(function) || Function::returnsPointer(function)) && !Function::returnsConst(function)) {
std::vector<const Token*> returns = Function::findReturns(function);
if (std::any_of(returns.cbegin(), returns.cend(), [&](const Token* retTok) {
if (retTok->varId() == var->declarationId())
return true;
while (retTok && retTok->isCast())
retTok = retTok->astOperand2() ? retTok->astOperand2() : retTok->astOperand1();
while (Token::simpleMatch(retTok, "."))
retTok = retTok->astOperand2();
if (Token::simpleMatch(retTok, "&"))
retTok = retTok->astOperand1();
ValueFlow::Value ltVal = ValueFlow::getLifetimeObjValue(retTok);
if (ltVal.isLifetimeValue() && ltVal.tokvalue->varId() == var->declarationId())
return true;
return ValueFlow::hasLifetimeToken(getParentLifetime(retTok), var->nameToken(), mSettings);
bool result = false;
visitAstNodes(retTok, [&result, &var, this](const Token* tok) {
if (tok->varId() == var->declarationId()) {
result = true;
return ChildrenToVisit::done;
}
ValueFlow::Value ltVal = ValueFlow::getLifetimeObjValue(tok);
if ((ltVal.isLifetimeValue() && ltVal.tokvalue->varId() == var->declarationId()) ||
ValueFlow::hasLifetimeToken(getParentLifetime(tok), var->nameToken(), mSettings)) {
result = true;
return ChildrenToVisit::done;
}
if (tok->isCast())
return tok->astOperand2() ? ChildrenToVisit::op2 : ChildrenToVisit::op1;
if (tok->str() == ".")
return ChildrenToVisit::op2;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

This only follows the member side of ., so a ternary in the object expression is not reached. int& g(S& s, S& t, bool b) { return (b ? s : t).a; } still gives constParameterReference for both s and t, and both are wrong because the function returns a non-const int&. Visiting op1 of . as well (or both operands) would cover this case too.

if (tok->str() == "&")
return ChildrenToVisit::op1;
if (tok->str() == "?")
return ChildrenToVisit::op2;
if (tok->str() == ":")
return ChildrenToVisit::op1_and_op2;
return ChildrenToVisit::none;
});
return result;
}))
continue;
}
Expand Down
11 changes: 11 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4225,6 +4225,17 @@ class TestOther : public TestFixture {
" int select(DS &ds) { return select(ds, [](int) {}); }\n"
"};\n");
ASSERT_EQUALS("", errout_str());

check("struct S {\n" // #15087
" int a, b;\n"
"};\n"
"int& f(S& s, bool b) {\n"
" return b ? s.a : s.b;\n"
"}\n"
"int* g(U& u) {\n"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

U is not declared here, and this function gives no warning on base either, so this part of the test does not cover the change. Consider replacing it with a case that only passes with the PR, e.g. int* g(S& s, bool b) { return b ? &s.a : &s.b; } or int& h(S& s, S& t, bool b) { return b ? s.a : t.a; }.

" return &(u.i);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void constParameterCallback() {
Expand Down
Loading