User account creation filtered due to spam.

Bug 40695 - bogus "may be used uninitialized in this function" warning
Summary: bogus "may be used uninitialized in this function" warning
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.4.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: Wuninitialized
  Show dependency treegraph
 
Reported: 2009-07-09 02:54 UTC by Shane Beasley
Modified: 2011-10-12 17:06 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-07-09 15:55:31


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Shane Beasley 2009-07-09 02:54:01 UTC
Apologies if this is a duplicate, but I can't find another quite like this...

<blockquote>
struct Class
{
    // undefined
    void Method();
};

template < typename w_type >
struct NoPtr
{
    // inline
    NoPtr( bool = false ) : m_ptr( 0 ) { }

    // inline; calls a method
    ~NoPtr() { if ( m_ptr ) m_ptr->Method(); }

    // undefined
    w_type *Get();

    w_type *m_ptr;
};

void f()
{
    bool cond = true;

    // a named NoPtr< Class > initialized from a ternary expression
    // with an if-expr that refers to any named object,
    // plus a then-expr that contains an rvalue of type NoPtr< Class >
    NoPtr< Class > named(
        cond
        ? NoPtr< Class >().Get()
        : 0
    );
}
</blockquote>

$ /usr/bin/g++-4.2 -c -o/dev/null -xc++ -Wuninitialized -O file.cpp
$ /usr/bin/g++-4.2 -dumpversion
4.2.4

$ g++-4 -c -o/dev/null -xc++ -Wuninitialized -O file.cpp
file.cpp: In function 'void f()':
file.cpp:14: warning: 'named.NoPtr<Class>::m_ptr' may be used uninitialized in this function
file.cpp:30: note: 'named.NoPtr<Class>::m_ptr' was declared here
$ g++-4 -dumpversion
4.3.2

$ /usr/bin/g++-4.3 -c -o/dev/null -xc++ -Wuninitialized -O file.cpp
file.cpp: In function 'void f()':
file.cpp:14: warning: 'named.NoPtr<Class>::m_ptr' may be used uninitialized in this function
file.cpp:30: note: 'named.NoPtr<Class>::m_ptr' was declared here
$ /usr/bin/g++-4.3 -dumpversion
4.3.3

$ /usr/bin/g++-4.4 -c -o/dev/null -xc++ -Wuninitialized -O file.cpp
file.cpp: In function 'void f()':
file.cpp:14: warning: 'named.NoPtr<Class>::m_ptr' may be used uninitialized in this function
file.cpp:30: note: 'named.NoPtr<Class>::m_ptr' was declared here
$ /usr/bin/g++-4.4 -dumpversion
4.4.0

If it helps, that's 4.2.4 on Debian; 4.3.2 on Cygwin; 4.3.3 and 4.4.0 on both Solaris and Debian.
Comment 1 Shane Beasley 2009-07-09 02:55:34 UTC
I should add that I boiled this down from a much larger specimen that had all the copy constructors and assignment operators. Here, I'm just using the default ones. The problem exists in both cases.
Comment 2 Manuel López-Ibáñez 2009-07-09 15:55:30 UTC
Thanks, it also happens in current trunk.
Comment 3 Shane Beasley 2009-07-10 04:37:49 UTC
Another test case (should I file another bug?):

<blockquote>
struct Class
{
    Class() { }
    /* implicit copy constructor */
    void Method() const; // undefined
    /* Class has no data members */
};

void f(); // undefined

void g( Class x ) // pass by value
{
    f();        // call undefined function
    x.Method(); // call method on parameter
}

void h()
{
    Class x;
    g( x );
}
<blockquote>

$ g++ -Wuninitialized -O3 -c -o /dev/null file.cpp
file.cpp: In function 'void h()':
file.cpp:20: warning: '<anonymous>' is used uninitialized in this function
$ g++ -dumpversion
4.4.0

BTW: I'm just now trying to build our stuff with GCC 4.4.0. I've run into two warnings. The first is comment 0. The second may or may not be similar to the above, except that it has data members -- but still no copy constructor or assignment operator (no need). For fun, I've added them and am rebuilding now. If that doesn't fix the problem, I'll probably be back later. :)
Comment 4 Paolo Carlini 2011-10-12 17:06:23 UTC
Fixed in 4.5.x.