Skip to content

Conversation

flovent
Copy link
Contributor

@flovent flovent commented Oct 7, 2025

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 .

@llvmbot
Copy link
Member

llvmbot commented Oct 7, 2025

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: None (flovent)

Changes

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 .


Full diff: https://github.com/llvm/llvm-project/pull/162218.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 (+16)
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
+  <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 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;
+  }
+}

Copy link
Contributor

@localspook localspook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM apart from one suggestion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang-tidy: cppcoreguidelines-init-variables does not work for a function pointer that has two or more parameters
3 participants