[clang-tools-extra] r307812 - [clang-tidy] Ignore blank spaces between cast's ")" and its sub expr.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 12 09:39:00 PDT 2017
Author: hokein
Date: Wed Jul 12 09:38:59 2017
New Revision: 307812
URL: http://llvm.org/viewvc/llvm-project?rev=307812&view=rev
Log:
[clang-tidy] Ignore blank spaces between cast's ")" and its sub expr.
Summary:
Before the change:
`auto i = (Enum) 5;` => `auto i = static_cast<Enum>( 5);`
After the change:
`auto i = (Enum) 5;` => `auto i = static_cast<Enum>(5);`
Reviewers: alexfh
Reviewed By: alexfh
Subscribers: JDevlieghere, xazax.hun, cfe-commits
Differential Revision: https://reviews.llvm.org/D31700
Modified:
clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.cpp
Modified: clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp?rev=307812&r1=307811&r2=307812&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp Wed Jul 12 09:38:59 2017
@@ -58,10 +58,9 @@ static bool pointedUnqualifiedTypesAreEq
void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
const auto *CastExpr = Result.Nodes.getNodeAs<CStyleCastExpr>("cast");
- auto ParenRange = CharSourceRange::getTokenRange(CastExpr->getLParenLoc(),
- CastExpr->getRParenLoc());
+
// Ignore casts in macros.
- if (ParenRange.getBegin().isMacroID() || ParenRange.getEnd().isMacroID())
+ if (CastExpr->getExprLoc().isMacroID())
return;
// Casting to void is an idiomatic way to mute "unused variable" and similar
@@ -82,6 +81,9 @@ void AvoidCStyleCastsCheck::check(const
const QualType SourceType = SourceTypeAsWritten.getCanonicalType();
const QualType DestType = DestTypeAsWritten.getCanonicalType();
+ auto ReplaceRange = CharSourceRange::getCharRange(
+ CastExpr->getLParenLoc(), CastExpr->getSubExprAsWritten()->getLocStart());
+
bool FnToFnCast =
isFunction(SourceTypeAsWritten) && isFunction(DestTypeAsWritten);
@@ -92,7 +94,7 @@ void AvoidCStyleCastsCheck::check(const
// pointer/reference types.
if (SourceTypeAsWritten == DestTypeAsWritten) {
diag(CastExpr->getLocStart(), "redundant cast to the same type")
- << FixItHint::CreateRemoval(ParenRange);
+ << FixItHint::CreateRemoval(ReplaceRange);
return;
}
}
@@ -136,7 +138,7 @@ void AvoidCStyleCastsCheck::check(const
getLangOpts()),
")");
}
- Diag << FixItHint::CreateReplacement(ParenRange, CastText);
+ Diag << FixItHint::CreateReplacement(ReplaceRange, CastText);
};
auto ReplaceWithNamedCast = [&](StringRef CastType) {
Diag << CastType;
Modified: clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.cpp?rev=307812&r1=307811&r2=307812&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.cpp Wed Jul 12 09:38:59 2017
@@ -85,6 +85,22 @@ void f(int a, double b, const char *cpc,
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
// CHECK-FIXES: b1 = (const int&)b;
+ b1 = (int) b;
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}; use static_cast {{.*}}
+ // CHECK-FIXES: b1 = static_cast<int>(b);
+
+ b1 = (int) b;
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}; use static_cast {{.*}}
+ // CHECK-FIXES: b1 = static_cast<int>(b);
+
+ b1 = (int) (b);
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}; use static_cast {{.*}}
+ // CHECK-FIXES: b1 = static_cast<int>(b);
+
+ b1 = (int) (b);
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}; use static_cast {{.*}}
+ // CHECK-FIXES: b1 = static_cast<int>(b);
+
Y *pB = (Y*)pX;
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
Y &rB = (Y&)*pX;
@@ -114,6 +130,14 @@ void f(int a, double b, const char *cpc,
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant cast to the same type
// CHECK-FIXES: {{^}} e = e;
+ e = (Enum) e;
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant cast to the same type
+ // CHECK-FIXES: {{^}} e = e;
+
+ e = (Enum) (e);
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant cast to the same type
+ // CHECK-FIXES: {{^}} e = (e);
+
static const int kZero = 0;
(int)kZero;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant cast to the same type
More information about the cfe-commits
mailing list