Bug ID: 44556
           Summary: -Wrange-loop-analysis is too strict in templates
           Product: clang
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Frontend

The warning -Wrange-loop-analysis diagnoses two issues:

* When the return value of the iterator's operator* is a reference type, but it
is captured by value: this means we've made an unnecessary copy.
* When the return value of the iterator's operator* is not a reference type,
but is captured by reference: this is fine, but suggests there is no copy when
in fact there is one. (Well, because of RVO there might actually be no copy.)

Unfortunately the warning flips in template code:

struct Rng {
    struct iterator {
        int operator*() const;
        iterator& operator++();
        bool operator!=(const iterator&) const;
    iterator begin() const;
    iterator end() const;

struct X {
    X() = default;
    X(const X&);

template<typename T>
void f(const T& t) {
    // Variant 1.
    for (const auto& _ : t)    // Warning from (1).
    // Variant 2.
    for (const auto _ : t)     // Warning from (2).

void g()
    X array[3];
    f(array);    // (1)
    f(Rng{});    // (2)

This means however I write f, there will always be a warning, although the
first variant is actually correct. I think that we should either not warn about
the second issue, or drop the warning when template-dependent types are
involved. However, we should in my opinion always warn about the first issue.

You are receiving this mail because:
You are on the CC list for the bug.
llvm-bugs mailing list

Reply via email to