Why is this code not triggering a warning with `-Wlifetime`?

Viewed 225

I was reading the lifetime safety core guidelines paper and was eager to try the -Wlifetime flag in practice. For starters, I wrote the following simple example:

class Wrapper
{
public:
    Wrapper(std::string s)
        : str_(std::move(s))
    { }

    const std::string& GetString() const
    {
        return str_;
    }

private:
    std::string str_;
};

const std::string& example() {
    return Wrapper("abc").GetString();
}

It does emit some warnings, but apparently they arise within stdlib and are not related to my code:

In file included from <built-in>:1:
/opt/compiler-explorer/clang-cppx-trunk/include/experimental/meta:3233:38: warning: returning a pointer with points-to set (*(*this).m_first) where points-to set ((null), **this) is expected [-Wlifetime]
  consteval iterator begin() const { return m_first; }
                                     ^~~~~~~~~~~~~~
/opt/compiler-explorer/clang-cppx-trunk/include/experimental/meta:3235:36: warning: returning a pointer with points-to set (*(*this).m_last) where points-to set ((null), **this) is expected [-Wlifetime]
  consteval iterator end() const { return m_last; }
                                   ^~~~~~~~~~~~~
/opt/compiler-explorer/clang-cppx-trunk/include/experimental/meta:3263:3: warning: returning a dangling pointer [-Wlifetime]
  return range(reflection, pred);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-cppx-trunk/include/experimental/meta:3276:10: note: in instantiation of function template specialization 'std::experimental::meta::v1::members_of<std::experimental::meta::v1::detail::always_true_fn>' requested here
  return members_of(reflection, detail::always_true);
         ^
/opt/compiler-explorer/clang-cppx-trunk/include/experimental/meta:3263:10: note: it was never initialized here
  return range(reflection, pred);
         ^~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-cppx-trunk/include/experimental/meta:3281:3: warning: returning a dangling pointer [-Wlifetime]
  return param_range(reflection);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-cppx-trunk/include/experimental/meta:3281:10: note: it was never initialized here
  return param_range(reflection);
         ^~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-cppx-trunk/include/experimental/meta:3289:3: warning: returning a dangling pointer [-Wlifetime]
  return __reflect(detail::query_get_begin, reflection);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-cppx-trunk/include/experimental/meta:3289:10: note: it was never initialized here
  return __reflect(detail::query_get_begin, reflection);

(these warnings remain no matter what code I write, so I consider them irrelevant)

I am surprised by the lack of warnings in my code. As far as I understood the paper, the returned string should have a lifetime of Wrapper, since it is an implicit argument to Wrapper::GetString (1.1.4: "by default we assume that a function returns values that are derived from it's arguments"). I even added [[gsl::Owner(std::string)]] to the class definition, but with no luck.

Godbolt example with -Wlifetime enabled

Why does my code produce no warnings and is there a way to make the static analysis work in this case?

0 Answers
Related