Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / consttobool.cxx
blob4fe41a8140f25c9741475be3e89604e4c05ea430
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 #include <sal/config.h>
12 #include <cassert>
14 #include <sal/types.h>
16 #pragma clang diagnostic ignored "-Wnull-conversion"
18 enum E
20 E0,
21 E1,
25 int const c1 = 1;
26 constexpr int c2 = 2;
28 struct S
30 S()
31 // expected-error-re@+1 {{implicit conversion of constant {{nullptr|0}} of type '{{(std::)?}}nullptr_t' to 'bool'; use 'false' instead [loplugin:consttobool]}}
32 : b(nullptr)
35 bool b;
38 int main()
40 bool b;
41 // expected-error@+1 {{implicit conversion of constant 0 of type 'int' to 'bool'; use 'false' instead [loplugin:consttobool]}}
42 b = 0;
43 // expected-error@+1 {{implicit conversion of constant 1 of type 'sal_Bool' (aka 'unsigned char') to 'bool'; use 'true' instead [loplugin:consttobool]}}
44 b = sal_True;
45 // expected-error@+1 {{implicit conversion of constant 1.000000e+00 of type 'double' to 'bool'; use 'true' instead [loplugin:consttobool]}}
46 b = 1.0;
47 // expected-error@+1 {{implicit conversion of constant 2 of type 'E' to 'bool'; use 'true' instead [loplugin:consttobool]}}
48 b = E2;
49 // expected-error@+1 {{implicit conversion of constant 97 of type 'char' to 'bool'; use 'true' instead [loplugin:consttobool]}}
50 b = 'a';
51 // expected-error@+1 {{implicit conversion of constant 1 of type 'int' to 'bool'; use 'true' instead [loplugin:consttobool]}}
52 b = c1;
53 // expected-error@+1 {{implicit conversion of constant 2 of type 'int' to 'bool'; use 'true' instead [loplugin:consttobool]}}
54 b = c2;
55 // expected-error@+1 {{implicit conversion of constant 3 of type 'int' to 'bool'; use 'true' instead [loplugin:consttobool]}}
56 b = (c1 | c2);
58 #if !defined NDEBUG
59 assert(b); // no warnings from within assert macro itself
60 assert(b && "msg"); // no warnings for `&& "msg"`
61 if (b)
63 assert(!"msg"); // no warnings for `!"msg"`
65 // expected-error@+1 {{implicit conversion of constant &"msg"[0] of type 'const char *' to 'bool'; use 'true' instead [loplugin:consttobool]}}
66 assert("msg");
67 #endif
70 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */