1// RUN: %check_clang_tidy -std=c++17-or-later %s modernize-loop-convert %t
2
3struct S {
4 int a{0};
5 int b{1};
6};
7
8template <typename T, unsigned long Size>
9struct array {
10
11 T *begin() { return data; }
12 const T* cbegin() const { return data; }
13 T *end() { return data + Size; }
14 const T *cend() const { return data + Size; }
15
16 T data[Size];
17};
18
19const int N = 6;
20S Arr[N];
21
22void f() {
23 int Sum = 0;
24
25 for (int I = 0; I < N; ++I) {
26 auto [a, b] = Arr[I];
27 Sum += a + b;
28 }
29 // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead [modernize-loop-convert]
30 // CHECK-FIXES: for (auto [a, b] : Arr) {
31 // CHECK-FIXES-NEXT: Sum += a + b;
32
33 array<S, N> arr;
34 for (auto* It = arr.begin(); It != arr.end(); ++It) {
35 auto [a, b] = *It;
36 Sum = a + b;
37 }
38 // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead [modernize-loop-convert]
39 // CHECK-FIXES: for (auto [a, b] : arr) {
40 // CHECK-FIXES-NEXT: Sum = a + b;
41
42 for (auto* It = arr.cbegin(); It != arr.cend(); ++It) {
43 auto [a, b] = *It;
44 Sum = a + b;
45 }
46 // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead [modernize-loop-convert]
47 // CHECK-FIXES: for (auto [a, b] : arr) {
48 // CHECK-FIXES-NEXT: Sum = a + b;
49}
50

source code of clang-tools-extra/test/clang-tidy/checkers/modernize/loop-convert-structured-binding.cpp