Skip to content

Commit 81a0863

Browse files
authored
[clang-tidy] fix false negatives in readability-redundant-casting when cast function pointer (#170502)
Part of #170476 When check equal of type, we need to ignore ParenType
1 parent bc2a64f commit 81a0863

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "RedundantCastingCheck.h"
1010
#include "../utils/FixItHintUtils.h"
1111
#include "clang/AST/ASTContext.h"
12+
#include "clang/AST/TypeBase.h"
1213
#include "clang/ASTMatchers/ASTMatchFinder.h"
1314
#include "clang/Lex/Lexer.h"
1415

@@ -29,7 +30,7 @@ static bool areTypesEqual(QualType S, QualType D) {
2930
const QualType PtrD = D->getPointeeType();
3031

3132
if (!PtrS.isNull() && !PtrD.isNull())
32-
return areTypesEqual(PtrS, PtrD);
33+
return areTypesEqual(PtrS.IgnoreParens(), PtrD.IgnoreParens());
3334

3435
const DeducedType *DT = S->getContainedDeducedType();
3536
if (DT && DT->isDeduced())

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,10 @@ Changes in existing checks
567567
<clang-tidy/checks/readability/qualified-auto>` check by adding the option
568568
`IgnoreAliasing`, that allows not looking at underlying types of type aliases.
569569

570+
- Improved :doc:`readability-redundant-casting
571+
<clang-tidy/checks/readability/redundant-casting>` check by fixing false
572+
negatives when explicitly cast from function pointer.
573+
570574
- Improved :doc:`readability-uppercase-literal-suffix
571575
<clang-tidy/checks/readability/uppercase-literal-suffix>` check to recognize
572576
literal suffixes added in C++23 and C23.

clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,13 @@ void testRedundantDependentNTTPCasting() {
235235
// CHECK-MESSAGES: :[[@LINE-4]]:25: note: source type originates from referencing this non-type template parameter
236236
// CHECK-FIXES: T a = V;
237237
}
238+
239+
namespace gh170476 {
240+
int f(void);
241+
int g1() {
242+
int (*fp)() = (int(*)(void))&f;
243+
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: redundant explicit casting to the same type 'int (*)()' as the sub-expression, remove this casting [readability-redundant-casting]
244+
// CHECK-FIXES: int (*fp)() = (&f);
245+
return fp();
246+
}
247+
} // namespace gh170476

0 commit comments

Comments
 (0)