{
if (!warn_dangling_reference)
return;
- if (!(TYPE_REF_OBJ_P (TREE_TYPE (decl))
- || std_pair_ref_ref_p (TREE_TYPE (decl))))
+ tree type = TREE_TYPE (decl);
+ /* Only warn if what we're initializing has type T&& or const T&, or
+ std::pair<const T&, const T&>. (A non-const lvalue reference can't
+ bind to a temporary.) */
+ if (!((TYPE_REF_OBJ_P (type)
+ && (TYPE_REF_IS_RVALUE (type)
+ || CP_TYPE_CONST_P (TREE_TYPE (type))))
+ || std_pair_ref_ref_p (type)))
return;
/* Don't suppress the diagnostic just because the call comes from
a system header. If the DECL is not in a system header, or if
void
test ()
{
- int& r1 = temporary1 (42); // { dg-warning "dangling reference" }
- int& r2 = temporary2 (42); // { dg-warning "dangling reference" }
+ int& r1 = temporary1 (42);
+ int& r2 = temporary2 (42);
}
// x1 = Y::operator int&& (&TARGET_EXPR <D.2410, {}>)
int&& x1 = Y(); // { dg-warning "dangling reference" }
int&& x2 = Y{}; // { dg-warning "dangling reference" }
-int& x3 = Y(); // { dg-warning "dangling reference" }
-int& x4 = Y{}; // { dg-warning "dangling reference" }
+int& x3 = Y(); // { dg-bogus "dangling reference" }
+int& x4 = Y{}; // { dg-bogus "dangling reference" }
const int& t1 = Y().foo(10); // { dg-warning "dangling reference" }
--- /dev/null
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wdangling-reference" }
+
+int& ref(const int&);
+int&& rref(const int&);
+
+void
+g ()
+{
+ const int& r1 = ref (1); // { dg-warning "dangling reference" }
+ int& r2 = ref (2); // { dg-bogus "dangling reference" }
+ auto& r3 = ref (3); // { dg-bogus "dangling reference" }
+ int&& r4 = rref (4); // { dg-warning "dangling reference" }
+ auto&& r5 = rref (5); // { dg-warning "dangling reference" }
+ const int&& r6 = rref (6); // { dg-warning "dangling reference" }
+}