[clang-tools-extra] Add bugprone-loop-variable-copied-then-modified clang-tidy check. (PR #157213)
Nicolas van Kempen via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 9 20:03:16 PDT 2025
https://github.com/nicovank commented:
Thanks!
I'm not a fan of the FixIt often leading to invalid code. Suggesting `const` when the variable is modified in the loop **always** produces invalid code (this is the case for the highlighted example in the documentation): suggesting `const` makes little sense when we know/assume the variable is modified.
Even if switching to reference only, the check will always transform the behavior of user code if applied directly. Most checks perform non-behavior-changing transformations. Maybe the FixIt can be a note instead or removed?
I ran this check on CMake sources (medium-sized codebase), there are a few true positives but also many false positives. Many being strings that are manipulated before being re-inserted into a different container for example. I couldn't think of a solution to reduce those yet.
One idea maybe is to check if the copy is then _used_ after modification. If it is not, that's a strong signal the copy was unintended. But this seems tricky.
https://github.com/llvm/llvm-project/pull/157213
More information about the cfe-commits
mailing list