Fix typo
[LibreOffice.git] / compilerplugins / clang / test / casttovoid.cxx
blob3d8c22b49c7e4a1b550817148a52f8266179923d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #undef NDEBUG
11 #include <cassert>
13 #define CAST_N3 (void) n3
14 #define ASSERT_N4 assert(n4 == 0)
15 #define ASSERT(x) assert(x)
16 #define USE(x) x
18 int f1(int n1, int n2, int n3, int n4, int n5) {
19 (void) n1; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
20 int const & r1 = n1; // expected-note {{first consumption is here [loplugin:casttovoid]}}
21 (void) n2; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
22 int const & r2 = {n2}; // expected-note {{first consumption is here [loplugin:casttovoid]}}
23 (void) n3; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
24 int const & r3{n3}; // expected-note {{first consumption is here [loplugin:casttovoid]}}
25 (void) n4; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
26 int const & r4(n4); // expected-note {{first consumption is here [loplugin:casttovoid]}}
27 (void) n5; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
28 int const & r5 = (n5); // expected-note {{first consumption is here [loplugin:casttovoid]}}
29 return r1 + r2 + r3 + r4 + r5;
32 int const & f2(int const & n) {
33 (void) n; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
34 return n; // expected-note {{first consumption is here [loplugin:casttovoid]}}
37 int const & f3(int const & n) {
38 (void) n; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
39 return (n); // expected-note {{first consumption is here [loplugin:casttovoid]}}
42 int const & f4(int const & n) {
43 (void) n; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
44 return {n}; // expected-note {{first consumption is here [loplugin:casttovoid]}}
47 int const & f5(int const & n) {
48 (void) n; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
49 return {(n)}; // expected-note {{first consumption is here [loplugin:casttovoid]}}
52 struct S1 {
53 S1(int n1, int n2):
54 n1_(n1), // expected-note {{first consumption is here [loplugin:casttovoid]}}
55 n2_{n2} // expected-note {{first consumption is here [loplugin:casttovoid]}}
57 (void) n1; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
58 (void) n2; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
60 int const n1_;
61 int const n2_;
64 struct S2 { int n; };
66 int fS2_1(S2 s) {
67 (void) s; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
68 return s.n; // expected-note {{first consumption is here [loplugin:casttovoid]}}
71 int const & fS2_2(S2 const & s) {
72 (void) s; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
73 return s.n; // expected-note {{first consumption is here [loplugin:casttovoid]}}
76 // Don't trigger assert in CastToVoid::VisitReturnStmt:
77 int n = [] { return 0; }();
79 int f() {
80 int n1 = n;
81 int n2 = [](int const & n) -> int const & {
82 (void) n; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
83 return n; // expected-note {{first consumption is here [loplugin:casttovoid]}}
84 }(n1);
85 return n2;
88 int main() {
89 int n1 = 0;
90 (void) n1; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
91 (void const) n1; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
92 (void volatile) n1; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
93 (void const volatile) n1; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
94 (void) (n1); // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
95 (void) ((n1)); // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
96 (void(n1)); // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
97 static_cast<void>(n1); // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
98 int n2 = 0;
99 assert(n2 == 0);
100 (void) n2; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
101 int n3 = 0;
102 CAST_N3;
103 int n4 = 0;
104 ASSERT_N4;
105 (void) n4;
106 int n5 = 0;
107 assert(n5 == 0);
108 (void) n5;
109 int n6 = 0;
110 ASSERT(n6 == 0);
111 (void) n6;
112 int n7 = 0;
113 assert(USE(n7) == 0);
114 (void) n7;
115 int n8 = 0;
116 ASSERT(USE(USE(n8 == 0)));
117 (void) n8;
118 int volatile n9 = 0;
119 (void) n9;
120 return n1 // expected-note 8 {{first consumption is here [loplugin:casttovoid]}}
121 + n2 // expected-note {{first consumption is here [loplugin:casttovoid]}}
122 + n3;
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */