Fix #11790 FP functionConst with template function (#5187)

This commit is contained in:
chrchr-github 2023-06-25 20:38:54 +02:00 committed by GitHub
parent a2ee32695f
commit 9dc38f80c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -2098,6 +2098,17 @@ void CheckClass::checkConst()
const bool returnsPtrOrRef = isPointerOrReference(func.retDef, func.tokenDef); const bool returnsPtrOrRef = isPointerOrReference(func.retDef, func.tokenDef);
if (Function::returnsPointer(&func, /*unknown*/ true) || Function::returnsReference(&func, /*unknown*/ true, /*includeRValueRef*/ true)) { // returns const/non-const depending on template arg
bool isTemplateArg = false;
for (const Token* tok2 = func.retDef; precedes(tok2, func.token); tok2 = tok2->next())
if (tok2->isTemplateArg() && tok2->str() == "const") {
isTemplateArg = true;
break;
}
if (isTemplateArg)
continue;
}
if (func.isOperator()) { // Operator without return type: conversion operator if (func.isOperator()) { // Operator without return type: conversion operator
const std::string& opName = func.tokenDef->str(); const std::string& opName = func.tokenDef->str();
if (opName.compare(8, 5, "const") != 0 && (endsWith(opName,'&') || endsWith(opName,'*'))) if (opName.compare(8, 5, "const") != 0 && (endsWith(opName,'&') || endsWith(opName,'*')))

View File

@ -180,6 +180,7 @@ private:
TEST_CASE(const88); TEST_CASE(const88);
TEST_CASE(const89); TEST_CASE(const89);
TEST_CASE(const90); TEST_CASE(const90);
TEST_CASE(const91);
TEST_CASE(const_handleDefaultParameters); TEST_CASE(const_handleDefaultParameters);
TEST_CASE(const_passThisToMemberOfOtherClass); TEST_CASE(const_passThisToMemberOfOtherClass);
@ -6595,6 +6596,20 @@ private:
errout.str()); errout.str());
} }
void const91() { // #11790
checkConst("struct S {\n"
" char* p;\n"
" template <typename T>\n"
" T* get() {\n"
" return reinterpret_cast<T*>(p);\n"
" }\n"
"};\n"
"const int* f(S& s) {\n"
" return s.get<const int>();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void const_handleDefaultParameters() { void const_handleDefaultParameters() {
checkConst("struct Foo {\n" checkConst("struct Foo {\n"
" void foo1(int i, int j = 0) {\n" " void foo1(int i, int j = 0) {\n"