Skip to content

Commit 9db0df2

Browse files
committed
Add testcases for array and tuple structured bindings
1 parent 266d228 commit 9db0df2

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

clang-tools-extra/test/clang-tidy/checkers/bugprone/infinite-loop.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,3 +852,64 @@ void structured_binding_in_template_instantiation(int b) {
852852
structured_binding_in_template_bylref(b, 0);
853853
structured_binding_in_template_byrref(b, 0);
854854
}
855+
856+
void array_structured_binding() {
857+
int arr[2] = {0, 0};
858+
auto [x, y] = arr;
859+
860+
while (x < 10) {
861+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (x) are updated in the loop body [bugprone-infinite-loop]
862+
y++;
863+
}
864+
865+
while (y < 10) {
866+
y++; // no warning
867+
}
868+
}
869+
870+
namespace std {
871+
using size_t = int;
872+
template <class> struct tuple_size;
873+
template <std::size_t, class> struct tuple_element;
874+
template <class...> class tuple;
875+
876+
namespace {
877+
template <class T, T v>
878+
struct size_helper { static const T value = v; };
879+
} // namespace
880+
881+
template <class... T>
882+
struct tuple_size<tuple<T...>> : size_helper<std::size_t, sizeof...(T)> {};
883+
884+
template <std::size_t I, class... T>
885+
struct tuple_element<I, tuple<T...>> {
886+
using type = __type_pack_element<I, T...>;
887+
};
888+
889+
template <class...> class tuple {};
890+
891+
template <std::size_t I, class... T>
892+
typename tuple_element<I, tuple<T...>>::type get(tuple<T...>);
893+
} // namespace std
894+
895+
std::tuple<int*, int> &get_chunk();
896+
897+
void test_structured_bindings_tuple() {
898+
auto [buffer, size ] = get_chunk();
899+
int maxLen = 8;
900+
901+
while (size < maxLen) {
902+
// No warning. The loop is finite because 'size' is being incremented in each iteration and compared against 'maxLen' for termination
903+
buffer[size++] = 2;
904+
}
905+
}
906+
907+
void test_structured_bindings_tuple_ref() {
908+
auto& [buffer, size ] = get_chunk();
909+
int maxLen = 8;
910+
911+
while (size < maxLen) {
912+
// No warning. The loop is finite because 'size' is being incremented in each iteration and compared against 'maxLen' for termination
913+
buffer[size++] = 2;
914+
}
915+
}

0 commit comments

Comments
 (0)