Hi,

On 06/12/2013 07:46 PM, Jason Merrill wrote:
On 06/12/2013 01:37 PM, Paolo Carlini wrote:
Rewording: as the one line comment says, I only call non_reference at the outset when I know that either we'll end up producing the normal unused-variable warning or nothing at all.
Oh, I see. But that's a rather subtle difference; better to have 'type' mean something consistent and leave the unused-but-set code checking TREE_TYPE (decl).
Yeah. Earlier today I had something similar in my tree. I'm finishing testing the below then.

Thanks,
Paolo.
Index: cp/decl.c
===================================================================
--- cp/decl.c   (revision 200012)
+++ cp/decl.c   (working copy)
@@ -622,17 +622,20 @@ 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));
        if (VAR_P (decl)
            && (! TREE_USED (decl) || !DECL_READ_P (decl))
            && ! DECL_IN_SYSTEM_HEADER (decl)
            && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)
-           && TREE_TYPE (decl) != error_mark_node
-           && (!CLASS_TYPE_P (TREE_TYPE (decl))
-               || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
+           && type != error_mark_node
+           && (!CLASS_TYPE_P (type)
+               || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)))
          {
            if (! TREE_USED (decl))
              warning (OPT_Wunused_variable, "unused variable %q+D", decl);
            else if (DECL_CONTEXT (decl) == current_function_decl
+                    // For -Wunused-but-set-variable leave references alone.
                     && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
                     && errorcount == unused_but_set_errorcount)
              {
Index: testsuite/g++.dg/warn/Wunused-var-20.C
===================================================================
--- testsuite/g++.dg/warn/Wunused-var-20.C      (revision 0)
+++ testsuite/g++.dg/warn/Wunused-var-20.C      (working copy)
@@ -0,0 +1,19 @@
+// PR c++/38958
+// { dg-options "-Wunused" }
+
+volatile int g;
+
+struct Lock
+{
+  ~Lock() { g = 0; }
+};
+
+Lock AcquireLock() { return Lock(); }
+
+int main()
+{
+  const Lock& lock = AcquireLock();
+  g = 1;
+  g = 2;
+  g = 3;
+}

Reply via email to