Issue 237694
Summary [clang-tidy] readability-use-anyofallof suggests std::ranges::any_of for a range that does not satisfy std::ranges::input_range
Labels clang-tidy
Assignees
Reporter YevheniiKotyrlo
    In C++20 mode `readability-use-anyofallof` asks to replace a range-based `for` loop with `std::ranges::any_of()` / `std::ranges::all_of()` without checking that the loop's range is a `std::ranges::input_range`. A range-based `for` only needs `begin()`/`end()`, `!=`, prefix `++` and `*`, so plenty of ranges written for it (enum ranges, hand-rolled iterators without `difference_type` or postfix `++`) are not `input_range`s, and the replacement the check asks for doesn't compile. With `WarningsAsErrors` the only ways out are rewriting the loop some other way or suppressing the check.

I ran into it with an enum range shaped like Yoga's `facebook::yoga::ordinals<Enum>()`, whose iterator has `operator*`, prefix `operator++` and a defaulted `operator==`, and nothing else.

## Steps to reproduce

Reproducer: https://gist.github.com/YevheniiKotyrlo/3c4c3dd3062834d8f6b511c870053217 (`loop.cpp`, and `suggested.cpp` with the loop written as the check suggests).

```cpp
// loop.cpp (abridged)
template <typename EnumT> auto ordinals() {
  struct Iterator {
    EnumT value{};
    EnumT operator*() const { return value; }
    Iterator &operator++() { /* next enumerator */ return *this; }
    bool operator==(const Iterator &other) const = default;
  };
  struct Range {
    Iterator begin() const { return Iterator{}; }
    Iterator end() const { return Iterator{EnumT::Count}; }
 };
  return Range{};
}

bool isSet(Edge edge);

bool anySet() {
  for (auto edge : ordinals<Edge>()) {
    if (isSet(edge)) {
      return true;
    }
  }
  return false;
}
```

```console
$ git clone https://gist.github.com/3c4c3dd3062834d8f6b511c870053217.git anyof
$ cd anyof
$ clang-tidy --checks='-*,readability-use-anyofallof' loop.cpp -- -std=c++20
1 warning generated.
loop.cpp:27:3: warning: replace loop by 'std::ranges::any_of()' [readability-use-anyofallof]
   27 |   for (auto edge : ordinals<Edge>()) {
      |   ^
loop.cpp:27:20: note: reusing the temporary range directly in the replacement may be unsafe; consider materializing it in a local variable first, or use 'std::ranges' algorithms which handle temporary ranges safely
   27 |   for (auto edge : ordinals<Edge>()) {
      |                    ^
$ clang++ -std=c++20 -fsyntax-only suggested.cpp
suggested.cpp:26:24: error: no matching function for call to object of type 'const _Any_of_fn'
   26 | bool anySet() { return std::ranges::any_of(ordinals<Edge>(), isSet); }
      | ^~~~~~~~~~~~~~~~~~~
...: note: because 'Range' does not satisfy 'input_range'
...: note: because 'Range' does not satisfy 'range'
1 error generated.
```

(The same error with libc++: `'Range' does not satisfy 'input_range'`.)

Expected: no `std::ranges::any_of()` suggestion when the loop's range does not satisfy `std::ranges::input_range` (either no warning, or the `std::any_of()` wording the check uses before C++20, which does accept these iterators).

Actual: the check suggests `std::ranges::any_of()`, which rejects the range.

The note is also a little odd here: it suggests using `std::ranges` algorithms as an alternative, while the warning itself already suggests one.

## Versions

- clang-tidy and clang built from `main` at 2b54d70a764a61c2565f8a7f9890c9f71c60d9b3 (MSVC 14.51, Release; MSVC's standard library); 23.1.3 behaves the same
- Windows 11 (10.0.26300); nothing here is platform specific

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to