public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "bisqwit at iki dot fi" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/55239] New: Spurious "unused variable" warning on function-local objects with a destructor and an initializer
Date: Thu, 08 Nov 2012 14:22:00 -0000	[thread overview]
Message-ID: <bug-55239-4@http.gcc.gnu.org/bugzilla/> (raw)


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.


             reply	other threads:[~2012-11-08 14:22 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-08 14:22 bisqwit at iki dot fi [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-55239-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).