The earlier fix for 38958 was too broad; we don't want to suppress the unused warning for all references to type with non-trivial destructor, just references bound to a temporary.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit 725c1c94f7e14cfc5314a86c3504cb33ec1cb74b
Author: Jason Merrill <ja...@redhat.com>
Date:   Fri Nov 21 17:50:58 2014 -0500

    	PR c++/63657
    	PR c++/38958
    	* call.c (set_up_extended_ref_temp): Set TREE_USED on the reference
    	if the temporary has a non-trivial destructor.
    	* decl.c (poplevel): Don't look through references.

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 5cda1b1..a7a8667 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -9622,6 +9622,10 @@ set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
 	/* Check whether the dtor is callable.  */
 	cxx_maybe_build_cleanup (var, tf_warning_or_error);
     }
+  /* Avoid -Wunused-variable warning (c++/38958).  */
+  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
+      && TREE_CODE (decl) == VAR_DECL)
+    TREE_USED (decl) = DECL_READ_P (decl) = true;
 
   *initp = init;
   return var;
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 899637f..225d408 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -638,8 +638,7 @@ poplevel (int keep, int reverse, int functionbody)
 	   push_local_binding where the list of decls returned by
 	   getdecls is built.  */
 	decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
-	// See through references for improved -Wunused-variable (PR 38958).
-	tree type = non_reference (TREE_TYPE (decl));
+	tree type = TREE_TYPE (decl);
 	if (VAR_P (decl)
 	    && (! TREE_USED (decl) || !DECL_READ_P (decl))
 	    && ! DECL_IN_SYSTEM_HEADER (decl)
diff --git a/gcc/testsuite/g++.dg/warn/Wunused-var-22.C b/gcc/testsuite/g++.dg/warn/Wunused-var-22.C
new file mode 100644
index 0000000..8ae46c1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wunused-var-22.C
@@ -0,0 +1,12 @@
+// PR c++/63657
+// { dg-options "-Wunused-variable" }
+
+class Bar
+{
+  virtual ~Bar() {}
+};
+Bar& getbar();
+void bar()
+{
+  Bar& b = getbar();		// { dg-warning "unused" }
+}

Reply via email to