[clang-tools-extra] 2790e61 - [clang-tidy] Fix crash in readability-non-const-parameter with redecls (#200178)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 2 06:13:22 PDT 2026
Author: Zeyi Xu
Date: 2026-06-02T21:13:17+08:00
New Revision: 2790e61b41949e4ce61faebdb1faed614ca14416
URL: https://github.com/llvm/llvm-project/commit/2790e61b41949e4ce61faebdb1faed614ca14416
DIFF: https://github.com/llvm/llvm-project/commit/2790e61b41949e4ce61faebdb1faed614ca14416.diff
LOG: [clang-tidy] Fix crash in readability-non-const-parameter with redecls (#200178)
The check matches `VarDecls` with `hasInitializer()`, which uses
`getAnyInitializer()` and may therefore match a redeclaration whose
initializer is attached to another declaration. So calling
`IgnoreParenCasts()` on `VD->getInit()` directly would crash when that
redeclaration had no initializer of its own.
This commit fixes the problem by using a new matcher that only matches
VarDecls with an initializer on the current declaration.
Closes https://github.com/llvm/llvm-project/issues/199197
Added:
Modified:
clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
index cc12479bcd86e..fa9184a290273 100644
--- a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
@@ -14,6 +14,15 @@ using namespace clang::ast_matchers;
namespace clang::tidy::readability {
+namespace {
+AST_MATCHER_P(VarDecl, hasOwnInitializer, ast_matchers::internal::Matcher<Expr>,
+ InnerMatcher) {
+ const Expr *Initializer = Node.getInit();
+ return Initializer != nullptr &&
+ InnerMatcher.matches(*Initializer, Finder, Builder);
+}
+} // namespace
+
void NonConstParameterCheck::registerMatchers(MatchFinder *Finder) {
// Add parameters to Parameters.
Finder->addMatcher(parmVarDecl().bind("Parm"), this);
@@ -32,7 +41,7 @@ void NonConstParameterCheck::registerMatchers(MatchFinder *Finder) {
cxxUnresolvedConstructExpr()))
.bind("Mark"),
this);
- Finder->addMatcher(varDecl(hasInitializer(anything())).bind("Mark"), this);
+ Finder->addMatcher(varDecl(hasOwnInitializer(anything())).bind("Mark"), this);
}
void NonConstParameterCheck::check(const MatchFinder::MatchResult &Result) {
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 3bc902529a1dc..e6897ebb4cf9f 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -758,6 +758,9 @@ Changes in existing checks
- Fixed a false positive in array subscript expressions where the types are
not yet resolved.
+ - Fixed a crash when analyzing a redeclaration whose initializer is attached
+ to another declaration.
+
- Improved :doc:`readability-redundant-casting
<clang-tidy/checks/readability/redundant-casting>` check by adding the
`IgnoreImplicitCasts` option (default `false`) to flag casts as redundant
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 d3749285df071..b4918df347e5c 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
@@ -433,3 +433,19 @@ void dependentInitInGenericLambdaMultiArg() {
DependentCtor2<T> s(p, p);
};
}
+
+template <class T>
+struct StaticDepInClassInit {
+ static const T X = 0;
+};
+
+template <class T>
+const T StaticDepInClassInit<T>::X;
+
+template <class T>
+struct StaticDepOutOfClassInit {
+ static const T X;
+};
+
+template <class T>
+const T StaticDepOutOfClassInit<T>::X = 0;
More information about the cfe-commits
mailing list