Skip to content

Commit f64500e

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 8a8f0f9 commit f64500e

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
@@ -1783,7 +1783,7 @@ void CheckOtherImpl::checkConstVariable()
17831783
continue;
17841784
if (var->isConst())
17851785
continue;
1786-
if (Token::Match(var->nameToken(), "%name% ) (")) // reference to function
1786+
if (Tokenizer::isFunctionPointer(var->nameToken())) // reference to function
17871787
continue;
17881788
const Scope* scope = var->scope();
17891789
if (!scope)

‎lib/symboldatabase.cpp‎

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

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

‎test/testsymboldatabase.cpp‎

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

0 commit comments

Comments
 (0)