diff --git a/infer/tests/codetoanalyze/cpp/pulse/temporaries.cpp b/infer/tests/codetoanalyze/cpp/pulse/temporaries.cpp new file mode 100644 index 000000000..8495f987f --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/pulse/temporaries.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2018-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +#include + +namespace temporaries { + +struct A { + int f; +}; + +std::unique_ptr some_f(); + +void FN_call_some_f_deref_bad() { + const A& a_ref = *some_f(); // temporary unique_ptr returned by `some_f` is + // destroyed at the end of the statement + std::cout << a_ref.f; +} + +void call_some_f_ok() { + auto local = some_f(); // ok, as ownership of a temporary unique_ptr is passed + // to `local` + const A& a_ref = *local; + std::cout << a_ref.f; +} + +void call_some_f_copy_object_ok() { + auto a = *some_f().get(); // ok, as value is copied before temporary + // unique_prt is destroyed + std::cout << a.f; +} + +} // namespace temporaries