From ce5341b02d6919d7a0fcee9c672221845715a01b Mon Sep 17 00:00:00 2001 From: Sergey Zubkov Date: Mon, 3 Jan 2022 16:50:07 -0500 Subject: [PATCH] merge ES.74 into ES.6, leaving a redirect behind closes #1858 --- CppCoreGuidelines.md | 56 +++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 37 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 9e90467..2f27e78 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -10212,7 +10212,10 @@ In this case, it might be a good idea to factor out the read: ##### 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 @@ -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 -* 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. +**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 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 * (hard) Flag selection/loop variables declared before the body and used after the body for an unrelated purpose. - - ### ES.7: Keep common and local names short, and keep uncommon and non-local names longer ##### Reason @@ -12718,39 +12732,7 @@ Flag actions in `for`-initializers and `for`-increments that do not relate to th ### ES.74: Prefer to declare a loop variable in the initializer part of a `for`-statement -##### Reason - -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. +See [ES.6](#Res-cond) ### ES.75: Avoid `do`-statements