Hi,

in this old issue submitter points out that we emit too easily Wunused_variable warnings even when the destructor has side effects (otoh, the constructor is handled Ok). This is particularly annoying together with eg, RAII.

Turns out that lately we are already careful when we handle the new Wunused_but_set_variable, thus I'm simply proposing to commonize the additional check.

Tested x86_64-linux.

Thanks,
Paolo.

/////////////////////////////
/cp
2012-08-20  Paolo Carlini  <paolo.carl...@oracle.com>

        PR c++/10416
        * decl.c (poplevel): Check TYPE_HAS_NONTRIVIAL_DESTRUCTOR for
        Wunused_variable too.

/testsuite
2012-08-20  Paolo Carlini  <paolo.carl...@oracle.com>

        PR c++/10416
        * g++.dg/warn/Wunused-var-17.C: New.
Index: testsuite/g++.dg/warn/Wunused-var-17.C
===================================================================
--- testsuite/g++.dg/warn/Wunused-var-17.C      (revision 0)
+++ testsuite/g++.dg/warn/Wunused-var-17.C      (revision 0)
@@ -0,0 +1,4 @@
+// PR c++/10416
+// { dg-options "-Wunused" }
+
+void f () { struct atend { ~atend () { __builtin_printf("leaving f\n"); } } a; 
}
Index: cp/decl.c
===================================================================
--- cp/decl.c   (revision 190526)
+++ cp/decl.c   (working copy)
@@ -621,16 +621,16 @@ poplevel (int keep, int reverse, int functionbody)
       if (TREE_CODE (decl) == VAR_DECL
          && (! TREE_USED (decl) || !DECL_READ_P (decl))
          && ! DECL_IN_SYSTEM_HEADER (decl)
-         && DECL_NAME (decl) && ! DECL_ARTIFICIAL (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))))
        {
          if (! TREE_USED (decl))
            warning (OPT_Wunused_variable, "unused variable %q+D", decl);
          else if (DECL_CONTEXT (decl) == current_function_decl
-                  && TREE_TYPE (decl) != error_mark_node
                   && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
-                  && errorcount == unused_but_set_errorcount
-                  && (!CLASS_TYPE_P (TREE_TYPE (decl))
-                      || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
+                  && errorcount == unused_but_set_errorcount)
            {
              warning (OPT_Wunused_but_set_variable,
                       "variable %q+D set but not used", decl); 

Reply via email to