-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang-tidy] fix false negatives in readability-redundant-casting when cast function pointer #170502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…n cast function pointer Part of llvm#170476 When check equal of type, we need to ignore ParenType
|
@llvm/pr-subscribers-clang-tools-extra Author: Congcong Cai (HerrCai0907) ChangesPart of #170476 Full diff: https://github.com/llvm/llvm-project/pull/170502.diff 3 Files Affected:
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
|
|
@llvm/pr-subscribers-clang-tidy Author: Congcong Cai (HerrCai0907) ChangesPart of #170476 Full diff: https://github.com/llvm/llvm-project/pull/170502.diff 3 Files Affected:
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
|
vbvictor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
| #include "RedundantCastingCheck.h" | ||
| #include "../utils/FixItHintUtils.h" | ||
| #include "clang/AST/ASTContext.h" | ||
| #include "clang/AST/TypeBase.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed?
…n cast function pointer (llvm#170502) Part of llvm#170476 When check equal of type, we need to ignore ParenType
…n cast function pointer (llvm#170502) Part of llvm#170476 When check equal of type, we need to ignore ParenType
Part of #170476
When check equal of type, we need to ignore ParenType