Author: flovent
Date: 2025-10-10T21:13:49+08:00
New Revision: 1d3ad74667e86a1ba19285e58d14156cce3de89d

URL: 
https://github.com/llvm/llvm-project/commit/1d3ad74667e86a1ba19285e58d14156cce3de89d
DIFF: 
https://github.com/llvm/llvm-project/commit/1d3ad74667e86a1ba19285e58d14156cce3de89d.diff

LOG: [clang-tidy] Fix insertion location for certain function pointers in 
`cppcoreguidelines-init-variables` (#162218)

This patch starts to find terminator from `VarDecl`'s end location
rather than it's `getLocation()` to ignore terminator(`,`) in function
paramaters list.

Kind of follow up to #112091 

Closes #161978 .

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp

Removed: 
    


################################################################################
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 d2d79dcc92ec2..9aeda037bbdf9 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -307,6 +307,10 @@ Changes in existing checks
   an additional matcher that generalizes the copy-and-swap idiom pattern
   detection.
 
+- Improved :doc:`cppcoreguidelines-init-variables
+  <clang-tidy/checks/cppcoreguidelines/init-variables>` check by fixing the
+  insertion location for function pointers with multiple parameters.
+
 - Improved :doc:`cppcoreguidelines-prefer-member-initializer
   <clang-tidy/checks/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 6aa69642d39ed..8a8973a032bf2 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,23 @@ namespace gh112089 {
     }
 } // namespace gh112089
 
+namespace gh161978 {
+  void test() {
+    bool (*fp1)(int);
+    // 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]]: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]]: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]]: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;
+  }
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to