brintos

brintos / llvm-project-archived public Read only

0
0
Text · 9.8 KiB · fa91995 Raw
238 lines · cpp
1// RUN: %check_clang_tidy -std=c++11,c++14,c++17 %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing2// RUN: %check_clang_tidy -std=c++11,c++14,c++17 -check-suffix=,MACROS %s readability-redundant-casting %t -- \3// RUN:   -config='{CheckOptions: { readability-redundant-casting.IgnoreMacros: false }}' \4// RUN:   -- -fno-delayed-template-parsing5// RUN: %check_clang_tidy -std=c++11,c++14,c++17 -check-suffix=,ALIASES %s readability-redundant-casting %t -- \6// RUN:   -config='{CheckOptions: { readability-redundant-casting.IgnoreTypeAliases: true }}' \7// RUN:   -- -fno-delayed-template-parsing8// RUN: %check_clang_tidy -std=c++20 %s readability-redundant-casting %t -- \9// RUN:   -- -fno-delayed-template-parsing -D CXX_20=110// RUN: %check_clang_tidy -std=c++20 -check-suffix=,MACROS %s readability-redundant-casting %t -- \11// RUN:   -config='{CheckOptions: { readability-redundant-casting.IgnoreMacros: false }}' \12// RUN:   -- -fno-delayed-template-parsing -D CXX_20=113// RUN: %check_clang_tidy -std=c++20 -check-suffix=,ALIASES %s readability-redundant-casting %t -- \14// RUN:   -config='{CheckOptions: { readability-redundant-casting.IgnoreTypeAliases: true }}' \15// RUN:   -- -fno-delayed-template-parsing -D CXX_20=116 17struct A {};18struct B : A {};19A getA();20 21void testRedundantStaticCasting(A& value) {22  A& a1 = static_cast<A&>(value);23  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]24  // CHECK-MESSAGES: :[[@LINE-3]]:36: note: source type originates from referencing this parameter25  // CHECK-FIXES: A& a1 = value;26}27 28void testRedundantConstCasting1(A& value) {29  A& a2 = const_cast<A&>(value);30  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]31  // CHECK-MESSAGES: :[[@LINE-3]]:36: note: source type originates from referencing this parameter32  // CHECK-FIXES: A& a2 = value;33}34 35void testRedundantConstCasting2(const A& value) {36  const A& a3 = const_cast<const A&>(value);37  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: redundant explicit casting to the same type 'const A' as the sub-expression, remove this casting [readability-redundant-casting]38  // CHECK-MESSAGES: :[[@LINE-3]]:42: note: source type originates from referencing this parameter39  // CHECK-FIXES: const A& a3 = value;40}41 42void testRedundantReinterpretCasting(A& value) {43  A& a4 = reinterpret_cast<A&>(value);44  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]45  // CHECK-MESSAGES: :[[@LINE-3]]:41: note: source type originates from referencing this parameter46  // CHECK-FIXES: A& a4 = value;47}48 49void testRedundantCCasting(A& value) {50  A& a5 = (A&)(value);51  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]52  // CHECK-MESSAGES: :[[@LINE-3]]:31: note: source type originates from referencing this parameter53  // CHECK-FIXES: A& a5 = value;54}55 56void testDoubleCasting(A& value) {57  A& a6 = static_cast<A&>(reinterpret_cast<A&>(value));58  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]59  // CHECK-MESSAGES: :[[@LINE-2]]:27: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]60  // CHECK-MESSAGES: :[[@LINE-4]]:27: note: source type originates from referencing this parameter61  // CHECK-FIXES: A& a6 = value;62}63 64void testDiffrentTypesCast(B& value) {65  A& a7 = static_cast<A&>(value);66}67 68#ifdef CXX_2069void testParenListInitExpr(A value) {70  B b = static_cast<B>(value);71}72#endif73 74void testCastingWithAuto() {75  auto a = getA();76  A& a8 = static_cast<A&>(a);77  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]78  // CHECK-MESSAGES: :[[@LINE-3]]:8: note: source type originates from referencing this variable79  // CHECK-FIXES: A& a8 = a;80}81 82void testCastingWithConstAuto() {83  const auto a = getA();84  const A& a9 = static_cast<const A&>(a);85  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: redundant explicit casting to the same type 'const A' as the sub-expression, remove this casting [readability-redundant-casting]86  // CHECK-MESSAGES: :[[@LINE-3]]:14: note: source type originates from referencing this variable87  // CHECK-FIXES: const A& a9 = a;88}89 90void testCastingWithAutoPtr(A& ptr) {91  auto* a = &ptr;92  A* a10 = static_cast<A*>(a);93  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant explicit casting to the same type 'A *' as the sub-expression, remove this casting [readability-redundant-casting]94  // CHECK-MESSAGES: :[[@LINE-3]]:9: note: source type originates from referencing this variable95  // CHECK-FIXES: A* a10 = a;96}97 98template<typename T>99void testRedundantTemplateCasting(T& value) {100  A& a = static_cast<A&>(value);101  T& t = static_cast<T&>(value);102  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: redundant explicit casting to the same type 'T' as the sub-expression, remove this casting [readability-redundant-casting]103  // CHECK-MESSAGES: :[[@LINE-4]]:38: note: source type originates from referencing this parameter104  // CHECK-FIXES: T& t = value;105}106 107void testTemplate() {108  A value;109  testRedundantTemplateCasting(value);110}111 112void testValidRefConstCast() {113  const auto a = getA();114  A& a11 = const_cast<A&>(a);115}116 117void testValidPtrConstCast(const A* ptr) {118  A* a12 = const_cast<A*>(ptr);119}120 121#define CAST(X) static_cast<int>(X)122 123void testMacroCasting(int value) {124  int a = CAST(value);125  // CHECK-MESSAGES-MACROS: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]126}127 128#define PTR_NAME name129 130void testMacroCasting(A* PTR_NAME) {131  A* a13 = static_cast<A*>(PTR_NAME);132  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant explicit casting to the same type 'A *' as the sub-expression, remove this casting [readability-redundant-casting]133  // CHECK-FIXES: A* a13 = PTR_NAME;134}135 136struct CastBool {137  operator bool() const {138    return true;139  }140};141 142void testUserOperatorCast(const CastBool& value) {143  bool b = static_cast<bool>(value);144}145 146using TypeA = A;147 148void testTypedefCast(A& value) {149  TypeA& a = static_cast<TypeA&>(value);150  // CHECK-MESSAGES-ALIASES: :[[@LINE-1]]:14: warning: redundant explicit casting to the same type 'TypeA' (aka 'A') as the sub-expression, remove this casting [readability-redundant-casting]151  // CHECK-FIXES-ALIASES: TypeA& a = value;152}153 154void testTypedefCast2(TypeA& value) {155  A& a = static_cast<A&>(value);156  // CHECK-MESSAGES-ALIASES: :[[@LINE-1]]:10: warning: redundant explicit casting to the same type 'A' as the sub-expression, remove this casting [readability-redundant-casting]157  // CHECK-FIXES-ALIASES: A& a = value;158}159 160void testFunctionalCastWithPrimitive(int a) {161  int b = int(a);162  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]163  // CHECK-FIXES: int b = a;164}165 166void testFunctionalCastWithInitExpr(unsigned a) {167  unsigned b = ~unsigned{!a};168  unsigned c = unsigned{0};169}170 171void testBinaryOperator(char c) {172  int a = int(c - 'C');173}174 175struct BIT {176  bool b:1;177};178 179template<typename ...Args>180void make(Args&& ...);181 182void testBinaryOperator(BIT b) {183  make((bool)b.b);184}185 186struct Class {187  using Iterator = const char*;188 189  Iterator begin() {190    return static_cast<Iterator>(first());191// CHECK-MESSAGES-ALIASES: :[[@LINE-1]]:12: warning: redundant explicit casting to the same type 'Iterator' (aka 'const char *') as the sub-expression, remove this casting [readability-redundant-casting]192// CHECK-MESSAGES-ALIASES: :[[@LINE+4]]:15: note: source type originates from the invocation of this method193// CHECK-FIXES-ALIASES: return first();194  }195 196  const char* first();197};198 199void testAddOperation(int aa, int bb) {200  int c = static_cast<int>(aa + bb) * aa;201 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]202 // CHECK-FIXES: int c = (aa + bb) * aa;203}204 205void testAddOperationWithParen(int a, int b) {206  int c = static_cast<int>((a+b))*a;207 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]208 // CHECK-FIXES: int c = (a+b)*a;209}210 211void testRValueCast(int&& a) {212  int&& b = static_cast<int&&>(a);213  int&& c = static_cast<int&&>(10);214  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]215  // CHECK-FIXES: int&& c = 10;216}217 218template <int V>219void testRedundantNTTPCasting() {220  int a = static_cast<int>(V);221  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]222  // CHECK-MESSAGES: :[[@LINE-4]]:15: note: source type originates from referencing this non-type template parameter223  // CHECK-FIXES: int a = V;224}225 226template <typename T, T V>227void testValidNTTPCasting() {228  int a = static_cast<int>(V);229}230 231template <typename T, T V>232void testRedundantDependentNTTPCasting() {233  T a = static_cast<T>(V);234  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: redundant explicit casting to the same type 'T' as the sub-expression, remove this casting [readability-redundant-casting]235  // CHECK-MESSAGES: :[[@LINE-4]]:25: note: source type originates from referencing this non-type template parameter236  // CHECK-FIXES: T a = V;237}238