From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6851 invoked by alias); 2 Sep 2011 12:45:40 -0000 Received: (qmail 6825 invoked by uid 22791); 2 Sep 2011 12:45:38 -0000 X-SWARE-Spam-Status: No, hits=-2.9 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 02 Sep 2011 12:45:24 +0000 From: "bisqwit at iki dot fi" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/50276] New: Wrong "used uninitialized in this function" warning [C++0x] Date: Fri, 02 Sep 2011 12:45: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: 2011-09/txt/msg00139.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50276 Bug #: 50276 Summary: Wrong "used uninitialized in this function" warning [C++0x] Classification: Unclassified Product: gcc Version: 4.6.1 Status: UNCONFIRMED Severity: minor Priority: P3 Component: c++ AssignedTo: unassigned@gcc.gnu.org ReportedBy: bisqwit@iki.fi For this example code, GCC mistakenly produces the following warning: tmp.cc:10:5: warning: 'value' is used uninitialized in this function [-Wuninitialized] The warning is wrongly given, because there is no execution path that does not assign a well-defined value to the variable. In fact, there are no branches at all between the declaring and the assigning of the variable. template unsigned testfun(const T& func) { return func(); } template unsigned test() { if(unsigned value = testfun( [] () { return 0; })) { return value; } return i; } int main() { return test<1>(); } The warning being wrongly given depends on the following conditions: - "test()" being a template function: changing "i" into an actual parameter removes the warning - "func" being a functor: changing it into an integer parameter removes the warning - the variable "value" being declared and assigned to in the if-condition: declaring and assigning it separately removes the warning. - the "func" parameter being a lambda function: changing it into a static method of a class removes the warning. The following aspects do not affect the warning: - "testfun()" being a template function: changing "T" into an explicit int(*)() retains the warning - whether "i" is used within "test()" or not - adding "static" or "inline" attributes to any function did not change the warning. Tested on GCC 4.5.3 and GCC 4.6.1, on x86_64-linux-gnu in both 32-bit and 64-bit mode on all optimization modes.