From da42dcf7d2d840d5668f7ef3e09da6ebd7c4d7c2 Mon Sep 17 00:00:00 2001 From: flovent Date: Tue, 7 Oct 2025 11:08:44 +0800 Subject: [PATCH 1/3] [clang-tidy] Fix insertion location for certain function pointers in `cppcoreguidelines-init-variables` --- .../cppcoreguidelines/InitVariablesCheck.cpp | 2 +- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ .../cppcoreguidelines/init-variables.cpp | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp index ed595e1148dec..2545548df4f45 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp @@ -108,7 +108,7 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) { << MatchedDecl; if (*InitializationString != nullptr) Diagnostic << FixItHint::CreateInsertion( - utils::lexer::findNextTerminator(MatchedDecl->getLocation(), + utils::lexer::findNextTerminator(MatchedDecl->getEndLoc(), *Result.SourceManager, Result.Context->getLangOpts()), *InitializationString); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 7e836a7114d50..40ed2fa7ee791 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -299,6 +299,10 @@ Changes in existing checks an additional matcher that generalizes the copy-and-swap idiom pattern detection. +- Improved :doc:`cppcoreguidelines-init-variables + ` check by fixing the + insertion location for function pointers with multiple parameters. + - Improved :doc:`cppcoreguidelines-prefer-member-initializer ` check to avoid false positives on inherited members in class templates. 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 824431c1bf52f..3a198c91e85c0 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 @@ -148,3 +148,19 @@ namespace gh112089 { } } // namespace gh112089 +namespace gh161978 { + void test() { + bool (*fp1)(int); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'fp1' is not initialized [cppcoreguidelines-init-variables] + // CHECK-FIXES: bool (*fp1)(int) = nullptr; + bool (*fp2)(int, int); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'fp2' is not initialized [cppcoreguidelines-init-variables] + // CHECK-FIXES: bool (*fp2)(int, int) = nullptr; + bool (*fp3)(int, int, int); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'fp3' is not initialized [cppcoreguidelines-init-variables] + // CHECK-FIXES: bool (*fp3)(int, int, int) = nullptr; + bool (*fp4)(int, int, int, ...); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'fp4' is not initialized [cppcoreguidelines-init-variables] + // CHECK-FIXES: bool (*fp4)(int, int, int, ...) = nullptr; + } +} From d2991c46c39d1ffc3df6bef30fa9b85dabf9945b Mon Sep 17 00:00:00 2001 From: flovent Date: Tue, 7 Oct 2025 11:23:25 +0800 Subject: [PATCH 2/3] [NFC] Fix test: wrong message column --- .../checkers/cppcoreguidelines/init-variables.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 3a198c91e85c0..2ca4ccad5532b 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 @@ -151,16 +151,16 @@ namespace gh112089 { namespace gh161978 { void test() { bool (*fp1)(int); - // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'fp1' is not initialized [cppcoreguidelines-init-variables] + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp1' is not initialized [cppcoreguidelines-init-variables] // CHECK-FIXES: bool (*fp1)(int) = nullptr; bool (*fp2)(int, int); - // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'fp2' is not initialized [cppcoreguidelines-init-variables] + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp2' is not initialized [cppcoreguidelines-init-variables] // CHECK-FIXES: bool (*fp2)(int, int) = nullptr; bool (*fp3)(int, int, int); - // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'fp3' is not initialized [cppcoreguidelines-init-variables] + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp3' is not initialized [cppcoreguidelines-init-variables] // CHECK-FIXES: bool (*fp3)(int, int, int) = nullptr; bool (*fp4)(int, int, int, ...); - // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: variable 'fp4' is not initialized [cppcoreguidelines-init-variables] + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp4' is not initialized [cppcoreguidelines-init-variables] // CHECK-FIXES: bool (*fp4)(int, int, int, ...) = nullptr; } } From 8cac8da6e4c5b809e17c9f709195d225803e4cd3 Mon Sep 17 00:00:00 2001 From: flovent Date: Tue, 7 Oct 2025 23:17:26 +0800 Subject: [PATCH 3/3] [NFC] Add test about multi `VarDecl` in one `DeclStmt` --- .../clang-tidy/checkers/cppcoreguidelines/init-variables.cpp | 4 ++++ 1 file changed, 4 insertions(+) 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 2ca4ccad5532b..312a6139fb879 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 @@ -162,5 +162,9 @@ namespace gh161978 { bool (*fp4)(int, int, int, ...); // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp4' is not initialized [cppcoreguidelines-init-variables] // CHECK-FIXES: bool (*fp4)(int, int, int, ...) = nullptr; + bool (*fp5)(int, int), (*fp6)(int, int); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'fp5' is not initialized [cppcoreguidelines-init-variables] + // CHECK-MESSAGES: :[[@LINE-2]]:30: warning: variable 'fp6' is not initialized [cppcoreguidelines-init-variables] + // CHECK-FIXES: bool (*fp5)(int, int) = nullptr, (*fp6)(int, int) = nullptr; } }