merge ES.74 into ES.6, leaving a redirect behind

closes #1858
This commit is contained in:
Sergey Zubkov
2022-01-03 16:50:07 -05:00
parent dc6ea376c4
commit ce5341b02d

View File

@@ -10212,7 +10212,10 @@ In this case, it might be a good idea to factor out the read:
##### Reason ##### Reason
Readability. Minimize resource retention. Readability.
Limit the loop variable visibility to the scope of the loop.
Avoid using the loop variable for other purposes after the loop.
Minimize resource retention.
##### Example ##### Example
@@ -10233,11 +10236,24 @@ Readability. Minimize resource retention.
} }
} }
##### Example, don't
int j; // BAD: j is visible outside the loop
for (j = 0; j < 100; ++j) {
// ...
}
// j is still visible here and isn't needed
**See also**: [Don't use a variable for two unrelated purposes](#Res-recycle)
##### Enforcement ##### Enforcement
* Flag loop variables declared before the loop and not used after the loop * Warn when a variable modified inside the `for`-statement is declared outside the loop and not being used outside the loop.
* (hard) Flag loop variables declared before the loop and used after the loop for an unrelated purpose. * (hard) Flag loop variables declared before the loop and used after the loop for an unrelated purpose.
**Discussion**: Scoping the loop variable to the loop body also helps code optimizers greatly. Recognizing that the induction variable
is only accessible in the loop body unblocks optimizations such as hoisting, strength reduction, loop-invariant code motion, etc.
##### C++17 and C++20 example ##### C++17 and C++20 example
Note: C++17 and C++20 also add `if`, `switch`, and range-`for` initializer statements. These require C++17 and C++20 support. Note: C++17 and C++20 also add `if`, `switch`, and range-`for` initializer statements. These require C++17 and C++20 support.
@@ -10255,8 +10271,6 @@ Note: C++17 and C++20 also add `if`, `switch`, and range-`for` initializer state
* Flag selection/loop variables declared before the body and not used after the body * Flag selection/loop variables declared before the body and not used after the body
* (hard) Flag selection/loop variables declared before the body and used after the body for an unrelated purpose. * (hard) Flag selection/loop variables declared before the body and used after the body for an unrelated purpose.
### <a name="Res-name-length"></a>ES.7: Keep common and local names short, and keep uncommon and non-local names longer ### <a name="Res-name-length"></a>ES.7: Keep common and local names short, and keep uncommon and non-local names longer
##### Reason ##### Reason
@@ -12718,39 +12732,7 @@ Flag actions in `for`-initializers and `for`-increments that do not relate to th
### <a name="Res-for-init"></a>ES.74: Prefer to declare a loop variable in the initializer part of a `for`-statement ### <a name="Res-for-init"></a>ES.74: Prefer to declare a loop variable in the initializer part of a `for`-statement
##### Reason See [ES.6](#Res-cond)
Limit the loop variable visibility to the scope of the loop.
Avoid using the loop variable for other purposes after the loop.
##### Example
for (int i = 0; i < 100; ++i) { // GOOD: i var is visible only inside the loop
// ...
}
##### Example, don't
int j; // BAD: j is visible outside the loop
for (j = 0; j < 100; ++j) {
// ...
}
// j is still visible here and isn't needed
**See also**: [Don't use a variable for two unrelated purposes](#Res-recycle)
##### Example
for (string s; cin >> s; ) {
cout << s << '\n';
}
##### Enforcement
Warn when a variable modified inside the `for`-statement is declared outside the loop and not being used outside the loop.
**Discussion**: Scoping the loop variable to the loop body also helps code optimizers greatly. Recognizing that the induction variable
is only accessible in the loop body unblocks optimizations such as hoisting, strength reduction, loop-invariant code motion, etc.
### <a name="Res-do"></a>ES.75: Avoid `do`-statements ### <a name="Res-do"></a>ES.75: Avoid `do`-statements