Skip to content

Commit

Permalink
Fix #11790 FP functionConst with template function
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jun 24, 2023
1 parent 04476bc commit 05f68ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2097,6 +2097,17 @@ void CheckClass::checkConst()
};

const bool returnsPtrOrRef = isPointerOrReference(func.retDef, func.tokenDef);

if (Function::returnsPointer(&func, /*unknown*/ true) || func.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()) {
isTemplateArg = true;
break;
}
if (isTemplateArg)
continue;
}

if (func.isOperator()) { // Operator without return type: conversion operator
const std::string& opName = func.tokenDef->str();
Expand Down
15 changes: 15 additions & 0 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class TestClass : public TestFixture {
TEST_CASE(const88);
TEST_CASE(const89);
TEST_CASE(const90);
TEST_CASE(const91);

TEST_CASE(const_handleDefaultParameters);
TEST_CASE(const_passThisToMemberOfOtherClass);
Expand Down Expand Up @@ -6595,6 +6596,20 @@ class TestClass : public TestFixture {
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() {
checkConst("struct Foo {\n"
" void foo1(int i, int j = 0) {\n"
Expand Down

0 comments on commit 05f68ba

Please sign in to comment.