[llvm-bugs] [Bug 41743] New: -Wrange-loop-analysis warns about copying small (or empty), trivially copyable types

via llvm-bugs llvm-bugs at lists.llvm.org
Sat May 4 18:13:30 PDT 2019


https://bugs.llvm.org/show_bug.cgi?id=41743

            Bug ID: 41743
           Summary: -Wrange-loop-analysis warns about copying small (or
                    empty), trivially copyable types
           Product: clang
           Version: 8.0
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: kerndog73 at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190505/5e0258b3/attachment.html>


More information about the llvm-bugs mailing list