[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:17 PDT 2025


================
@@ -0,0 +1,90 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "LoopVariableCopiedThenModifiedCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/TypeTraits.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Analysis/Analyses/ExprMutationAnalyzer.h"
+#include "clang/Basic/Diagnostic.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+LoopVariableCopiedThenModifiedCheck::LoopVariableCopiedThenModifiedCheck(
+    StringRef Name, ClangTidyContext *Context)
+    : ClangTidyCheck(Name, Context),
+      IgnoreInexpensiveVariables(
+          Options.get("IgnoreInexpensiveVariables", false)) {}
+
+void LoopVariableCopiedThenModifiedCheck::storeOptions(
+    ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "IgnoreInexpensiveVariables", IgnoreInexpensiveVariables);
+}
+
+void LoopVariableCopiedThenModifiedCheck::registerMatchers(
+    MatchFinder *Finder) {
+  auto HasReferenceOrPointerTypeOrIsAllowed = hasType(qualType(
----------------
nicovank wrote:

```suggestion
  const auto HasReferenceOrPointerTypeOrIsAllowed = hasType(qualType(
```

Nit here and below.

https://github.com/llvm/llvm-project/pull/157213


More information about the cfe-commits mailing list