Index: clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp =================================================================== --- clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp +++ clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp @@ -132,10 +132,11 @@ case ConstructionContext::SimpleConstructorInitializerKind: { const auto *ICC = cast(CC); const auto *Init = ICC->getCXXCtorInitializer(); - assert(Init->isAnyMemberInitializer()); const CXXMethodDecl *CurCtor = cast(LCtx->getDecl()); Loc ThisPtr = SVB.getCXXThis(CurCtor, LCtx->getStackFrame()); SVal ThisVal = State->getSVal(ThisPtr); + if (Init->isBaseInitializer() || Init->isDelegatingInitializer()) + return ThisVal; const ValueDecl *Field; SVal FieldVal; @@ -364,6 +365,8 @@ case ConstructionContext::CXX17ElidedCopyConstructorInitializerKind: case ConstructionContext::SimpleConstructorInitializerKind: { const auto *ICC = cast(CC); + const auto *Init = ICC->getCXXCtorInitializer(); + assert(Init->isAnyMemberInitializer()); return addObjectUnderConstruction(State, ICC->getCXXCtorInitializer(), LCtx, V); } Index: clang/unittests/StaticAnalyzer/TestReturnValueUnderConstruction.cpp =================================================================== --- clang/unittests/StaticAnalyzer/TestReturnValueUnderConstruction.cpp +++ clang/unittests/StaticAnalyzer/TestReturnValueUnderConstruction.cpp @@ -51,22 +51,65 @@ TEST(TestReturnValueUnderConstructionChecker, ReturnValueUnderConstructionChecker) { EXPECT_TRUE(runCheckerOnCode( - R"(class C { - public: - C(int nn): n(nn) {} - virtual ~C() {} - private: - int n; - }; - - C returnC(int m) { - C c(m); - return c; - } - - void foo() { - C c = returnC(1); - })")); + R"(class C { + public: + C(int nn): n(nn) {} + virtual ~C() {} + private: + int n; + }; + + C returnC(int m) { + C c(m); + return c; + } + + void foo() { + C c = returnC(1); + })")); + + EXPECT_TRUE(runCheckerOnCode( + R"(class C { + public: + C(int nn): n(nn) {} + explicit C(): C(0) {} + virtual ~C() {} + private: + int n; + }; + + C returnC() { + C c; + return c; + } + + void foo() { + C c = returnC(); + })")); + + EXPECT_TRUE(runCheckerOnCode( + R"(class C { + public: + C(int nn): n(nn) {} + virtual ~C() {} + private: + int n; + }; + + class D: public C { + public: + D(int nn): C(nn) {} + virtual ~D() {} + }; + + D returnD(int m) { + D d(m); + return d; + } + + void foo() { + D d = returnD(1); + })")); } } // namespace