diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -6591,7 +6591,7 @@ OO == OverloadedOperatorKind::OO_Star; } return llvm::StringSwitch(Callee->getName()) - .Cases("front", "back", "at", true) + .Cases("front", "back", "at", "top", "value", true) .Default(false); } return false; diff --git a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp --- a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp +++ b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp @@ -168,7 +168,15 @@ struct optional { optional(); optional(const T&); - T &operator*(); + T &operator*() &; + T &&operator*() &&; + T &value() &; + T &&value() &&; +}; + +template +struct stack { + T &top(); }; } @@ -186,6 +194,16 @@ return s.c_str(); // expected-warning {{address of stack memory associated with local variable 's' returned}} } +int &danglingRawPtrFromLocal2() { + std::optional o; + return o.value(); // expected-warning {{reference to stack memory associated with local variable 'o' returned}} +} + +int &danglingRawPtrFromLocal3() { + std::optional o; + return *o; // expected-warning {{reference to stack memory associated with local variable 'o' returned}} +} + const char *danglingRawPtrFromTemp() { return std::basic_string().c_str(); // expected-warning {{returning address of local temporary object}} } @@ -201,9 +219,10 @@ } void danglingReferenceFromTempOwner() { - int &r = *std::optional(); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} - int &r2 = *std::optional(5); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} - int &r3 = std::vector().at(3); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} + int &&r = *std::optional(); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} + int &&r2 = *std::optional(5); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} + int &&r3 = std::optional(5).value(); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} + int &r4 = std::vector().at(3); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} } std::vector getTempVec();