Skip to content

Commit 152dd9d

Browse files
Fix #13302 false negative: noConstructor (regression) (danmar#7001)
1 parent 07d59e9 commit 152dd9d

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

lib/astutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2551,7 +2551,7 @@ bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Setti
25512551
}
25522552

25532553
if (const Variable* var = tok->variable()) {
2554-
if (tok == var->nameToken() && (!var->isReference() || var->isConst()) && (!var->isClass() || (var->valueType() && var->valueType()->container))) // const ref or passed to (copy) ctor
2554+
if (tok == var->nameToken() && (!var->isReference() || (var->isConst() && var->type() == tok1->type())) && (!var->isClass() || (var->valueType() && var->valueType()->container))) // const ref or passed to (copy) ctor
25552555
return false;
25562556
}
25572557

lib/symboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2380,7 +2380,7 @@ void Variable::evaluate(const Settings& settings)
23802380
std::string strtype = mTypeStartToken->str();
23812381
for (const Token *typeToken = mTypeStartToken; Token::Match(typeToken, "%type% :: %type%"); typeToken = typeToken->tokAt(2))
23822382
strtype += "::" + typeToken->strAt(2);
2383-
setFlag(fIsClass, !lib.podtype(strtype) && !mTypeStartToken->isStandardType() && !isEnumType() && !isPointer() && strtype != "...");
2383+
setFlag(fIsClass, !lib.podtype(strtype) && !mTypeStartToken->isStandardType() && !isEnumType() && !isPointer() && !isReference() && strtype != "...");
23842384
setFlag(fIsStlType, Token::simpleMatch(mTypeStartToken, "std ::"));
23852385
setFlag(fIsStlString, ::isStlStringType(mTypeStartToken));
23862386
setFlag(fIsSmartPointer, mTypeStartToken->isCpp() && lib.isSmartPointer(mTypeStartToken));

test/testconstructors.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class TestConstructors : public TestFixture {
7575
TEST_CASE(simple15); // #8942 multiple arguments, decltype
7676
TEST_CASE(simple16); // copy members with c++11 init
7777
TEST_CASE(simple17); // #10360
78+
TEST_CASE(simple18);
7879

7980
TEST_CASE(noConstructor1);
8081
TEST_CASE(noConstructor2);
@@ -510,6 +511,23 @@ class TestConstructors : public TestFixture {
510511
ASSERT_EQUALS("", errout_str());
511512
}
512513

514+
void simple15() { // #8942
515+
check("class A\n"
516+
"{\n"
517+
"public:\n"
518+
" int member;\n"
519+
"};\n"
520+
"class B\n"
521+
"{\n"
522+
"public:\n"
523+
" B(const decltype(A::member)& x, const decltype(A::member)& y) : x(x), y(y) {}\n"
524+
"private:\n"
525+
" const decltype(A::member)& x;\n"
526+
" const decltype(A::member)& y;\n"
527+
"};\n");
528+
ASSERT_EQUALS("", errout_str());
529+
}
530+
513531
void simple16() {
514532
check("struct S {\n"
515533
" int i{};\n"
@@ -554,21 +572,13 @@ class TestConstructors : public TestFixture {
554572
ASSERT_EQUALS("", errout_str());
555573
}
556574

557-
void simple15() { // #8942
558-
check("class A\n"
559-
"{\n"
560-
"public:\n"
561-
" int member;\n"
562-
"};\n"
563-
"class B\n"
564-
"{\n"
565-
"public:\n"
566-
" B(const decltype(A::member)& x, const decltype(A::member)& y) : x(x), y(y) {}\n"
567-
"private:\n"
568-
" const decltype(A::member)& x;\n"
569-
" const decltype(A::member)& y;\n"
570-
"};\n");
571-
ASSERT_EQUALS("", errout_str());
575+
void simple18() { // #13302
576+
check("struct S {};\n"
577+
"class C1 { S& r; };\n"
578+
"class C2 { S* p; };\n");
579+
ASSERT_EQUALS("[test.cpp:2]: (style) The class 'C1' does not declare a constructor although it has private member variables which likely require initialization.\n"
580+
"[test.cpp:3]: (style) The class 'C2' does not declare a constructor although it has private member variables which likely require initialization.\n",
581+
errout_str());
572582
}
573583

574584
void noConstructor1() {

0 commit comments

Comments
 (0)