1 // { dg-options "-Wold-style-cast -fdiagnostics-show-caret" }
2
3 struct foo {};
4 struct bar { const foo *field; };
5
test_1(void * ptr)6 void test_1 (void *ptr)
7 {
8 foo *f = (foo *)ptr; // { dg-warning "old-style cast" }
9 /* { dg-begin-multiline-output "" }
10 foo *f = (foo *)ptr;
11 ^~~
12 ----------
13 static_cast<foo *> (ptr)
14 { dg-end-multiline-output "" } */
15 }
16
test_2(const foo * ptr)17 void test_2 (const foo *ptr)
18 {
19 foo *f = (foo *)ptr; // { dg-warning "old-style cast" }
20 /* { dg-begin-multiline-output "" }
21 foo *f = (foo *)ptr;
22 ^~~
23 ----------
24 const_cast<foo *> (ptr)
25 { dg-end-multiline-output "" } */
26 }
27
test_3(bar * ptr)28 void test_3 (bar *ptr)
29 {
30 foo *f = (foo *)ptr; // { dg-warning "old-style cast" }
31 /* { dg-begin-multiline-output "" }
32 foo *f = (foo *)ptr;
33 ^~~
34 ----------
35 reinterpret_cast<foo *> (ptr)
36 { dg-end-multiline-output "" } */
37 }
38
test_4(bar * ptr)39 void test_4 (bar *ptr)
40 {
41 foo *f = (foo *)ptr->field; // { dg-warning "old-style cast" }
42 /* { dg-begin-multiline-output "" }
43 foo *f = (foo *)ptr->field;
44 ^~~~~
45 -----------------
46 const_cast<foo *> (ptr->field)
47 { dg-end-multiline-output "" } */
48 }
49
test_5()50 void test_5 ()
51 {
52 bar b_inst;
53 foo *f = (foo *)&b_inst; // { dg-warning "old-style cast" }
54 /* { dg-begin-multiline-output "" }
55 foo *f = (foo *)&b_inst;
56 ^~~~~~
57 --------------
58 reinterpret_cast<foo *> (&b_inst)
59 { dg-end-multiline-output "" } */
60 }
61
62 /* We don't offer suggestions for templates. */
63
64 template <typename T>
test_6(void * ptr)65 void test_6 (void *ptr)
66 {
67 foo *f = (foo *)ptr; // { dg-warning "old-style cast" }
68 /* { dg-begin-multiline-output "" }
69 foo *f = (foo *)ptr;
70 ^~~
71 { dg-end-multiline-output "" } */
72 }
73
74 /* We don't offer suggestions where a single C++-style cast can't be
75 used. */
76
test_7(const void * ptr)77 void test_7 (const void *ptr)
78 {
79 foo *f = (foo *)ptr; // { dg-warning "old-style cast" }
80 /* { dg-begin-multiline-output "" }
81 foo *f = (foo *)ptr;
82 ^~~
83 { dg-end-multiline-output "" } */
84 }
85
86 /* Likewise, no single C++-style cast is usable here. */
87
test_8(const bar & b_inst)88 void test_8 (const bar &b_inst)
89 {
90 foo *f = (foo *)&b_inst; // { dg-warning "old-style cast" }
91 /* { dg-begin-multiline-output "" }
92 foo *f = (foo *)&b_inst;
93 ^~~~~~
94 { dg-end-multiline-output "" } */
95 }
96