is there any way to detect temporary referencing with clang?

or is that already covered by


#include <cstdint>
#include <cstdio>

class user_t
   user_t(const int64_t& p_a):

   const int64_t& m_a; // someone added evil & while refactoring

   int64_t use_it() const
     return m_a;


//example result:
// X Release --> a: 8, user_a: 75769734271860736 FAIL
// X Debug --> a: 8, user_a: 7 FAIL
// - Release --> a: 8, user_a: 8 OK
// - Debug --> a: 8, user_a: 8 OK

int main(int argc, char** argv)
   const int64_t value = argc+6;
   int64_t a = value;

#if defined(EVIL_TEMPORARY)
   const user_t user(a++); // the temporary result of a++ is referenced by member m_a
   const user_t user(a);
   const int64_t user_a = user.use_it();
   const bool ok = user_a == a;
   printf("a: %I64d, user_a: %I64d %s\n", a, user_a, ok ? "OK":"FAIL");
   return ok;

Either msan or the static analyzer might catch this, but there’s no straight up compiler warning for this (and as the issue is interprocedural, any such warning would have to be assisted by source annotations)

This is more like use-after-scope (not implemented yet) in asan