public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/55239] New: Spurious "unused variable" warning on function-local objects with a destructor and an initializer
@ 2012-11-08 14:22 bisqwit at iki dot fi
  2012-11-08 14:47 ` [Bug c++/55239] " redi at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: bisqwit at iki dot fi @ 2012-11-08 14:22 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55239

             Bug #: 55239
           Summary: Spurious "unused variable" warning on function-local
                    objects with a destructor and an initializer
    Classification: Unclassified
           Product: gcc
           Version: 4.7.1
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: bisqwit@iki.fi


In the code below, a function-local object is declared with a destructor whose
role is to ensure that some action is taken at the end of the scope, no matter
which route the function is exited.

    #include <stdio.h>
    void LoadSomeFile(const char* fn)
    {
        /* Open file */
        FILE* fp = fopen(fn, "rb");
        /* Ensure that the file is automatically closed no matter which path
this function is exited */
        struct closer { FILE* f;  ~closer() { if(f) fclose(f); }
                      } autoclosefp = {fp};
        /* Some code here that deals with fp, and may include several "return;"
clauses */
    }
    int main() { LoadSomeFile(__FILE__); } // test

Bug GCC gives a spurious "unused variable 'autoclosefp'" for this code,
implying that autoclosefp has no function. It does. Without it, the file would
not be closed and resources would be leaked.

The problem also occurs, when the code is rewritten like this:

    #include <stdio.h>
    void LoadSomeFile(const char* fn)
    {
        /* Open file */
        FILE* fp = fopen(fn, "rb");
        /* Ensure that the file is automatically closed no matter which path
this function is exited */
        struct closer { FILE* f;  ~closer() { if(f) fclose(f); } };
        closer autoclosefp = {fp};
        /* Some code here that deals with fp, and may include several "return;"
clauses */
    }
    int main() { LoadSomeFile(__FILE__); } // test

Changing the "= {fp}" into C++11 style "{fp}" does not take away the warning,
either.

Only changing the initialization-by-initializer into an member-assignment takes
away the warning.

    #include <stdio.h>
    void LoadSomeFile(const char* fn)
    {
        /* Open file */
        FILE* fp = fopen(fn, "rb");
        /* Ensure that the file is automatically closed no matter which path
this function is exited */
        struct closer { FILE* f;  ~closer() { if(f) fclose(f); } } autoclosefp;
        autoclosefp.f = fp;
        /* Some code here that deals with fp, and may include several "return;"
clauses */
    }
    int main() { LoadSomeFile(__FILE__); } // test

I would argue that this is inconvenient, and wrong behavior on GCC.

Tested and verified on GCC 3.3 through 4.7.1. The -Wunused-variable (or -Wall)
option is required.


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-11-08 15:21 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-08 14:22 [Bug c++/55239] New: Spurious "unused variable" warning on function-local objects with a destructor and an initializer bisqwit at iki dot fi
2012-11-08 14:47 ` [Bug c++/55239] " redi at gcc dot gnu.org
2012-11-08 15:04 ` paolo.carlini at oracle dot com
2012-11-08 15:06 ` paolo.carlini at oracle dot com
2012-11-08 15:14 ` redi at gcc dot gnu.org
2012-11-08 15:17 ` bisqwit at iki dot fi
2012-11-08 15:20 ` paolo.carlini at oracle dot com
2012-11-08 15:21 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).