<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - -Wuninitialized false positive when passing a value to a nameless argument"
   href="https://bugs.llvm.org/show_bug.cgi?id=38447">38447</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>-Wuninitialized false positive when passing a value to a nameless argument
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>6.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Static Analyzer
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>dcoughlin@apple.com
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>plroskin@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=20641" name="attach_20641" title="Example source">attachment 20641</a> <a href="attachment.cgi?id=20641&action=edit" title="Example source">[details]</a></span>
Example source

I'm using a class that reads data into data members of other classes. To ensure
that the correct data is read into the field, the field is passed as an
argument to the data reader.

Only the type of the field is used. The original value of the field is not used
by the data reader. That should be clear to the compiler from the fact that the
data reader doesn't name its argument in its implementation, which is visible
to the complier.

Following is a greatly simplified implementation.


struct DataReader
{
    template <class T>
    T operator()(const T&) const
    {
      return read_data(T{});
    }

    int read_data(const int&) const
    {
        return 1;
    }

    long read_data(const long&) const
    {
        return 2;
    }
};

class MyData
{
    explicit MyData(const DataReader& data_reader):
        field_int{data_reader(field_int)},
        field_long{data_reader(field_long)}
    {}

    const int field_int;
    const long field_long;
};


clang 6.0.1 reports warnings:

example.cpp:23:31: warning: field 'field_int' is uninitialized when used here
[-Wuninitialized]
        field_int{data_reader(field_int)},
                              ^
example.cpp:24:32: warning: field 'field_long' is uninitialized when used here
[-Wuninitialized]
        field_long{data_reader(field_long)}
                               ^

No version of gcc (from 4.9 to 8.1) reports any warnings for that code. A
resent version of icc doesn't report any warnings either.

As a result, I have to disable -Wuninitialized for the clang build.

The code base has a large number of the DataReader users, and I really want to
keep the calls simple, i.e. no pragma in every file, no decltype in every file,
and no macros that would obscure the fact that I'm initializing data members.

The maybe_unused attribute doesn't help. I need to tell the compile that the
argument is definitely not used. But it should be clear already. Nameless
arguments cannot be used.

clang 3.8, 3.9, 4.0.1 and 5.0.2 suffer from that issue as well.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>