Fixed #8674 (False positive: Method that returns const should not be const)

This commit is contained in:
Daniel Marjamäki 2018-08-07 18:06:14 +02:00
parent b66af545ca
commit 78df7f98dd
2 changed files with 16 additions and 0 deletions

View File

@ -1805,6 +1805,9 @@ void CheckClass::checkConst()
const std::string& opName = func->tokenDef->str();
if (opName.compare(8, 5, "const") != 0 && (endsWith(opName,'&') || endsWith(opName,'*')))
continue;
} else if (Token::simpleMatch(func->retDef, "std :: shared_ptr <")) {
// Don't warn if a std::shared_ptr is returned
continue;
} else {
// don't warn for unknown types..
// LPVOID, HDC, etc

View File

@ -195,6 +195,7 @@ private:
TEST_CASE(constUnion); // ticket #2111 - fp when there is a union
TEST_CASE(constArrayOperator); // #4406
TEST_CASE(constRangeBasedFor); // #5514
TEST_CASE(const_shared_ptr);
TEST_CASE(initializerListOrder);
TEST_CASE(initializerListUsage);
@ -6199,6 +6200,18 @@ private:
ASSERT_EQUALS("[test.cpp:8]: (style, inconclusive) Technically the member function 'Fred::f2' can be const.\n", errout.str());
}
void const_shared_ptr() { // #8674
checkConst("class Fred {\n"
"public:\n"
" std::shared_ptr<Data> getData();\n"
"private:\n"
" std::shared_ptr<Data> data;\n"
"};\n"
"\n"
"std::shared_ptr<Data> Fred::getData() { return data; }");
ASSERT_EQUALS("", errout.str());
}
void checkInitializerListOrder(const char code[]) {
// Clear the error log
errout.str("");