@@ -17,10 +17,12 @@ main()
17
17
18
18
// Case 1: Variable is used inside a loop condition without being modified.
19
19
while (n < 10 ) {} // warning 250: variable "n" used in loop condition not modified in loop body
20
+ do {} while (n < 10 ); // warning 250: variable "n" used in loop condition not modified in loop body
20
21
for (new i = 0 , j = 0 ; i < 10 ; ++ j) {} // warning 250: variable "i" used in loop condition not modified in loop body
21
22
22
23
// Case 2: Variable is used inside a loop condition and modified in the loop body.
23
24
while (n != 0 ) { n++ ; }
25
+ do { n++ ; } while (n < 10 );
24
26
for (new i = 0 ; i < 10 ; ) { i++ ; }
25
27
26
28
// Case 3: Variable is used inside a loop condition and modified in the
@@ -30,17 +32,21 @@ main()
30
32
// Case 4: Variable is used and modified inside a loop condition.
31
33
while (n++ != 0 ) {}
32
34
while (++ n != 0 ) {}
35
+ do {} while (n++ != 0 );
36
+ do {} while (++ n != 0 );
33
37
for (new i = 0 ; i++ < 10 ; ) {}
34
38
for (new i = 0 ; ++ i < 10 ; ) {}
35
39
36
40
// Case 5: Same variable is used inside a loop condition more than once
37
41
// and it's not modified.
38
42
while (n == 0 || n < 10 ) {} // warning 250: variable "n" used in loop condition not modified in loop body
43
+ do {} while (n == 0 || n < 10 ); // warning 250: variable "n" used in loop condition not modified in loop body
39
44
for (new i = 0 ; i == 0 || i < 10 ; ) {} // warning 250: variable "i" used in loop condition not modified in loop body
40
45
41
46
// Case 6: Same variable is used inside a loop condition more than once,
42
47
// but it's modified.
43
48
while (n == 0 || n < 10 ) { n++ ; }
49
+ do { n++ ; } while (n == 0 || n < 10 );
44
50
for (new i = 0 ; i == 0 || i < 10 ; i++ ) {}
45
51
46
52
// Case 7: Two variables are used inside a loop condition, both aren't modified.
@@ -51,18 +57,21 @@ main()
51
57
// Solution: introduce a warning that simply says that none of the variables
52
58
// were modified, and let the user decide which variable should be modified.
53
59
while (n < m) {} // warning 251: none of the variables used in loop condition are modified in loop body
60
+ do {} while (n < m); // warning 251: none of the variables used in loop condition are modified in loop body
54
61
for (new i = 0 ; i < m; ) {} // warning 251: none of the variables used in loop condition are modified in loop body
55
62
56
63
// Case 7: Two variables are used in a loop condition, but one of them
57
64
// is modified inside the loop body (or the loop counter increment/decrement
58
65
// section of a "for" loop), and the other one is not modified.
59
66
while (n < m) { ++ n; }
67
+ do { -- m; } while (n < m);
60
68
for (new i = 0 ; i < m; ) { i++ ; }
61
69
for (new i = 0 ; i < m; i++ ) {}
62
70
63
71
// Case 8: Two variables are used in a loop condition, but one of them
64
72
// is being modified prior to being used, and the other one is not modified.
65
73
while (++ n < m) {}
74
+ do {} while (++ n < m);
66
75
for (new i = 0 ; ++ i < m; ) {}
67
76
68
77
// Case 9: Two variables are used in a loop condition, but one of them
@@ -89,14 +98,16 @@ main()
89
98
// may be inaccurate otherwise.
90
99
new File: f = fopen (" test.txt" , io_read);
91
100
new line[128 ];
92
- while (fread (f,line,sizeof (line),false ) < m) {} // shouldn't warn about "f" or "m" not being modified
101
+ while (fread (f,line,sizeof (line),false ) < m) {} // shouldn't warn about "f" or "m" not being modified
102
+ do {} while (fread (f,line,sizeof (line),false ) < m); // shouldn't warn about "f" or "m" not being modified
93
103
fclose (f);
94
104
95
105
// Case 12: Warnings 250 and 251 shouldn't trigger when at least one global
96
106
// variable is used inside the loop condition, as globals can be modified
97
107
// from a function called from the loop body and currently there's no easy
98
108
// way to track this.
99
109
while (n < glbvar) {}
110
+ do {} while (n < glbvar);
100
111
for (new i = 0 ; i < glbvar; ) {}
101
112
102
113
// Case 13: Warnings 250 and 251 shouldn't trigger when the loop counter
@@ -111,4 +122,3 @@ main()
111
122
while (n < 10 ) UseVarByConstRef (n); // warning 250: variable "n" used in loop condition not modified in loop body
112
123
while (n < m) UseVarByConstRef (n); // warning 251: none of the variables used in loop condition are modified in loop body
113
124
}
114
-
0 commit comments