You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[CherryPick][clang-tidy] Improve bugprone-infinite-loop check by adding handing for structured bindings (llvm#144213) (#11104)
Before this patch, this check only handles `VarDecl` as varaibles
declaration in statement, but this will ignore variables in structured
bindings (`BindingDecl` in AST), which leads to false positives.
rdar://152083639
Co-authored-by: flovent <[email protected]>
p(0); // we don't know what p points to so no warning
713
713
}
714
+
715
+
structPairVal {
716
+
int a;
717
+
int b;
718
+
PairVal(int a, int b) : a(a), b(b) {}
719
+
};
720
+
721
+
voidstructured_binding_infinite_loop1() {
722
+
auto [x, y] = PairVal(0, 0);
723
+
while (x < 10) {
724
+
// 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]
725
+
y++;
726
+
}
727
+
while (y < 10) {
728
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (y) are updated in the loop body [bugprone-infinite-loop]
729
+
x++;
730
+
}
731
+
}
732
+
733
+
voidstructured_binding_infinite_loop2() {
734
+
auto [x, y] = PairVal(0, 0);
735
+
while (x < 10) {
736
+
// 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]
737
+
// No update to x or y
738
+
}
739
+
while (y < 10) {
740
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (y) are updated in the loop body [bugprone-infinite-loop]
test_local_static_structured_binding_recursion(); // no warning, recursively decrement i
767
+
for (; i >= 0;)
768
+
test_local_static_structured_binding_recursion(); // no warning, recursively decrement i
769
+
for (; i + j >= 0;)
770
+
test_local_static_structured_binding_recursion(); // no warning, recursively decrement i
771
+
for (; i >= 0; i--)
772
+
; // no warning, i decrements
773
+
while (j >= 0)
774
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (j) are updated in the loop body [bugprone-infinite-loop]
775
+
test_local_static_structured_binding_recursion();
776
+
777
+
int (*p)(int) = 0;
778
+
779
+
while (i >= 0)
780
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i) are updated in the loop body [bugprone-infinite-loop]
781
+
p = 0;
782
+
while (i >= 0)
783
+
p(0); // we don't know what p points to so no warning
784
+
}
785
+
786
+
structS { int a; };
787
+
voidissue_138842_reduced() {
788
+
int x = 10;
789
+
auto [y] = S{1};
790
+
791
+
while (y < x) {
792
+
y++;
793
+
}
794
+
}
795
+
796
+
namespacestd {
797
+
template <typename T, typename U>
798
+
structpair {
799
+
T first;
800
+
U second;
801
+
802
+
pair(T a, U b) : first(a), second(b) {}
803
+
};
804
+
}
805
+
806
+
template <typename T, typename U>
807
+
voidstructured_binding_in_template_byval(T a, U b) {
808
+
auto [c, d] = std::pair<T, U>(a,b);
809
+
810
+
while (c < 10) {
811
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (c) are updated in the loop body [bugprone-infinite-loop]
812
+
d++;
813
+
}
814
+
815
+
while (c < 10) {
816
+
c++; // no warning
817
+
}
818
+
}
819
+
820
+
template <typename T, typename U>
821
+
voidstructured_binding_in_template_bylref(T a, U b) {
822
+
auto p = std::pair<T, U>(a,b);
823
+
auto& [c, d] = p;
824
+
825
+
while (c < 10) {
826
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (c) are updated in the loop body [bugprone-infinite-loop]
827
+
d++;
828
+
}
829
+
830
+
while (c < 10) {
831
+
c++; // no warning
832
+
}
833
+
}
834
+
835
+
template <typename T, typename U>
836
+
voidstructured_binding_in_template_byrref(T a, U b) {
837
+
auto p = std::pair<T, U>(a,b);
838
+
auto&& [c, d] = p;
839
+
840
+
while (c < 10) {
841
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (c) are updated in the loop body [bugprone-infinite-loop]
842
+
d++;
843
+
}
844
+
845
+
while (c < 10) {
846
+
c++; // no warning
847
+
}
848
+
}
849
+
850
+
voidstructured_binding_in_template_instantiation(int b) {
851
+
structured_binding_in_template_byval(b, 0);
852
+
structured_binding_in_template_bylref(b, 0);
853
+
structured_binding_in_template_byrref(b, 0);
854
+
}
855
+
856
+
voidarray_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]
0 commit comments