Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / reducevarscope.cxx
blobca4ed09be5852ffe63065e6ef350f1ff4115626f
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 <rtl/ustring.hxx>
12 #pragma clang diagnostic ignored "-Wunknown-warning-option" // for Clang < 13
13 #pragma clang diagnostic ignored "-Wunused-but-set-variable"
15 void test1()
17 int i = 2; // expected-error {{can reduce scope of var [loplugin:reducevarscope]}}
19 i = 3; // expected-note {{used here [loplugin:reducevarscope]}}
21 int j = 2; // expected-error {{can reduce scope of var [loplugin:reducevarscope]}}
23 j = 3; // expected-note {{used here [loplugin:reducevarscope]}}
25 j = 4; // expected-note {{used here [loplugin:reducevarscope]}}
30 // negative test - seen inside a loop
31 void test2()
33 int i = 2;
34 for (int j = 1; j < 10; ++j)
36 i = 3;
40 // negative test - initial assignment from non-constant
41 void test3()
43 int j = 1;
44 int i = j;
46 i = 3;
50 // negative test
51 void test4()
53 int i = 2;
55 i = 3;
57 i = 4;
60 // negative test
61 void test5()
63 int i = 2;
64 i = 3;
67 // negative test - seen in 2 child blocks
68 void test6()
70 int i;
72 i = 3;
75 i = 3;
79 // TODO negative test - storing pointer to OUString data
80 // void test7()
81 // {
82 // OUString s;
83 // const sal_Unicode* p = nullptr;
84 // {
85 // p = s.getStr();
86 // }
87 // auto p2 = p;
88 // (void)p2;
89 // }
91 // negative test - passing var into lambda
92 void test8()
94 int i;
95 auto l1 = [&]() { i = 1; };
96 (void)l1;
99 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */