[clang-tools-extra] Add bugprone-loop-variable-copied-then-modified clang-tidy check. (PR #157213)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 13 02:11:46 PDT 2025
================
@@ -0,0 +1,48 @@
+.. title:: clang-tidy - bugprone-loop-variable-copied-then-modified
+
+bugprone-loop-variable-copied-then-modified
+===========================================
+
+Detects when a loop variable is copied and then subsequently modified and
+suggests replacing with a reference or an explicit copy.
+
+This pattern is considered bugprone because, frequently, programmers do not
+realize that they are modifying a *copy* rather than an underlying value,
+resulting in subtly erroneous code.
+
+For instance, the following code attempts to null out a value in a map, but only
+succeeds in nulling out a value in a *copy* of the map:
+
+.. code-block:: c++
+
+ for (auto target : target_map) {
+ target.value = nullptr;
+ }
+
+The programmer is likely to have intended this code instead:
+
+.. code-block:: c++
+
+ for (auto& target : target_map) {
+ target.value = nullptr;
+ }
+
+This warning can be suppressed in one of two ways:
----------------
vbvictor wrote:
```suggestion
This warning can be suppressed in one of two ways:
```
https://github.com/llvm/llvm-project/pull/157213
More information about the cfe-commits
mailing list