Skip to content

Conversation

Changqing-JING
Copy link
Contributor

@Changqing-JING Changqing-JING commented Oct 6, 2025

Fix clang-tidy misc-const-correctness of function pointer, because function pointer can't be declared as const*const

void function_pointer_basic() {
  void (*const fp)() = nullptr;
  fp();
}


test.cpp:2:3: warning: pointee of variable 'fp' of type 'void (*const)()' can be declared 'const' [misc-const-correctness]
    2 |   void (*const fp)() = nullptr;
      |   ^
      |                const 

Copy link

github-actions bot commented Oct 6, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Oct 6, 2025

@llvm/pr-subscribers-clang-tidy

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

Author: Changqing Jing (Changqing-JING)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/162079.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp (+2-1)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp (+4)
diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
index b93f3d6a5a13b..8c25e928667df 100644
--- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
@@ -249,7 +249,8 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) {
       CheckValue();
     if (WarnPointersAsPointers) {
       if (const auto *PT = dyn_cast<PointerType>(VT)) {
-        if (!PT->getPointeeType().isConstQualified())
+        if (!PT->getPointeeType().isConstQualified() &&
+            !PT->getPointeeType()->isFunctionType())
           CheckPointee();
       }
       if (const auto *AT = dyn_cast<ArrayType>(VT)) {
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 7e836a7114d50..512527abf608a 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -321,6 +321,10 @@ Changes in existing checks
 - Improved :doc:`misc-header-include-cycle
   <clang-tidy/checks/misc/header-include-cycle>` check performance.
 
+- Improved :doc:`misc-const-correctness
+  <clang-tidy/checks/misc/const-correctness>` check by fixing false positives
+  of function pointer.
+
 - Improved :doc:`modernize-avoid-c-arrays
   <clang-tidy/checks/modernize/avoid-c-arrays>` to not diagnose array types
   which are part of an implicit instantiation of a template.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp
index 2ef47266b02b0..c2c939b9ba32e 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp
@@ -48,3 +48,7 @@ void ignore_const_alias() {
   p_local0 = &a[1];
 }
 
+void function_pointer_basic() {
+  void (*const fp)() = nullptr;
+  fp();
+}

Copy link
Contributor

@HerrCai0907 HerrCai0907 left a comment

Choose a reason for hiding this comment

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

LGTM

@HerrCai0907 HerrCai0907 enabled auto-merge (squash) October 9, 2025 02:37
@HerrCai0907 HerrCai0907 merged commit 72fe2d2 into llvm:main Oct 9, 2025
9 of 10 checks passed
Copy link

github-actions bot commented Oct 9, 2025

@Changqing-JING Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

svkeerthy pushed a commit that referenced this pull request Oct 9, 2025
…ter (#162079)

Fix clang-tidy misc-const-correctness of function pointer, because
function pointer can't be declared as `const*const`

```
void function_pointer_basic() {
  void (*const fp)() = nullptr;
  fp();
}


test.cpp:2:3: warning: pointee of variable 'fp' of type 'void (*const)()' can be declared 'const' [misc-const-correctness]
    2 |   void (*const fp)() = nullptr;
      |   ^
      |                const 
```

---------

Co-authored-by: Congcong Cai <congcongcai0907@163.com>
clingfei pushed a commit to clingfei/llvm-project that referenced this pull request Oct 10, 2025
…ter (llvm#162079)

Fix clang-tidy misc-const-correctness of function pointer, because
function pointer can't be declared as `const*const`

```
void function_pointer_basic() {
  void (*const fp)() = nullptr;
  fp();
}


test.cpp:2:3: warning: pointee of variable 'fp' of type 'void (*const)()' can be declared 'const' [misc-const-correctness]
    2 |   void (*const fp)() = nullptr;
      |   ^
      |                const 
```

---------

Co-authored-by: Congcong Cai <congcongcai0907@163.com>
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.

4 participants