<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 - -Wrange-loop-analysis warns about copying small (or empty), trivially copyable types"
   href="https://bugs.llvm.org/show_bug.cgi?id=41743">41743</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>-Wrange-loop-analysis warns about copying small (or empty), trivially copyable types
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>8.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>normal
          </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>kerndog73@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This is the code to reproduce:

#include <type_traits>

struct Thing {
  // no warning if you remove the constructor
  Thing() {}
};

static_assert(sizeof(Thing) <= sizeof(void *));
static_assert(std::is_trivially_copyable_v<Thing>);

int main() {
  Thing things[] = {{}, {}, {}};
  // no warning if you remove const
  for (const Thing thing : things) {}
}

This is the compiler invocation I used:

clang++ test.cpp -std=c++17 -Wrange-loop-analysis

This is the warning I received:

test.cpp:12:20: warning: loop variable 'thing' of type 'const Thing' creates a
copy from type 'const Thing'
      [-Wrange-loop-analysis]
  for (const Thing thing : things) {}
                   ^
test.cpp:12:8: note: use reference type 'const Thing &' to prevent copying
  for (const Thing thing : things) {}
       ^~~~~~~~~~~~~~~~~~~
1 warning generated.

Why am I getting a warning here? I'm copying an empty struct. Why does using
const matter? Why does defining the default constructor matter? The default
constructor shouldn't even be called. thing should be copy constructed from
each element in things.</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>