Fix #10311 FP functionConst (#4012)

* Fix #10311 FP functionConst

* Format

* Format
This commit is contained in:
chrchr-github 2022-04-13 12:25:36 +02:00 committed by GitHub
parent 55cb396d18
commit f323e8a6c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 14 deletions

View File

@ -1988,18 +1988,32 @@ void CheckClass::checkConst()
// don't warn when returning non-const pointer/reference // don't warn when returning non-const pointer/reference
{ {
bool isPointerOrReference = false; auto isPointerOrReference = [this](const Token* start, const Token* end) -> bool {
for (const Token *typeToken = func.retDef; typeToken; typeToken = typeToken->next()) { bool inTemplArgList = false, isConstTemplArg = false;
if (Token::Match(typeToken, "(|{|;")) for (const Token* tok = start; tok != end; tok = tok->next()) {
break; if (tok->str() == "{") // end of trailing return type
if (!isPointerOrReference && typeToken->str() == "const") return false;
break; if (tok->str() == "<") {
if (Token::Match(typeToken, "*|&")) { if (!tok->link())
isPointerOrReference = true; mSymbolDatabase->debugMessage(tok, "debug", "CheckClass::checkConst found unlinked template argument list '" + tok->expressionString() + "'.");
break; inTemplArgList = true;
}
else if (tok->str() == ">") {
inTemplArgList = false;
isConstTemplArg = false;
}
else if (tok->str() == "const") {
if (!inTemplArgList)
return false;
isConstTemplArg = true;
}
else if (!isConstTemplArg && Token::Match(tok, "*|&"))
return true;
} }
} return false;
if (isPointerOrReference) };
if (isPointerOrReference(func.retDef, func.tokenDef))
continue; continue;
} }

View File

@ -193,7 +193,7 @@ private:
TEST_CASE(const74); // ticket #10671 TEST_CASE(const74); // ticket #10671
TEST_CASE(const75); // ticket #10065 TEST_CASE(const75); // ticket #10065
TEST_CASE(const76); // ticket #10825 TEST_CASE(const76); // ticket #10825
TEST_CASE(const77); // ticket #10307 TEST_CASE(const77); // ticket #10307, #10311
TEST_CASE(const78); // ticket #10315 TEST_CASE(const78); // ticket #10315
TEST_CASE(const_handleDefaultParameters); TEST_CASE(const_handleDefaultParameters);
TEST_CASE(const_passThisToMemberOfOtherClass); TEST_CASE(const_passThisToMemberOfOtherClass);
@ -6033,13 +6033,19 @@ private:
errout.str()); errout.str());
} }
void const77() { // #10307 void const77() {
checkConst("template <typename T>\n" checkConst("template <typename T>\n" // #10307
"struct S {\n" "struct S {\n"
" std::vector<T> const* f() const { return p; }\n" " std::vector<T> const* f() const { return p; }\n"
" std::vector<T> const* p;\n" " std::vector<T> const* p;\n"
"};\n"); "};\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkConst("struct S {\n" // #10311
" std::vector<const int*> v;\n"
" std::vector<const int*>& f() { return v; }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
} }
void const78() { // #10315 void const78() { // #10315