Debian Codegraph @

gcc/testsuite/g++.dg/pr85523.C

raw
1 /* { dg-options "-fdiagnostics-show-caret" } */
2
3 /* Verify that we emit a "return *this;" fix-it hint for
4 a missing return in an assignment operator. */
5
6 struct s1 {
7 s1& operator=(const s1&) { } // { dg-warning "no return statement in function returning non-void" }
8 /* { dg-begin-multiline-output "" }
9 s1& operator=(const s1&) { }
10 ^
11 return *this;
12 { dg-end-multiline-output "" } */
13 };
14
15 /* Likewise for +=. */
16
17 struct s2 {
18 s2& operator+=(const s2&) {} // { dg-warning "no return statement in function returning non-void" }
19 /* { dg-begin-multiline-output "" }
20 s2& operator+=(const s2&) {}
21 ^
22 return *this;
23 { dg-end-multiline-output "" } */
24 };
25
26 /* No warning for "void" return. */
27
28 struct s3 {
29 void operator=(const s3&) { }
30 };
31
32 /* We shouldn't issue the fix-it hint if the return type isn't right. */
33
34 struct s4 {
35 int operator=(int) { } // { dg-warning "no return statement in function returning non-void" }
36 /* { dg-begin-multiline-output "" }
37 int operator=(int) { }
38 ^
39 { dg-end-multiline-output "" } */
40 };
41
42 /* Example of a multi-line fix-it hint. */
43
44 struct s5 {
45 int i;
46 s5& operator=(const s5& z) {
47 i = z.i;
48 } // { dg-warning "no return statement in function returning non-void" }
49 /* { dg-begin-multiline-output "" }
50 i = z.i;
51 + return *this;
52 }
53 ^
54 { dg-end-multiline-output "" } */
55 };
56
57 /* Example of a multi-line fix-it hint with other statements. */
58
59 extern void log (const char *);
60 struct s6 {
61 int i;
62 s6& operator=(const s6& z) {
63 log ("operator=\n");
64 i = z.i;
65 } // { dg-warning "no return statement in function returning non-void" }
66 /* { dg-begin-multiline-output "" }
67 i = z.i;
68 + return *this;
69 }
70 ^
71 { dg-end-multiline-output "" } */
72 };
73
74 /* Another example of a multi-line fix-it hint with other statements. */
75
76 struct s7 {
77 int i;
78 s7& operator=(const s6& z) {
79 if (z.i)
80 log ("operator=\n");
81 else
82 log ("operator=\n");
83 i = z.i;
84 } // { dg-warning "no return statement in function returning non-void" }
85 /* { dg-begin-multiline-output "" }
86 i = z.i;
87 + return *this;
88 }
89 ^
90 { dg-end-multiline-output "" } */
91 };