Skip to content

Commit 7e48369

Browse files
autoantwortclaude
andcommitted
Don't treat a reference to a function as copy or move constructor parameter
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent dbfcc30 commit 7e48369

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎lib/checkother.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1768,7 +1768,7 @@ void CheckOtherImpl::checkConstVariable()
17681768
continue;
17691769
if (var->isConst())
17701770
continue;
1771-
if (Token::Match(var->nameToken(), "%name% ) (")) // reference to function
1771+
if (Tokenizer::isFunctionPointer(var->nameToken())) // reference to function
17721772
continue;
17731773
const Scope* scope = var->scope();
17741774
if (!scope)

‎lib/symboldatabase.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,8 @@ void SymbolDatabase::createSymbolDatabaseCopyAndMoveConstructors()
916916
continue;
917917

918918
const Variable* firstArg = func->getArgumentVar(0);
919-
if (firstArg->type() == scope.definedType) {
919+
// skip references to functions: Foo(Foo& (&f)())
920+
if (firstArg->type() == scope.definedType && !Token::Match(firstArg->typeEndToken(), "&|&& %name%| ) (")) {
920921
if (firstArg->isRValueReference())
921922
func->type = FunctionType::eMoveConstructor;
922923
else if (firstArg->isReference() && !firstArg->isPointer())

‎test/testsymboldatabase.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,6 +2475,26 @@ class TestSymbolDatabase : public TestFixture {
24752475
const Function* ctor = tokenizer.tokens()->tokAt(3)->function();
24762476
ASSERT(db && ctor && ctor->type == FunctionType::eConstructor);
24772477
}
2478+
{
2479+
GET_SYMBOL_DB("class Foo { Foo(Foo& (&f)()); };\n");
2480+
const Function* ctor = tokenizer.tokens()->tokAt(3)->function();
2481+
ASSERT(db && ctor && ctor->type == FunctionType::eConstructor);
2482+
}
2483+
{
2484+
GET_SYMBOL_DB("class Foo { Foo(Foo&& (&f)()); };\n");
2485+
const Function* ctor = tokenizer.tokens()->tokAt(3)->function();
2486+
ASSERT(db && ctor && ctor->type == FunctionType::eConstructor);
2487+
}
2488+
{
2489+
GET_SYMBOL_DB("class Foo { Foo(Foo (&f)()); };\n");
2490+
const Function* ctor = tokenizer.tokens()->tokAt(3)->function();
2491+
ASSERT(db && ctor && ctor->type == FunctionType::eConstructor);
2492+
}
2493+
{
2494+
GET_SYMBOL_DB("class Foo { Foo(Foo& (&)()); };\n");
2495+
const Function* ctor = tokenizer.tokens()->tokAt(3)->function();
2496+
ASSERT(db && ctor && ctor->type == FunctionType::eConstructor);
2497+
}
24782498
{
24792499
GET_SYMBOL_DB("void f() { extern void f(); }\n");
24802500
ASSERT(db && db->scopeList.size() == 2);

0 commit comments

Comments
 (0)