[clang-tools-extra] [clang-tidy]suggest use `std::span` as replacement of c array in C++20 for modernize-avoid-c-arrays (PR #108555)

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 13 10:04:47 PDT 2024


================
@@ -72,11 +81,24 @@ void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
 
 void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) {
   const auto *ArrayType = Result.Nodes.getNodeAs<TypeLoc>("typeloc");
-
+  const bool IsInParam =
+      Result.Nodes.getNodeAs<ParmVarDecl>("param_decl") != nullptr;
+  const bool IsVLA = ArrayType->getTypePtr()->isVariableArrayType();
+  enum class RecommendType { Array, Vector, Span };
+  RecommendType RecommendType = RecommendType::Array;
+  if (IsVLA) {
+    RecommendType = RecommendType::Vector;
+  } else if (ArrayType->getTypePtr()->isIncompleteArrayType() && IsInParam) {
+    // in function parameter, we also don't know the size of
+    // IncompleteArrayType.
+    RecommendType = Result.Context->getLangOpts().CPlusPlus20
+                        ? RecommendType::Span
+                        : RecommendType::Vector;
+  }
----------------
5chmidti wrote:

These single statements are inside compound statements

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


More information about the cfe-commits mailing list