1 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -Wcast-qual -verify %s
5 // do *NOT* warn on const_cast<>()
6 // use clang-tidy's cppcoreguidelines-pro-type-const-cast for that.
8 const char *const ptr
= 0;
9 char *t0
= const_cast<char *>(ptr
); // no warning
11 volatile char *ptr2
= 0;
12 char *t1
= const_cast<char *>(ptr2
); // no warning
14 const volatile char *ptr3
= 0;
15 char *t2
= const_cast<char *>(ptr3
); // no warning
19 void* p0
= (void*)(const void*)"txt"; // expected-warning {{cast from 'const void *' to 'void *' drops const qualifier}}
20 void* p1
= (void*)"txt"; // FIXME
21 char* p2
= (char*)"txt"; // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
27 const int &a0
= a
; // no warning
28 const int &a1
= (const int &)a
; // no warning
30 int &a2
= (int &)a
; // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
31 const int &a3
= (int &)a
; // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
32 int &a4
= (int &)((const int &)a
); // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
33 int &a5
= (int &)((int &)a
); // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
34 const int &a6
= (int &)((int &)a
); // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
35 const int &a7
= (int &)((const int &)a
); // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
36 const int &a8
= (const int &)((int &)a
); // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
42 volatile int &a0
= a
; // no warning
43 volatile int &a1
= (volatile int &)a
; // no warning
45 int &a2
= (int &)a
; // expected-warning {{cast from 'volatile int' to 'int &' drops volatile qualifier}}
46 volatile int &a3
= (int &)a
; // expected-warning {{cast from 'volatile int' to 'int &' drops volatile qualifier}}
47 int &a4
= (int &)((volatile int &)a
); // expected-warning {{cast from 'volatile int' to 'int &' drops volatile qualifier}}
48 int &a5
= (int &)((int &)a
); // expected-warning {{cast from 'volatile int' to 'int &' drops volatile qualifier}}
49 volatile int &a6
= (int &)((int &)a
); // expected-warning {{cast from 'volatile int' to 'int &' drops volatile qualifier}}
50 volatile int &a7
= (int &)((volatile int &)a
); // expected-warning {{cast from 'volatile int' to 'int &' drops volatile qualifier}}
51 volatile int &a8
= (volatile int &)((int &)a
); // expected-warning {{cast from 'volatile int' to 'int &' drops volatile qualifier}}
55 const volatile int a
= 0;
57 const volatile int &a0
= a
; // no warning
58 const volatile int &a1
= (const volatile int &)a
; // no warning
60 int &a2
= (int &)a
; // expected-warning {{cast from 'const volatile int' to 'int &' drops const and volatile qualifiers}}
61 const volatile int &a3
= (int &)a
; // expected-warning {{cast from 'const volatile int' to 'int &' drops const and volatile qualifiers}}
62 int &a4
= (int &)((const volatile int &)a
); // expected-warning {{cast from 'const volatile int' to 'int &' drops const and volatile qualifiers}}
63 int &a5
= (int &)((int &)a
); // expected-warning {{cast from 'const volatile int' to 'int &' drops const and volatile qualifiers}}
64 const volatile int &a6
= (int &)((int &)a
); // expected-warning {{cast from 'const volatile int' to 'int &' drops const and volatile qualifiers}}
65 const volatile int &a7
= (int &)((const volatile int &)a
); // expected-warning {{cast from 'const volatile int' to 'int &' drops const and volatile qualifiers}}
66 const volatile int &a8
= (const volatile int &)((int &)a
); // expected-warning {{cast from 'const volatile int' to 'int &' drops const and volatile qualifiers}}
73 int **a0
= (int **)((const int **)a
); // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
74 int **a1
= (int **)((int **)a
); // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
76 // const int **a2 = (int **)((int **)a);
77 // const int **a3 = (int **)((const int **)a);
79 const int **a4
= (const int **)((int **)a
); // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}} expected-warning {{cast from 'int **' to 'const int **' must have all intermediate pointers const qualified to be safe}}
80 const int **a5
= (const int **)((const int **)a
); // no warning
87 int *&a0
= (int *&)((const int *&)a
); // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
88 int *&a1
= (int *&)((int *&)a
); // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
90 // const int *&a2 = (int *&)((int *&)a);
91 // const int *&a3 = (int *&)((const int *&)a);
93 const int *&a4
= (const int *&)((int *&)a
); // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}} expected-warning {{cast from 'int *' to 'const int *&' must have all intermediate pointers const qualified to be safe}}
94 const int *&a5
= (const int *&)((const int *&)a
); // no warning
106 ((C
&)S
).B(); // expected-warning {{cast from 'const C' to 'C &' drops const qualifier}}
107 ((C
&)S
).A(); // expected-warning {{cast from 'const C' to 'C &' drops const qualifier}}
109 ((C
*)&S
)->B(); // expected-warning {{cast from 'const C *' to 'C *' drops const qualifier}}
110 ((C
*)&S
)->A(); // expected-warning {{cast from 'const C *' to 'C *' drops const qualifier}}
125 (int &)(S
.a
) = 0; // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
126 (int &)(S
.b
) = 0; // no warning
128 *(int *)(&S
.a
) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
129 *(int *)(&S
.b
) = 0; // no warning
134 (int &)(S
.a
) = 0; // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
135 (int &)(S
.b
) = 0; // expected-warning {{cast from 'const int' to 'int &' drops const qualifier}}
137 *(int *)(&S
.a
) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
138 *(int *)(&S
.b
) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}