Added example from the issue
This commit is contained in:
hsutter 2017-01-30 11:52:11 -08:00
parent 51b4cddbca
commit 8f2ecf0665

View File

@ -9613,7 +9613,7 @@ not. Unfortunately, it may be impossible to detect when a non-`const` was not
##### Reason
Readability.
Readability and safety.
##### Example, bad
@ -9624,6 +9624,26 @@ Readability.
for (i = 0; i < 200; ++i) { /* ... */ } // bad: i recycled
}
##### Note
As an optimization, you may want to reuse a buffer as a scratchpad, but even then prefer to limit the variables's scope as much as possible and be careful not to cause bugs from data left in a recycled buffer as this is a common source of security bugs.
{
std::string buffer; // to avoid reallocations on every loop iteration
for (auto& o : objects)
{
// First part of the work.
generateFirstString(buffer, o);
writeToFile(buffer);
// Second part of the work.
generateSecondString(buffer, o);
writeToFile(buffer);
// etc...
}
}
##### Enforcement
Flag recycled variables.