[clang-tools-extra] [clang-tidy] fix fasle negatives in readability-redundant-casting when cast function pointer (PR #170502)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 3 08:14:12 PST 2025
https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/170502
Part of #170476
When check equal of type, we need to ignore ParenType
>From 03ca23b7f253a6ddebddd809c68d15a152ad0c80 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Thu, 4 Dec 2025 00:09:36 +0800
Subject: [PATCH] [clang-tidy] fix fasle negatives in
readability-redundant-casting when cast function pointer
Part of #170476
When check equal of type, we need to ignore ParenType
---
.../clang-tidy/readability/RedundantCastingCheck.cpp | 3 ++-
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
.../checkers/readability/redundant-casting.cpp | 10 ++++++++++
3 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp
index d11c41c33d2be..21f481a718219 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp
@@ -9,6 +9,7 @@
#include "RedundantCastingCheck.h"
#include "../utils/FixItHintUtils.h"
#include "clang/AST/ASTContext.h"
+#include "clang/AST/TypeBase.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
@@ -29,7 +30,7 @@ static bool areTypesEqual(QualType S, QualType D) {
const QualType PtrD = D->getPointeeType();
if (!PtrS.isNull() && !PtrD.isNull())
- return areTypesEqual(PtrS, PtrD);
+ return areTypesEqual(PtrS.IgnoreParens(), PtrD.IgnoreParens());
const DeducedType *DT = S->getContainedDeducedType();
if (DT && DT->isDeduced())
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 9533d56c219f7..b4d8c02a852a1 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -562,6 +562,10 @@ Changes in existing checks
<clang-tidy/checks/readability/qualified-auto>` check by adding the option
`IgnoreAliasing`, that allows not looking at underlying types of type aliases.
+- Improved :doc:`readability-redundant-casting
+ <clang-tidy/checks/readability/redundant-casting>` check by fixing false
+ negatives when explicitly cast from function pointer.
+
- Improved :doc:`readability-uppercase-literal-suffix
<clang-tidy/checks/readability/uppercase-literal-suffix>` check to recognize
literal suffixes added in C++23 and C23.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp
index fa91995c5615f..3e723b8b61d1d 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp
@@ -235,3 +235,13 @@ void testRedundantDependentNTTPCasting() {
// CHECK-MESSAGES: :[[@LINE-4]]:25: note: source type originates from referencing this non-type template parameter
// CHECK-FIXES: T a = V;
}
+
+namespace gh170476 {
+int f(void);
+int g1() {
+ int (*fp)() = (int(*)(void))&f;
+ // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: redundant explicit casting to the same type 'int (*)()' as the sub-expression, remove this casting [readability-redundant-casting]
+ // CHECK-FIXES: int (*fp)() = (&f);
+ return fp();
+}
+} // namespace gh170476
More information about the cfe-commits
mailing list