[llvm-bugs] [Bug 32823] Warn about accidental coping of data in range based for

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Jan 17 12:06:22 PST 2020


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

Denis Nikitin <denik at google.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |denik at google.com
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |---

--- Comment #4 from Denis Nikitin <denik at google.com> ---
We are hitting the warning in template code with bool and loop like this: "for
(const auto& element : value)" where element is bool from template. But not
always.
When it is bool the warning triggers:
error: loop variable 'element' is always a copy because the range of type
'const std::vector<bool, allocator<bool> >' does not return a reference
[-Werror,-Wrange-loop-analysis]

However, the template type can also use non-bools and if I remove the reference
I hit alternative case where the warning complains about copy operation of
reference types:
error: loop variable 'element' of type 'const std::1::basic_string<char>'
creates a copy from type 'const std::1::basic_string<char>'
[-Werror,-Wrange-loop-analysis]
for (const auto element : value):
use reference type 'const std::1::tuple<unsigned int, bool,
std::1::vector<unsigned char, std::__1::allocator<unsigned char> > > &' to
prevent copying

So there is no way to satisfy the warning other than disabling it.

Should the warning be improved for the template types?

-- 
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/20200117/a98b030a/attachment.html>


More information about the llvm-bugs mailing list