Skip to content

Commit 72fe2d2

Browse files
[clang-tidy]misc-const-correctness fix fake positive of function pointer (#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 <[email protected]>
1 parent 066ee51 commit 72fe2d2

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) {
249249
CheckValue();
250250
if (WarnPointersAsPointers) {
251251
if (const auto *PT = dyn_cast<PointerType>(VT)) {
252-
if (!PT->getPointeeType().isConstQualified())
252+
if (!PT->getPointeeType().isConstQualified() &&
253+
!PT->getPointeeType()->isFunctionType())
253254
CheckPointee();
254255
}
255256
if (const auto *AT = dyn_cast<ArrayType>(VT)) {

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ Changes in existing checks
328328

329329
- Improved :doc:`misc-const-correctness
330330
<clang-tidy/checks/misc/const-correctness>` check to avoid false
331-
positives when pointers is tranferred to non-const references.
331+
positives when pointers is tranferred to non-const references
332+
and avoid false positives of function pointer.
332333

333334
- Improved :doc:`misc-header-include-cycle
334335
<clang-tidy/checks/misc/header-include-cycle>` check performance.

clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ void ignore_const_alias() {
4848
p_local0 = &a[1];
4949
}
5050

51+
void function_pointer_basic() {
52+
void (*const fp)() = nullptr;
53+
fp();
54+
}
55+
5156
void takeNonConstRef(int *&r);
5257

5358
void ignoreNonConstRefOps() {

0 commit comments

Comments
 (0)