2015-10-22 19:31:44 +08:00
// RUN: %check_clang_tidy %s google-readability-casting %t
2014-06-30 06:19:53 +08:00
bool g ( ) { return false ; }
2014-10-01 20:47:53 +08:00
enum Enum { Enum1 } ;
2014-07-14 15:37:05 +08:00
struct X { } ;
struct Y : public X { } ;
void f ( int a , double b , const char * cpc , const void * cpv , X * pX ) {
const char * cpc2 = ( const char * ) cpc ;
2015-01-29 23:17:13 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant cast to the same type [google-readability-casting]
2014-07-14 15:37:05 +08:00
// CHECK-FIXES: const char *cpc2 = cpc;
2015-01-29 23:17:13 +08:00
typedef const char * Typedef1 ;
typedef const char * Typedef2 ;
Typedef1 t1 ;
( Typedef2 ) t1 ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: C-style casts are discouraged; use static_cast (if needed, the cast may be redundant) [google-readability-casting]
// CHECK-FIXES: {{^}} static_cast<Typedef2>(t1);
2015-01-29 23:17:13 +08:00
( const char * ) t1 ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: {{.*}}; use static_cast (if needed
// CHECK-FIXES: {{^}} static_cast<const char*>(t1);
2015-01-29 23:17:13 +08:00
( Typedef1 ) cpc ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: {{.*}}; use static_cast (if needed
// CHECK-FIXES: {{^}} static_cast<Typedef1>(cpc);
2015-01-29 23:17:13 +08:00
( Typedef1 ) t1 ;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant cast to the same type
// CHECK-FIXES: {{^}} t1;
2014-07-14 15:37:05 +08:00
char * pc = ( char * ) cpc ;
2016-01-08 23:21:40 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use const_cast [google-readability-casting]
2014-10-01 20:47:53 +08:00
// CHECK-FIXES: char *pc = const_cast<char*>(cpc);
2017-03-02 23:27:34 +08:00
typedef char Char ;
Char * pChar = ( Char * ) pc ;
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: {{.*}}; use static_cast (if needed
// CHECK-FIXES: {{^}} Char *pChar = static_cast<Char*>(pc);
( Char ) * cpc ;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: {{.*}}; use static_cast (if needed
// CHECK-FIXES: {{^}} static_cast<Char>(*cpc);
( char ) * pChar ;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: {{.*}}; use static_cast (if needed
// CHECK-FIXES: {{^}} static_cast<char>(*pChar);
( const char * ) cpv ;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: {{.*}}; use static_cast [
// CHECK-FIXES: static_cast<const char*>(cpv);
2014-07-14 15:37:05 +08:00
char * pc2 = ( char * ) ( cpc + 33 ) ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}; use const_cast [
2014-10-01 20:47:53 +08:00
// CHECK-FIXES: char *pc2 = const_cast<char*>(cpc + 33);
2014-07-14 15:37:05 +08:00
const char & crc = * cpc ;
char & rc = ( char & ) crc ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: {{.*}}; use const_cast [
2014-10-01 20:47:53 +08:00
// CHECK-FIXES: char &rc = const_cast<char&>(crc);
2014-07-14 15:37:05 +08:00
char & rc2 = ( char & ) * cpc ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}; use const_cast [
2014-10-01 20:47:53 +08:00
// CHECK-FIXES: char &rc2 = const_cast<char&>(*cpc);
2014-07-14 15:37:05 +08:00
char * * const * const * ppcpcpc ;
char * * * * ppppc = ( char * * * * ) ppcpcpc ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: {{.*}}; use const_cast [
2014-10-01 20:47:53 +08:00
// CHECK-FIXES: char ****ppppc = const_cast<char****>(ppcpcpc);
2014-07-14 15:37:05 +08:00
2014-07-16 21:38:48 +08:00
char * * * pppc = ( char * * * ) * ( ppcpcpc ) ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: {{.*}}; use const_cast [
2014-10-01 20:47:53 +08:00
// CHECK-FIXES: char ***pppc = const_cast<char***>(*(ppcpcpc));
2014-07-16 21:38:48 +08:00
char * * * pppc2 = ( char * * * ) ( * ppcpcpc ) ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: {{.*}}; use const_cast [
2014-10-01 20:47:53 +08:00
// CHECK-FIXES: char ***pppc2 = const_cast<char***>(*ppcpcpc);
2014-07-16 21:38:48 +08:00
char * pc5 = ( char * ) ( const char * ) ( cpv ) ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}; use const_cast [
// CHECK-MESSAGES: :[[@LINE-2]]:22: warning: {{.*}}; use static_cast [
// CHECK-FIXES: char *pc5 = const_cast<char*>(static_cast<const char*>(cpv));
2014-07-16 21:38:48 +08:00
2014-06-30 06:19:53 +08:00
int b1 = ( int ) b ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: {{.*}}; use static_cast [
2014-07-14 15:37:05 +08:00
// CHECK-FIXES: int b1 = static_cast<int>(b);
Y * pB = ( Y * ) pX ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
2014-07-14 15:37:05 +08:00
Y & rB = ( Y & ) * pX ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
2014-06-30 06:19:53 +08:00
2014-07-14 15:37:05 +08:00
const char * pc3 = ( const char * ) cpv ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: {{.*}}; use static_cast [
// CHECK-FIXES: const char *pc3 = static_cast<const char*>(cpv);
2014-07-14 15:37:05 +08:00
char * pc4 = ( char * ) cpv ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
2014-07-14 15:37:05 +08:00
// CHECK-FIXES: char *pc4 = (char*)cpv;
2014-10-01 20:47:53 +08:00
b1 = ( int ) Enum1 ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}; use static_cast [
2014-10-01 20:47:53 +08:00
// CHECK-FIXES: b1 = static_cast<int>(Enum1);
Enum e = ( Enum ) b1 ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: {{.*}}; use static_cast [
2014-10-01 20:47:53 +08:00
// CHECK-FIXES: Enum e = static_cast<Enum>(b1);
2017-03-02 23:47:28 +08:00
e = ( Enum ) Enum1 ;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant cast to the same type
// CHECK-FIXES: {{^}} e = Enum1;
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
// CHECK-FIXES: {{^}} kZero;
2014-06-30 06:19:53 +08:00
int b2 = int ( b ) ;
int b3 = static_cast < double > ( b ) ;
int b4 = b ;
double aa = a ;
( void ) b2 ;
return ( void ) g ( ) ;
}
2014-07-14 15:37:05 +08:00
template < typename T >
void template_function ( T t , int n ) {
int i = ( int ) t ;
2017-03-02 23:27:34 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
2014-07-14 15:37:05 +08:00
// CHECK-FIXES: int i = (int)t;
int j = ( int ) n ;
2015-01-29 23:17:13 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant cast to the same type
2014-07-14 15:37:05 +08:00
// CHECK-FIXES: int j = n;
}
template < typename T >
struct TemplateStruct {
void f ( T t , int n ) {
int k = ( int ) t ;
2016-01-08 23:21:40 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast
2014-07-14 15:37:05 +08:00
// CHECK-FIXES: int k = (int)t;
int l = ( int ) n ;
2015-01-29 23:17:13 +08:00
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant cast to the same type
2014-07-14 15:37:05 +08:00
// CHECK-FIXES: int l = n;
}
} ;
void test_templates ( ) {
template_function ( 1 , 42 ) ;
template_function ( 1.0 , 42 ) ;
TemplateStruct < int > ( ) . f ( 1 , 42 ) ;
TemplateStruct < double > ( ) . f ( 1.0 , 42 ) ;
}
2015-01-29 23:17:13 +08:00
extern " C " {
void extern_c_code ( const char * cpc ) {
const char * cpc2 = ( const char * ) cpc ;
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant cast to the same type
// CHECK-FIXES: const char *cpc2 = cpc;
char * pc = ( char * ) cpc ;
}
}
2014-07-14 15:37:05 +08:00
# define CAST(type, value) (type)(value)
void macros ( double d ) {
int i = CAST ( int , d ) ;
}
2014-06-30 06:19:53 +08:00
enum E { E1 = 1 } ;
template < E e >
2014-07-14 15:37:05 +08:00
struct A {
// Usage of template argument e = E1 is represented as (E)1 in the AST for
// some reason. We have a special treatment of this case to avoid warnings
// here.
static const E ee = e ;
} ;
2014-06-30 06:19:53 +08:00
struct B : public A < E1 > { } ;
2017-02-10 22:57:19 +08:00
void overloaded_function ( ) ;
void overloaded_function ( int ) ;
template < typename Fn >
void g ( Fn fn ) {
fn ( ) ;
}
void function_casts ( ) {
typedef void ( * FnPtrVoid ) ( ) ;
typedef void ( & FnRefVoid ) ( ) ;
typedef void ( & FnRefInt ) ( int ) ;
g ( ( void ( * ) ( ) ) overloaded_function ) ;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: g(static_cast<void (*)()>(overloaded_function));
g ( ( void ( * ) ( ) ) & overloaded_function ) ;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: g(static_cast<void (*)()>(&overloaded_function));
g ( ( void ( & ) ( ) ) overloaded_function ) ;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: g(static_cast<void (&)()>(overloaded_function));
g ( ( FnPtrVoid ) overloaded_function ) ;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: g(static_cast<FnPtrVoid>(overloaded_function));
g ( ( FnPtrVoid ) & overloaded_function ) ;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: g(static_cast<FnPtrVoid>(&overloaded_function));
g ( ( FnRefVoid ) overloaded_function ) ;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: g(static_cast<FnRefVoid>(overloaded_function));
FnPtrVoid fn0 = ( void ( * ) ( ) ) & overloaded_function ;
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: FnPtrVoid fn0 = static_cast<void (*)()>(&overloaded_function);
FnPtrVoid fn1 = ( void ( * ) ( ) ) overloaded_function ;
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: FnPtrVoid fn1 = static_cast<void (*)()>(overloaded_function);
FnPtrVoid fn1a = ( FnPtrVoid ) overloaded_function ;
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: FnPtrVoid fn1a = static_cast<FnPtrVoid>(overloaded_function);
FnRefInt fn2 = ( void ( & ) ( int ) ) overloaded_function ;
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: FnRefInt fn2 = static_cast<void (&)(int)>(overloaded_function);
auto fn3 = ( void ( * ) ( ) ) & overloaded_function ;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: auto fn3 = static_cast<void (*)()>(&overloaded_function);
auto fn4 = ( void ( * ) ( ) ) overloaded_function ;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: auto fn4 = static_cast<void (*)()>(overloaded_function);
auto fn5 = ( void ( & ) ( int ) ) overloaded_function ;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: auto fn5 = static_cast<void (&)(int)>(overloaded_function);
void ( * fn6 ) ( ) = ( void ( * ) ( ) ) & overloaded_function ;
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: void (*fn6)() = static_cast<void (*)()>(&overloaded_function);
void ( * fn7 ) ( ) = ( void ( * ) ( ) ) overloaded_function ;
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: void (*fn7)() = static_cast<void (*)()>(overloaded_function);
void ( * fn8 ) ( ) = ( FnPtrVoid ) overloaded_function ;
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: void (*fn8)() = static_cast<FnPtrVoid>(overloaded_function);
void ( & fn9 ) ( int ) = ( void ( & ) ( int ) ) overloaded_function ;
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: C-style casts are discouraged; use static_cast [
// CHECK-FIXES: void (&fn9)(int) = static_cast<void (&)(int)>(overloaded_function);
void ( * correct1 ) ( ) = static_cast < void ( * ) ( ) > ( overloaded_function ) ;
FnPtrVoid correct2 = static_cast < void ( * ) ( ) > ( & overloaded_function ) ;
FnRefInt correct3 = static_cast < void ( & ) ( int ) > ( overloaded_function ) ;
}
2017-02-18 17:45:00 +08:00
struct S {
S ( const char * ) ;
} ;
struct ConvertibleToS {
operator S ( ) const ;
} ;
struct ConvertibleToSRef {
operator const S & ( ) const ;
} ;
void conversions ( ) {
//auto s1 = (const S&)"";
// C HECK-MESSAGES: :[[@LINE-1]]:10: warning: C-style casts are discouraged; use static_cast [
// C HECK-FIXES: S s1 = static_cast<const S&>("");
auto s2 = ( S ) " " ;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: C-style casts are discouraged; use constructor call syntax [
// CHECK-FIXES: auto s2 = S("");
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>("");
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
// 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 [
// CHECK-FIXES: auto s4 = S(c);
ConvertibleToSRef cr ;
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
// 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 [
// CHECK-FIXES: auto s6 = S(cr);
}