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
{{ message }}
Repository navigation
Commit a7f0386
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: test/testother.cpp
+98Lines changed: 98 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -14711,6 +14711,104 @@ class TestOther : public TestFixture {
14711
14711
" dostuff(a);\n"
14712
14712
"}\n");
14713
14713
ASSERT_EQUALS("", errout_str());
14714
+
14715
+
// Step updates via function call — OK (false positive previously)
14716
+
check("void inc(int* p) { ++*p; }\n"
14717
+
"void f(int n) {\n"
14718
+
" for (int i = 0; i < n; inc(&i)) {}\n"
14719
+
"}\n");
14720
+
ASSERT_EQUALS("", errout_str());
14721
+
14722
+
// Step updates via std::advance — OK
14723
+
check("#include <iterator>\n"
14724
+
"#include <list>\n"
14725
+
"void f(std::list<int>& l) {\n"
14726
+
" for (auto it = l.begin(); it != l.end(); std::advance(it, 1)) {}\n"
14727
+
"}\n");
14728
+
ASSERT_EQUALS("", errout_str());
14729
+
14730
+
// Step updates via member taking reference — OK
14731
+
check("struct S { void step(int& i); };\n"
14732
+
"void f(S& s, int n) {\n"
14733
+
" for (int i = 0; i < n; s.step(i)) {}\n"
14734
+
"}\n");
14735
+
ASSERT_EQUALS("", errout_str());
14736
+
14737
+
// Lambda in step that updates the variable — OK
14738
+
check("void f(int n) {\n"
14739
+
" for (int i = 0; i < n; ([&]{ i++; })()) {}\n"
14740
+
"}\n");
14741
+
ASSERT_EQUALS("", errout_str());
14742
+
14743
+
// Other operand of condition changes in body — OK
14744
+
check("void use(int);\n"
14745
+
"void f(int n) {\n"
14746
+
" for (int i = 0; i < n;) {\n"
14747
+
" use(i);\n"
14748
+
" n--;\n"
14749
+
" }\n"
14750
+
"}\n");
14751
+
ASSERT_EQUALS("", errout_str());
14752
+
14753
+
// Other operand is a non-const function call — OK
14754
+
check("int get();\n"
14755
+
"void f() {\n"
14756
+
" for (int i = 0; i != get();) {}\n"
14757
+
"}\n");
14758
+
ASSERT_EQUALS("", errout_str());
14759
+
14760
+
// Other operand reads volatile — OK
14761
+
check("void f(const volatile int *p) {\n"
14762
+
" for (int i = 0; i < *p;) {}\n"
14763
+
"}\n");
14764
+
ASSERT_EQUALS("", errout_str());
14765
+
14766
+
// Init assigns bound; step updates the other operand — OK
14767
+
check("void f(int M) {\n"
14768
+
" int i = 0;\n"
14769
+
" for (int m = M; i < m; i++) {}\n"
14770
+
"}\n");
14771
+
ASSERT_EQUALS("", errout_str());
14772
+
14773
+
// noreturn/exit in body — OK
14774
+
check("#include <stdlib.h>\n"
14775
+
"void use(int);\n"
14776
+
"void f(int n) {\n"
14777
+
" for (int i = 0; i < n;) {\n"
14778
+
" use(i);\n"
14779
+
" exit(1);\n"
14780
+
" }\n"
14781
+
"}\n");
14782
+
ASSERT_EQUALS("", errout_str());
14783
+
14784
+
// [[noreturn]] in body — OK
14785
+
check("void use(int);\n"
14786
+
"[[noreturn]] void fail();\n"
14787
+
"void f(int n) {\n"
14788
+
" for (int i = 0; i < n;) {\n"
14789
+
" use(i);\n"
14790
+
" fail();\n"
14791
+
" }\n"
14792
+
"}\n");
14793
+
ASSERT_EQUALS("", errout_str());
14794
+
14795
+
// continue alone does not exit the loop — still warn
14796
+
check("void use(int);\n"
14797
+
"void f(int n) {\n"
14798
+
" for (int i = 0; i < n;) {\n"
14799
+
" if (n > 0)\n"
14800
+
" continue;\n"
14801
+
" use(i);\n"
14802
+
" }\n"
14803
+
"}\n");
14804
+
ASSERT_EQUALS("[test.cpp:3:3]: (warning) The loop variable 'i' is not updated in this for-loop, so the loop might never terminate. [missingForLoopIncrement]\n", errout_str());
14805
+
14806
+
// Step updates a different variable, not the control var — still warn
14807
+
check("void f() {\n"
14808
+
" int k = 0;\n"
14809
+
" for (int h = 35; h > 5; k++) {}\n"
14810
+
"}\n");
14811
+
ASSERT_EQUALS("[test.cpp:3:3]: (warning) The loop variable 'h' is not updated in this for-loop, so the loop might never terminate. [missingForLoopIncrement]\n", errout_str());
0 commit comments