Skip to content

[clang-tidy] False positive modernize-loop-convert for index used with another container in lambda capture  #109083

Closed
@vient

Description

@vient

https://godbolt.org/z/YPd4h3xPn

#include <cstdlib>
#include <vector>

struct A {
    void f(std::vector<double> &v) {
        for (size_t i = 0; i < v.size(); ++i) {
            [[maybe_unused]] auto l = [x = std::move(vv[i])]() {
                return x;
            }();
            if (v[i] == 0) {}
        }
    }

    std::vector<float> vv;
};

produces

<source>:6:9: warning: use range-based for loop instead [modernize-loop-convert]
    6 |         for (size_t i = 0; i < v.size(); ++i) {
      |         ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |             (double i : v)
    7 |             [[maybe_unused]] auto l = [x = std::move(vv[i])]() {
    8 |                 return x;
    9 |             }();
   10 |             if (v[i] == 0) {}
      |                 ~~~~
      |                 i
1 warning generated.

without considering that capture [x = std::move(vv[i])] uses i to index another container.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions