Skip to content

Commit ed477ce

Browse files
committed
Fixed false negative mentioned in danmar#4354.
1 parent 641ac5c commit ed477ce

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/checkclass.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,15 @@ void CheckClass::copyconstructors()
200200

201201
for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
202202
if (func->type == Function::eConstructor && func->functionScope) {
203-
for (const Token* tok = func->functionScope->classStart; tok!=func->functionScope->classEnd; tok=tok->next()) {
203+
const Token* tok = func->functionScope->classDef->linkAt(1);
204+
for (const Token* const end = func->functionScope->classStart; tok != end; tok = tok->next()) {
205+
if (Token::Match(tok, "%var% ( new|malloc|g_malloc|g_try_malloc|realloc|g_realloc|g_try_realloc")) {
206+
const Variable* var = tok->variable();
207+
if (var && var->isPointer() && var->scope() == &*scope)
208+
allocatedVars[tok->varId()] = tok;
209+
}
210+
}
211+
for (const Token* const end = func->functionScope->classEnd; tok != end; tok = tok->next()) {
204212
if (Token::Match(tok, "%var% = new|malloc|g_malloc|g_try_malloc|realloc|g_realloc|g_try_realloc")) {
205213
const Variable* var = tok->variable();
206214
if (var && var->isPointer() && var->scope() == &*scope)

test/testclass.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class TestClass : public TestFixture {
374374
" p = malloc(100);\n"
375375
" }\n"
376376
"};");
377-
TODO_ASSERT_EQUALS("[test.cpp:2]: (style) 'class F' does not have a copy constructor which is required since the class contains a pointer to allocated memory.\n", "", errout.str());
377+
TODO_ASSERT_EQUALS("[test.cpp:2]: (style) 'class F' does not have a copy constructor which is recommended since the class contains a pointer to allocated memory.\n", "", errout.str());
378378

379379
checkCopyConstructor("class F {\n"
380380
" char *p;\n"
@@ -384,6 +384,12 @@ class TestClass : public TestFixture {
384384
" F(F& f);\n" // non-copyable
385385
"};");
386386
ASSERT_EQUALS("", errout.str());
387+
388+
checkCopyConstructor("class F {\n"
389+
" char *p;\n"
390+
" F() : p(malloc(100)) {}\n"
391+
"};");
392+
ASSERT_EQUALS("[test.cpp:1]: (style) 'class F' does not have a copy constructor which is recommended since the class contains a pointer to allocated memory.\n", errout.str());
387393
}
388394

389395

0 commit comments

Comments
 (0)