summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/test/casttovoid.cxx
blob: 3d8c22b49c7e4a1b550817148a52f8266179923d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#undef NDEBUG
#include <cassert>

#define CAST_N3 (void) n3
#define ASSERT_N4 assert(n4 == 0)
#define ASSERT(x) assert(x)
#define USE(x) x

int f1(int n1, int n2, int n3, int n4, int n5) {
    (void) n1; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    int const & r1 = n1; // expected-note {{first consumption is here [loplugin:casttovoid]}}
    (void) n2; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    int const & r2 = {n2}; // expected-note {{first consumption is here [loplugin:casttovoid]}}
    (void) n3; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    int const & r3{n3}; // expected-note {{first consumption is here [loplugin:casttovoid]}}
    (void) n4; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    int const & r4(n4); // expected-note {{first consumption is here [loplugin:casttovoid]}}
    (void) n5; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    int const & r5 = (n5); // expected-note {{first consumption is here [loplugin:casttovoid]}}
    return r1 + r2 + r3 + r4 + r5;
}

int const & f2(int const & n) {
    (void) n; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    return n; // expected-note {{first consumption is here [loplugin:casttovoid]}}
}

int const & f3(int const & n) {
    (void) n; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    return (n); // expected-note {{first consumption is here [loplugin:casttovoid]}}
}

int const & f4(int const & n) {
    (void) n; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    return {n}; // expected-note {{first consumption is here [loplugin:casttovoid]}}
}

int const & f5(int const & n) {
    (void) n; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    return {(n)}; // expected-note {{first consumption is here [loplugin:casttovoid]}}
}

struct S1 {
    S1(int n1, int n2):
        n1_(n1), // expected-note {{first consumption is here [loplugin:casttovoid]}}
        n2_{n2} // expected-note {{first consumption is here [loplugin:casttovoid]}}
    {
        (void) n1; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
        (void) n2; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    }
    int const n1_;
    int const n2_;
};

struct S2 { int n; };

int fS2_1(S2 s) {
    (void) s; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    return s.n; // expected-note {{first consumption is here [loplugin:casttovoid]}}
}

int const & fS2_2(S2 const & s) {
    (void) s; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    return s.n; // expected-note {{first consumption is here [loplugin:casttovoid]}}
}

// Don't trigger assert in CastToVoid::VisitReturnStmt:
int n = [] { return 0; }();

int f() {
    int n1 = n;
    int n2 = [](int const & n) -> int const & {
        (void) n; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
        return n; // expected-note {{first consumption is here [loplugin:casttovoid]}}
    }(n1);
    return n2;
}

int main() {
    int n1 = 0;
    (void) n1; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    (void const) n1; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    (void volatile) n1; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    (void const volatile) n1; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    (void) (n1); // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    (void) ((n1)); // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    (void(n1)); // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    static_cast<void>(n1); // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    int n2 = 0;
    assert(n2 == 0);
    (void) n2; // expected-error {{unnecessary cast to void [loplugin:casttovoid]}}
    int n3 = 0;
    CAST_N3;
    int n4 = 0;
    ASSERT_N4;
    (void) n4;
    int n5 = 0;
    assert(n5 == 0);
    (void) n5;
    int n6 = 0;
    ASSERT(n6 == 0);
    (void) n6;
    int n7 = 0;
    assert(USE(n7) == 0);
    (void) n7;
    int n8 = 0;
    ASSERT(USE(USE(n8 == 0)));
    (void) n8;
    int volatile n9 = 0;
    (void) n9;
    return n1 // expected-note 8 {{first consumption is here [loplugin:casttovoid]}}
        + n2 // expected-note {{first consumption is here [loplugin:casttovoid]}}
        + n3;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */