[clang-tools-extra] [clang-tidy] Add support for member pointers in cppcoreguidelines-init-variables (PR #180973)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 11 08:52:04 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
Author: mitchell (zeyi2)
<details>
<summary>Changes</summary>
Closes #<!-- -->180894
---
Full diff: https://github.com/llvm/llvm-project/pull/180973.diff
3 Files Affected:
- (modified) clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp (+1-1)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
- (modified) clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp (+33)
``````````diff
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
index 93b5b96926865..770d1c8e55fef 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
@@ -95,7 +95,7 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
else if (TypePtr->isFloatingType()) {
InitializationString = " = NAN";
AddMathInclude = true;
- } else if (TypePtr->isAnyPointerType()) {
+ } else if (TypePtr->isAnyPointerType() || TypePtr->isMemberPointerType()) {
if (getLangOpts().CPlusPlus11)
InitializationString = " = nullptr";
else
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 0ad69f5fdc5aa..d1ddc57a02c5d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -158,6 +158,10 @@ Changes in existing checks
the invalidating function in the warning message when a custom invalidation
function is used (via the `InvalidationFunctions` option).
+- Improved :doc:`cppcoreguidelines-init-variables
+ <clang-tidy/checks/cppcoreguidelines/init-variables>` check by ensuring that
+ member pointers are correctly flagged as uninitialized.
+
- Improved :doc:`cppcoreguidelines-pro-type-vararg
<clang-tidy/checks/cppcoreguidelines/pro-type-vararg>` check by no longer
warning on builtins with custom type checking (e.g., type-generic builtins
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp
index 8a8973a032bf2..e4a68fea59b52 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp
@@ -168,3 +168,36 @@ namespace gh161978 {
// CHECK-FIXES: bool (*fp5)(int, int) = nullptr, (*fp6)(int, int) = nullptr;
}
}
+
+void gh180894() {
+ struct S {
+ int x;
+ };
+
+ int S::* mp;
+ // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'mp' is not initialized [cppcoreguidelines-init-variables]
+ // CHECK-FIXES: int S::* mp = nullptr;
+
+ int S::* mp1 = nullptr, S::* mp2;
+ // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: variable 'mp2' is not initialized [cppcoreguidelines-init-variables]
+ // CHECK-FIXES: int S::* mp1 = nullptr, S::* mp2 = nullptr;
+
+ int S::* mp3, S::* mp4 = &S::x;
+ // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'mp3' is not initialized [cppcoreguidelines-init-variables]
+ // CHECK-FIXES: int S::* mp3 = nullptr, S::* mp4 = &S::x;
+
+ using MemPtr = int S::*;
+ MemPtr mp5;
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: variable 'mp5' is not initialized [cppcoreguidelines-init-variables]
+ // CHECK-FIXES: MemPtr mp5 = nullptr;
+
+ struct S1 {
+ int x;
+ int y;
+ };
+
+ int S::* mp6, S1::* mp7;
+ // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'mp6' is not initialized [cppcoreguidelines-init-variables]
+ // CHECK-MESSAGES: :[[@LINE-2]]:23: warning: variable 'mp7' is not initialized [cppcoreguidelines-init-variables]
+ // CHECK-FIXES: int S::* mp6 = nullptr, S1::* mp7 = nullptr;
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/180973
More information about the cfe-commits
mailing list