[clang-tidy] google-readability-casting: don't use constructor call syntax for const types

llvm-svn: 296858
This commit is contained in:
Alexander Kornienko 2017-03-03 08:18:49 +00:00
parent 69bccf96bd
commit db04cccb87
3 changed files with 12 additions and 3 deletions

View File

@ -149,7 +149,7 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
ReplaceWithNamedCast("static_cast");
return;
case CK_ConstructorConversion:
if (!DestTypeAsWritten.hasQualifiers() &&
if (!CastExpr->getTypeAsWritten().hasQualifiers() &&
DestTypeAsWritten->isRecordType() &&
!DestTypeAsWritten->isElaboratedTypeSpecifier()) {
Diag << "constructor call syntax";

View File

@ -17,6 +17,8 @@ void f(const char *cpc) {
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant cast to the same type [google-readability-casting]
// CHECK-FIXES: const char *cpc2 = cpc;
char *pc = (char*)cpc;
typedef const char *Typedef1;
(Typedef1)cpc;
}
#endif

View File

@ -81,6 +81,9 @@ void f(int a, double b, const char *cpc, const void *cpv, X *pX) {
int b1 = (int)b;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: {{.*}}; use static_cast [
// CHECK-FIXES: int b1 = static_cast<int>(b);
b1 = (const int&)b;
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
// CHECK-FIXES: b1 = (const int&)b;
Y *pB = (Y*)pX;
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
@ -271,11 +274,15 @@ void conversions() {
auto s2a = (struct S)"";
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: auto s2a = static_cast<struct S>("");
auto s2b = (const S)"";
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use static_cast [
// FIXME: This should be constructor call syntax: S("").
// CHECK-FIXES: auto s2b = static_cast<const S>("");
ConvertibleToS c;
auto s3 = (const S&)c;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: C-style casts are discouraged; use static_cast/const_cast/reinterpret_cast [
// CHECK-FIXES: auto s3 = (const S&)c;
// FIXME: This should be a static_cast
// FIXME: This should be a static_cast.
// C HECK-FIXES: auto s3 = static_cast<const S&>(c);
auto s4 = (S)c;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: C-style casts are discouraged; use constructor call syntax [
@ -284,7 +291,7 @@ void conversions() {
auto s5 = (const S&)cr;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: C-style casts are discouraged; use static_cast/const_cast/reinterpret_cast [
// CHECK-FIXES: auto s5 = (const S&)cr;
// FIXME: This should be a static_cast
// FIXME: This should be a static_cast.
// C HECK-FIXES: auto s5 = static_cast<const S&>(cr);
auto s6 = (S)cr;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: C-style casts are discouraged; use constructor call syntax [