Skip to content

Commit 4d22ada

Browse files
committed
Fixed danmar#5839 (False positive: Function can be const, if this is passed to functor)
1 parent ce7bfba commit 4d22ada

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/checkclass.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,10 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
19781978
memberAccessed = true;
19791979
}
19801980
// Member variable given as parameter
1981-
for (const Token* tok2 = tok1->tokAt(2); tok2 && tok2 != tok1->next()->link(); tok2 = tok2->next()) {
1981+
const Token *lpar = tok1->next();
1982+
if (Token::simpleMatch(lpar, "( ) ("))
1983+
lpar = lpar->tokAt(2);
1984+
for (const Token* tok2 = lpar->next(); tok2 && tok2 != tok1->next()->link(); tok2 = tok2->next()) {
19821985
if (tok2->str() == "(")
19831986
tok2 = tok2->link();
19841987
else if (tok2->isName() && isMemberVar(scope, tok2)) {

test/testclass.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5135,6 +5135,17 @@ class TestClass : public TestFixture {
51355135
" }\n"
51365136
"};");
51375137
ASSERT_EQUALS("[test.cpp:2]: (performance, inconclusive) Technically the member function 'Foo::foo' can be static.\n", errout.str());
5138+
5139+
checkConst("struct A;\n" // #5839 - operator()
5140+
"struct B {\n"
5141+
" void operator()(A *a);\n"
5142+
"};\n"
5143+
"struct A {\n"
5144+
" void dostuff() {\n"
5145+
" B()(this);\n"
5146+
" }\n"
5147+
"};");
5148+
ASSERT_EQUALS("", errout.str());
51385149
}
51395150

51405151
void assigningPointerToPointerIsNotAConstOperation() {

0 commit comments

Comments
 (0)