<html>
    <head>
      <base href="http://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 - Clang should warn for not initializing field in a move constructor"
   href="http://bugs.llvm.org/show_bug.cgi?id=32427">32427</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang should warn for not initializing field in a move constructor
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Windows NT
          </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>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

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

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The C++ standard states that a move constructor is called when returning a
value from a method unless the compiler is able to elide the move,

For example, the following code invokes undefined behaviour:

#include <iostream>

class CallEmission {
public:
    bool assumedPlusZeroSelf;
    CallEmission(bool assumedPlusZeroSelf) :
assumedPlusZeroSelf(assumedPlusZeroSelf) {}

    CallEmission(CallEmission &&e) {
        std::cout << "Called move constructor" << "\n";
    }
};

CallEmission method() {
    CallEmission x(false);
    return x;
}

int main() {
    CallEmission x = method();
    std::cout << x.assumedPlusZeroSelf;
}

MSVC:
<span class="quote">> Called move constructor
> 140</span >

Clang:
<span class="quote">> 0</span >

This is because with MSVC the move constructor is called, which fails to
initialize the memory of `assumedPlusZeroSelf`.

This caused a crash in Swift on Windows with MSVC
(<a href="https://github.com/apple/swift/pull/8337">https://github.com/apple/swift/pull/8337</a>)

Michael Gottesman, an engineer on the Swift team said "@hughbe Nice find! I
wonder if there is a warning for this.". Seemingly there is not, so it would be
nice to issue a warning here to prevent different behaviour on different
compilers.

Maybe something like "field assumedPlusZeroSelf is not initialized in
constructor CallEmission(CallEmission&&)"</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>