From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11400 invoked by alias); 8 Nov 2012 14:22:50 -0000 Received: (qmail 11354 invoked by uid 48); 8 Nov 2012 14:22:31 -0000 From: "bisqwit at iki dot fi" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Keywords: X-Bugzilla-Severity: minor X-Bugzilla-Who: bisqwit at iki dot fi X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2012-11/txt/msg00718.txt.bz2 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 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 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 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.