llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Iterating over std::initializer_list results in a null pointer warning.

Open roccomoretti opened this issue 5 years ago • 1 comments

#include <initializer_list>

void foo( std::initializer_list< int > const & initlist ) {
        for ( auto const & entry: initlist ) {;}
}

compiled with -Wlifetime results in the following issues

test.cc:5:26: warning: dereferencing a possibly null pointer [-Wlifetime-null]
        for ( auto const & entry: initlist ) {;}
                                ^
test.cc:5:26: note: assigned here
        for ( auto const & entry: initlist ) {;}
                                ^
test.cc:5:8: warning: assigning a possibly null pointer to a non-null object [-Wlifetime-null]
        for ( auto const & entry: initlist ) {;}
              ^~~~~~~~~~~~~~~~~~~

(https://godbolt.org/z/2aBLpr)

The only pointers here that I can think of are the std::initializer_list::begin() and std::initializer_list::end(), and it does look like the cause is thinking that std::initializer_list::begin() can be null: https://godbolt.org/z/8j_Qs3

(Even if for some reason begin() can return a null pointer, the phrasing of that message when applied to the range-for loop is quite obscure.)

roccomoretti avatar Apr 19 '20 19:04 roccomoretti

Thank you for taking the time to report this bug!

initializer_list have some special behaviors to them. We will look into it.

mgehre avatar Apr 21 '20 06:04 mgehre