Skip to content

Commit 9dc38f8

Browse files
Fix #11790 FP functionConst with template function (#5187)
1 parent a2ee326 commit 9dc38f8

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

lib/checkclass.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,17 @@ void CheckClass::checkConst()
20982098

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

2101+
if (Function::returnsPointer(&func, /*unknown*/ true) || Function::returnsReference(&func, /*unknown*/ true, /*includeRValueRef*/ true)) { // returns const/non-const depending on template arg
2102+
bool isTemplateArg = false;
2103+
for (const Token* tok2 = func.retDef; precedes(tok2, func.token); tok2 = tok2->next())
2104+
if (tok2->isTemplateArg() && tok2->str() == "const") {
2105+
isTemplateArg = true;
2106+
break;
2107+
}
2108+
if (isTemplateArg)
2109+
continue;
2110+
}
2111+
21012112
if (func.isOperator()) { // Operator without return type: conversion operator
21022113
const std::string& opName = func.tokenDef->str();
21032114
if (opName.compare(8, 5, "const") != 0 && (endsWith(opName,'&') || endsWith(opName,'*')))

test/testclass.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ class TestClass : public TestFixture {
180180
TEST_CASE(const88);
181181
TEST_CASE(const89);
182182
TEST_CASE(const90);
183+
TEST_CASE(const91);
183184

184185
TEST_CASE(const_handleDefaultParameters);
185186
TEST_CASE(const_passThisToMemberOfOtherClass);
@@ -6595,6 +6596,20 @@ class TestClass : public TestFixture {
65956596
errout.str());
65966597
}
65976598

6599+
void const91() { // #11790
6600+
checkConst("struct S {\n"
6601+
" char* p;\n"
6602+
" template <typename T>\n"
6603+
" T* get() {\n"
6604+
" return reinterpret_cast<T*>(p);\n"
6605+
" }\n"
6606+
"};\n"
6607+
"const int* f(S& s) {\n"
6608+
" return s.get<const int>();\n"
6609+
"}\n");
6610+
ASSERT_EQUALS("", errout.str());
6611+
}
6612+
65986613
void const_handleDefaultParameters() {
65996614
checkConst("struct Foo {\n"
66006615
" void foo1(int i, int j = 0) {\n"

0 commit comments

Comments
 (0)