[clang-tools-extra] [clang-tidy] fix fasle negatives in readability-redundant-casting when cast function pointer (PR #170502)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 3 08:14:48 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra
Author: Congcong Cai (HerrCai0907)
<details>
<summary>Changes</summary>
Part of #<!-- -->170476
When check equal of type, we need to ignore ParenType
---
Full diff: https://github.com/llvm/llvm-project/pull/170502.diff
3 Files Affected:
- (modified) clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp (+2-1)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
- (modified) clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp (+10)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/170502
More information about the cfe-commits
mailing list