42 lines · cpp
1// RUN: %clang_analyze_cc1 %s -verify -analyzer-checker=core2 3#include "Inputs/system-header-simulator-cxx.h"4 5 6namespace GH94193 {7template<typename T> class optional {8 union {9 char x;10 T uvalue;11 };12 bool holds_value = false;13public:14 optional() = default;15 optional(const optional&) = delete;16 optional(optional&&) = delete;17 template <typename U = T> explicit optional(U&& value) : holds_value(true) {18 new (static_cast<void*>(std::addressof(uvalue))) T(std::forward<U>(value));19 }20 optional& operator=(const optional&) = delete;21 optional& operator=(optional&&) = delete;22 explicit operator bool() const {23 return holds_value;24 }25 T& unwrap() & {26 return uvalue; // no-warning: returns a valid value27 }28};29 30int top1(int x) {31 optional<int> opt{x}; // note: Ctor was inlined.32 return opt.unwrap(); // no-warning: returns a valid value33}34 35std::string *top2() {36 std::string a = "123";37 // expected-warning@+2 {{address of stack memory associated with local variable 'a' returned}} diagnosed by -Wreturn-stack-address38 // expected-warning@+1 {{Address of stack memory associated with local variable 'a' returned to caller [core.StackAddressEscape]}}39 return std::addressof(a);40}41} // namespace GH9419342