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: unassignedclangb...@nondot.org
          Reporter: kerndo...@gmail.com
                CC: blitzrak...@gmail.com, dgre...@apple.com,
                    erik.pilking...@gmail.com, llvm-bugs@lists.llvm.org,
                    richard-l...@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.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to