[llvm-bugs] [Bug 44556] New: -Wrange-loop-analysis is too strict in templates

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jan 15 08:01:39 PST 2020


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

            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
          Assignee: unassignedclangbugs at nondot.org
          Reporter: aaronpuchert at alice-dsl.net
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200115/659ae917/attachment.html>


More information about the llvm-bugs mailing list