|
3 | 3 |
|
4 | 4 | new glbvar = 0;
|
5 | 5 |
|
| 6 | +stock UseVarByRef(&arg) |
| 7 | + return arg; |
| 8 | + |
| 9 | +#pragma warning disable 238 // "meaningless combination of class specifiers (const reference)" |
| 10 | +stock UseVarByConstRef(const &arg) |
| 11 | + return arg; |
| 12 | + |
6 | 13 | main()
|
7 | 14 | {
|
8 | 15 | new n = 0, m = 10;
|
9 | 16 | static st = 0;
|
10 | 17 |
|
11 | 18 | // Case 1: Variable is used inside a loop condition without being modified.
|
12 |
| - while (n < 10) {} // warning 250: variable used in loop condition not modified in loop body (symbol "n") |
13 |
| - for (new i = 0, j = 0; i < 10; ++j) {} // warning 250: variable used in loop condition not modified in loop body (symbol "i") |
| 19 | + while (n < 10) {} // warning 250: variable "n" used in loop condition not modified in loop body |
| 20 | + for (new i = 0, j = 0; i < 10; ++j) {} // warning 250: variable "i" used in loop condition not modified in loop body |
14 | 21 |
|
15 | 22 | // Case 2: Variable is used inside a loop condition and modified in the loop body.
|
16 | 23 | while (n != 0) { n++; }
|
|
28 | 35 |
|
29 | 36 | // Case 5: Same variable is used inside a loop condition more than once
|
30 | 37 | // and it's not modified.
|
31 |
| - while (n == 0 || n < 10) {} // warning 250: variable used in loop condition not modified in loop body (symbol "n") |
32 |
| - for (new i = 0; i == 0 || i < 10; ) {} // warning 250: variable used in loop condition not modified in loop body (symbol "i") |
| 38 | + while (n == 0 || n < 10) {} // warning 250: variable "n" used in loop condition not modified in loop body |
| 39 | + for (new i = 0; i == 0 || i < 10; ) {} // warning 250: variable "i" used in loop condition not modified in loop body |
33 | 40 |
|
34 | 41 | // Case 6: Same variable is used inside a loop condition more than once,
|
35 | 42 | // but it's modified.
|
@@ -91,5 +98,17 @@ main()
|
91 | 98 | // way to track this.
|
92 | 99 | while (n < glbvar) {}
|
93 | 100 | for (new i = 0; i < glbvar; ) {}
|
| 101 | + |
| 102 | + // Case 13: Warnings 250 and 251 shouldn't trigger when the loop counter |
| 103 | + // variable is passed to a function by reference. |
| 104 | + while (n < 10) UseVarByRef(n); |
| 105 | + while (n < m) UseVarByRef(n); |
| 106 | + |
| 107 | + // Case 14: While const references for single function arguments are |
| 108 | + // meaningless and there's warning 238 for this, such references still |
| 109 | + // shouldn't affect warnings 250 and 251, as variables passed by const |
| 110 | + // references aren't counted as modified. |
| 111 | + while (n < 10) UseVarByConstRef(n); // warning 250: variable "n" used in loop condition not modified in loop body |
| 112 | + while (n < m) UseVarByConstRef(n); // warning 251: none of the variables used in loop condition are modified in loop body |
94 | 113 | }
|
95 | 114 |
|
0 commit comments