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

* [Bug c++/55239] Spurious "unused variable" warning on function-local objects with a destructor and an initializer
  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 ` redi at gcc dot gnu.org
  2012-11-08 15:04 ` paolo.carlini at oracle dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2012-11-08 14:47 UTC (permalink / raw)
  To: gcc-bugs


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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2012-11-08
     Ever Confirmed|0                           |1

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-11-08 14:46:56 UTC ---
Yes, annoying.

Giving the type a user-defined constructor makes the warning go away.  A
user-defined destructor should have the same effect.


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

* [Bug c++/55239] Spurious "unused variable" warning on function-local objects with a destructor and an initializer
  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
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-11-08 15:04 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #2 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-11-08 15:04:05 UTC ---
I fixed this in mainline, didn't I?


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

* [Bug c++/55239] Spurious "unused variable" warning on function-local objects with a destructor and an initializer
  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
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-11-08 15:06 UTC (permalink / raw)
  To: gcc-bugs


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

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |DUPLICATE

--- Comment #3 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-11-08 15:06:14 UTC ---
Yes.

*** This bug has been marked as a duplicate of bug 10416 ***


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

* [Bug c++/55239] Spurious "unused variable" warning on function-local objects with a destructor and an initializer
  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
                   ` (2 preceding siblings ...)
  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
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2012-11-08 15:14 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-11-08 15:14:25 UTC ---
Oops, thanks, Paolo!


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

* [Bug c++/55239] Spurious "unused variable" warning on function-local objects with a destructor and an initializer
  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
                   ` (3 preceding siblings ...)
  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
  6 siblings, 0 replies; 8+ messages in thread
From: bisqwit at iki dot fi @ 2012-11-08 15:17 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #5 from Joel Yliluoma <bisqwit at iki dot fi> 2012-11-08 15:16:48 UTC ---
Nice. I had no idea this was first reported in 2003 and fixed in 2012 in a
version recent enough to be still unreleased :)


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

* [Bug c++/55239] Spurious "unused variable" warning on function-local objects with a destructor and an initializer
  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
                   ` (4 preceding siblings ...)
  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
  6 siblings, 0 replies; 8+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-11-08 15:20 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #6 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-11-08 15:20:37 UTC ---
Understandable: the kind of work I have been doing lately at times is pretty
weird ;)


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

* [Bug c++/55239] Spurious "unused variable" warning on function-local objects with a destructor and an initializer
  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
                   ` (5 preceding siblings ...)
  2012-11-08 15:20 ` paolo.carlini at oracle dot com
@ 2012-11-08 15:21 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2012-11-08 15:21 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-11-08 15:21:40 UTC ---
Paolo's been very busy recently, cleaning up loads of longstanding bugs like
that


^ 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).