Skip to content

Commit 889e60d

Browse files
authored
[clang-tidy] Ignore casts from void to void in bugprone-casting-through-void (#90566)
Improved bugprone-casting-through-void check by ignoring casts where source is already a void pointer, making middle void pointer casts bug-free. Closes #87069
1 parent a370d57 commit 889e60d

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "CastingThroughVoidCheck.h"
10-
#include "clang/AST/ASTContext.h"
1110
#include "clang/AST/Expr.h"
1211
#include "clang/AST/Type.h"
1312
#include "clang/ASTMatchers/ASTMatchFinder.h"
1413
#include "clang/ASTMatchers/ASTMatchers.h"
15-
#include "llvm/ADT/StringSet.h"
1614

1715
using namespace clang::ast_matchers;
1816

@@ -27,7 +25,8 @@ void CastingThroughVoidCheck::registerMatchers(MatchFinder *Finder) {
2725
hasSourceExpression(
2826
explicitCastExpr(
2927
hasSourceExpression(
30-
expr(hasType(qualType().bind("source_type")))),
28+
expr(hasType(qualType(unless(pointsTo(voidType())))
29+
.bind("source_type")))),
3130
hasDestinationType(
3231
qualType(pointsTo(voidType())).bind("void_type")))
3332
.bind("cast"))),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ Changes in existing checks
173173
<clang-tidy/checks/bugprone/assert-side-effect>` check by detecting side
174174
effect from calling a method with non-const reference parameters.
175175

176+
- Improved :doc:`bugprone-casting-through-void
177+
<clang-tidy/checks/bugprone/casting-through-void>` check by ignoring casts
178+
where source is already a ``void``` pointer, making middle ``void`` pointer
179+
casts bug-free.
180+
176181
- Improved :doc:`bugprone-forwarding-reference-overload
177182
<clang-tidy/checks/bugprone/forwarding-reference-overload>`
178183
check to ignore deleted constructors which won't hide other overloads.

clang-tools-extra/test/clang-tidy/checkers/bugprone/casting-through-void.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,10 @@ void bit_cast() {
8989
__builtin_bit_cast(int *, static_cast<void *>(&d));
9090
// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: do not cast 'double *' to 'int *' through 'void *' [bugprone-casting-through-void]
9191
}
92+
93+
namespace PR87069 {
94+
void castconstVoidToVoid() {
95+
const void* ptr = nullptr;
96+
int* numberPtr = static_cast<int*>(const_cast<void*>(ptr));
97+
}
98+
}

0 commit comments

Comments
 (0)