[clang-tools-extra] [clang-tidy] Fix false positive in readability-non-const-parameter with dependent array subscripts (PR #190936)
Mao Chuanjun via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 8 02:49:36 PDT 2026
https://github.com/maochuanjun updated https://github.com/llvm/llvm-project/pull/190936
>From 4d1162f87705a9d703ea3a153a4721209c2bec79 Mon Sep 17 00:00:00 2001
From: maochuanjun <10255501521 at stu.ecnu.edu.cn>
Date: Wed, 8 Apr 2026 16:51:51 +0800
Subject: [PATCH 1/2] [clang-tidy] Fix false positive in
readability-non-const-parameter for array subscript in dependent context
---
.../readability/NonConstParameterCheck.cpp | 6 ++++++
clang-tools-extra/docs/ReleaseNotes.rst | 14 +++++++++-----
.../checkers/readability/non-const-parameter.cpp | 16 ++++++++++++++++
3 files changed, 31 insertions(+), 5 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
index 9950eca3fa7bd..75e851d285b54 100644
--- a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
@@ -216,6 +216,12 @@ void NonConstParameterCheck::markCanNotBeConst(const Expr *E,
}
} else if (const auto *A = dyn_cast<ArraySubscriptExpr>(E)) {
markCanNotBeConst(A->getBase(), true);
+ if (A->isInstantiationDependent()) {
+ markCanNotBeConst(A->getLHS(), true);
+ markCanNotBeConst(A->getRHS(), true);
+ } else {
+ markCanNotBeConst(A->getBase(), true);
+ }
} else if (const auto *CLE = dyn_cast<CompoundLiteralExpr>(E)) {
markCanNotBeConst(CLE->getInitializer(), true);
} else if (const auto *Constr = dyn_cast<CXXConstructExpr>(E)) {
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 2a185bd2baa15..3d5e322761ce3 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -226,7 +226,7 @@ Changes in existing checks
C++ files because suggested ``reinterpret_cast`` is not available in pure C.
- Improved :doc:`bugprone-derived-method-shadowing-base-method
- <clang-tidy/checks/bugprone/derived-method-shadowing-base-method>` check by
+ <clang-tidy/checks/bugprone/derived-method-shadowing-base-method>` check by
correctly ignoring function templates.
- Improved :doc:`bugprone-exception-escape
@@ -257,7 +257,7 @@ Changes in existing checks
<clang-tidy/checks/bugprone/std-namespace-modification>` check by fixing
false positives when extending the standard library with a specialization of
user-defined type and by removing detection of the compiler generated ``std``
- namespace extensions.
+ namespace extensions.
- Improved :doc:`bugprone-string-constructor
<clang-tidy/checks/bugprone/string-constructor>` check to detect suspicious
@@ -475,9 +475,13 @@ Changes in existing checks
to ``bool`` in C.
- Improved :doc:`readability-non-const-parameter
- <clang-tidy/checks/readability/non-const-parameter>` check by avoiding false
- positives on parameters used in dependent expressions (e.g. inside generic
- lambdas).
+ <clang-tidy/checks/readability/non-const-parameter>` check:
+
+ - Avoid false positives on parameters used in dependent expressions
+ (e.g. inside generic lambdas).
+
+ - Fixed a false positive in array subscript expressions where the types are
+ not yet resolved.
- Improved :doc:`readability-redundant-preprocessor
<clang-tidy/checks/readability/redundant-preprocessor>` check by fixing a
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
index c07312f989cea..eec29efb87bb0 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
@@ -372,3 +372,19 @@ void testGenericLambdaIssue177354() {
T x(*p);
};
}
+
+template <typename Index>
+void dependentArray1(double *ToFill, Index I, double FillValue) {
+ ToFill[I] = FillValue;
+}
+
+template <typename Index>
+void dependentArray2(double *ToFill, Index I, double FillValue) {
+ I[ToFill] = FillValue;
+}
+
+void useDependentArray() {
+ double ToFill[2] = {};
+ dependentArray1(ToFill, 0, 1.0);
+ dependentArray2(ToFill, 0, 1.0);
+}
>From 3947dc0aa6db429ad0ebb76d2e6d4a19fe0fe208 Mon Sep 17 00:00:00 2001
From: Mao Chuanjun <10255501521 at stu.ecnu.edu.cn>
Date: Wed, 8 Apr 2026 17:49:26 +0800
Subject: [PATCH 2/2] Update
clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
Co-authored-by: Zeyi Xu <zeyi2 at nekoarch.cc>
---
.../clang-tidy/readability/NonConstParameterCheck.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
index 75e851d285b54..ae93d299f8774 100644
--- a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
@@ -215,7 +215,6 @@ void NonConstParameterCheck::markCanNotBeConst(const Expr *E,
markCanNotBeConst(U->getSubExpr(), CanNotBeConst);
}
} else if (const auto *A = dyn_cast<ArraySubscriptExpr>(E)) {
- markCanNotBeConst(A->getBase(), true);
if (A->isInstantiationDependent()) {
markCanNotBeConst(A->getLHS(), true);
markCanNotBeConst(A->getRHS(), true);
More information about the cfe-commits
mailing list