https://github.com/zeyi2 created https://github.com/llvm/llvm-project/pull/180973
Closes #180894 >From 275db8750347d559d13466580fab16844806ccfa Mon Sep 17 00:00:00 2001 From: mtx <[email protected]> Date: Thu, 12 Feb 2026 00:45:44 +0800 Subject: [PATCH] [clang-tidy] Add support for member pointers in cppcoreguidelines-init-variables --- .../cppcoreguidelines/InitVariablesCheck.cpp | 2 +- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../cppcoreguidelines/init-variables.cpp | 33 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) 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; +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
