Skip to content

Commit da9015a

Browse files
authored
[clang-tidy] Provide fix-its for downcasts in google-readability-casting (#165411)
1 parent 9dfd14a commit da9015a

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
269269
return;
270270
}
271271
break;
272+
case CK_BaseToDerived:
273+
if (!needsConstCast(SourceType, DestType)) {
274+
ReplaceWithNamedCast("static_cast");
275+
return;
276+
}
277+
break;
272278
default:
273279
break;
274280
}

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,10 @@ Changes in existing checks
402402
adding an option to allow pointer arithmetic via prefix/postfix increment or
403403
decrement operators.
404404

405+
- Improved :doc:`google-readability-casting
406+
<clang-tidy/checks/google/readability-casting>` check by adding fix-it
407+
notes for downcasts.
408+
405409
- Improved :doc:`llvm-prefer-isa-or-dyn-cast-in-conditionals
406410
<clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals>` check:
407411

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ void f(int a, double b, const char *cpc, const void *cpv, X *pX) {
102102
// CHECK-FIXES: b1 = static_cast<int>(b);
103103

104104
Y *pB = (Y*)pX;
105-
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
105+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast {{.*}}
106+
// CHECK-FIXES: Y *pB = static_cast<Y*>(pX);
106107
Y &rB = (Y&)*pX;
107-
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
108+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast {{.*}}
109+
// CHECK-FIXES: Y &rB = static_cast<Y&>(*pX);
108110

109111
const char *pc3 = (const char*)cpv;
110112
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: {{.*}}; use static_cast [

0 commit comments

Comments
 (0)