2017-07-05 08:32:57 +02:00
|
|
|
/* -*- 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/.
|
|
|
|
*/
|
|
|
|
|
2023-08-23 22:09:07 +02:00
|
|
|
#include <cstddef>
|
2019-03-05 13:44:17 +02:00
|
|
|
#include <memory>
|
2017-11-20 10:26:01 +02:00
|
|
|
#include <string>
|
|
|
|
#include <rtl/ustring.hxx>
|
2018-12-04 11:20:03 +02:00
|
|
|
#include <o3tl/typed_flags_set.hxx>
|
2017-11-20 10:26:01 +02:00
|
|
|
|
2017-11-29 18:21:29 +01:00
|
|
|
#define MACRO (1)
|
|
|
|
|
2017-07-05 08:32:57 +02:00
|
|
|
bool foo(int);
|
|
|
|
|
2017-07-06 14:49:15 +02:00
|
|
|
enum class EFoo { Bar };
|
|
|
|
|
2017-11-23 08:13:06 +01:00
|
|
|
struct S { operator bool(); };
|
|
|
|
|
2018-12-04 11:20:03 +02:00
|
|
|
enum class BrowseMode
|
|
|
|
{
|
|
|
|
Modules = 0x01,
|
|
|
|
Top = 0x02,
|
|
|
|
Bottom = 0x04,
|
2020-03-28 18:15:28 +01:00
|
|
|
Left = 0x08,
|
2018-12-04 11:20:03 +02:00
|
|
|
};
|
|
|
|
namespace o3tl
|
|
|
|
{
|
|
|
|
template <> struct typed_flags<BrowseMode> : is_typed_flags<BrowseMode, 0xf>
|
|
|
|
{
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-23 22:09:07 +02:00
|
|
|
void f(char const *);
|
|
|
|
char const * operator ""_s1(char const *, std::size_t);
|
|
|
|
#if __cplusplus >= 202002L
|
|
|
|
struct Str { constexpr Str(char const *) {} };
|
|
|
|
template<Str> char const * operator ""_s2();
|
|
|
|
#endif
|
|
|
|
|
2017-07-05 08:32:57 +02:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int x = 1;
|
|
|
|
x = ((2)); // expected-error {{parentheses around parentheses [loplugin:unnecessaryparen]}}
|
|
|
|
|
2017-07-05 16:30:06 +02:00
|
|
|
if ((foo(1))) foo(2); // expected-error {{parentheses immediately inside if statement [loplugin:unnecessaryparen]}}
|
2017-07-07 08:42:54 +02:00
|
|
|
|
|
|
|
foo((1)); // expected-error {{parentheses immediately inside single-arg call [loplugin:unnecessaryparen]}}
|
2017-07-06 14:49:15 +02:00
|
|
|
|
2017-11-27 09:46:06 +01:00
|
|
|
int y = (x); // expected-error {{parentheses immediately inside vardecl statement [loplugin:unnecessaryparen]}}
|
2017-07-06 14:49:15 +02:00
|
|
|
(void)y;
|
|
|
|
|
2017-11-29 18:21:29 +01:00
|
|
|
EFoo efoo = EFoo::Bar;
|
|
|
|
switch (efoo) {
|
2017-09-05 08:29:58 +02:00
|
|
|
case (EFoo::Bar): break; // expected-error {{parentheses immediately inside case statement [loplugin:unnecessaryparen]}}
|
2017-07-06 14:49:15 +02:00
|
|
|
}
|
|
|
|
|
2017-11-22 14:12:34 +01:00
|
|
|
int z = (y) ? 1 : 0; // expected-error {{unnecessary parentheses around identifier [loplugin:unnecessaryparen]}}
|
2017-07-06 14:49:15 +02:00
|
|
|
(void)z;
|
2017-09-04 08:53:38 +02:00
|
|
|
|
|
|
|
int v1 = (static_cast<short>(1)) + 1; // expected-error {{unnecessary parentheses around cast [loplugin:unnecessaryparen]}}
|
|
|
|
(void)v1;
|
Enable -Wunreachable-code
...motivated by <https://gerrit.libreoffice.org/#/c/41565/2> adding dead code
at the end of a switch statement, after the last case's "break".
-Wunreachable-code appears to work well on Clang, while it appears to have no
effect on GCC.
Most of the affected places are apparently temporary/TODO/FIXME cases of
disabling code via "if (false)", which can be written with an extra set of
parentheses as "if ((false))" to silence -Wunreachable-code on Clang (which thus
needed loplugin:unnecessaryparen to be adapted accordingly). In some cases,
the controlling expression was more complex than just "false" and needed to be
rewritten by taking it out of the if statement to silence Clang.
One noteworthy case where the nature of the disabled code wasn't immediately
apparent:
Sep 12 16:59:58 <sberg> quikee, is that "if (false)" in
ScExponentialSmoothingDialog::ApplyOutput
(sc/source/ui/StatisticsDialogs/ExponentialSmoothingDialog.cxx) some work-in-
progress or dead code?
Sep 12 17:02:03 <quikee> sberg: WIP, but you can remove it
Sep 12 17:04:47 <sberg> quikee, I'll wrap the false in an extra set of
parentheses for now, to silence -Wunreachable-code (I wouldn't want to
remove it, as I have no idea whether I should then also remove the "Initial
value" comment preceding it)
Sep 12 17:07:29 <quikee> sberg: both are different ways to calculate the
"intital value"... so no
Another case where the nature of the dead code, following while (true) loops
without breaks, is unclear is sd/source/ui/remotecontrol/BluetoothServer.cxx,
where I added TODO markers to the workarounds that silence the warnings for now.
basic/source/sbx/sbxvalue.cxx had a variable of type double, of automatic
storage duration, and without an initalizer at the top of a switch statement.
Clang warning about it is arguably a false positive.
Apart from that, this didn't find any cases of genuinely dead code in the
existing code base.
Change-Id: Ib00b822c8efec94278c048783d5997b8ba86a94c
Reviewed-on: https://gerrit.libreoffice.org/42217
Tested-by: Stephan Bergmann <sbergman@redhat.com>
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2017-09-12 18:24:46 +02:00
|
|
|
|
|
|
|
// No warnings, used to silence -Wunreachable-code:
|
|
|
|
if ((false)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
x = (true) ? 0 : 1;
|
2017-11-20 10:26:01 +02:00
|
|
|
|
2017-11-29 18:21:29 +01:00
|
|
|
// More "no warnings", at least potentially used to silence -Wunreachable-code:
|
|
|
|
while ((false)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (; (false);) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
x = foo(0) && (false) ? 0 : 1;
|
|
|
|
x = MACRO < (0) ? 0 : 1;
|
|
|
|
// cf. odd Clang -Wunreachable-code--suppression mechanism when the macro itself contains
|
|
|
|
// parentheses, causing the issue that lead to c421ac3f9432f2e9468d28447dc4c2e45b6f4da3
|
|
|
|
// "Revert loplugin:unnecessaryparen warning around integer literals"
|
|
|
|
|
2017-11-20 10:26:01 +02:00
|
|
|
int v2 = (1); // expected-error {{parentheses immediately inside vardecl statement [loplugin:unnecessaryparen]}}
|
|
|
|
(void)v2;
|
|
|
|
|
|
|
|
std::string v3;
|
|
|
|
v3 = (std::string("xx") + "xx"); // expected-error {{parentheses immediately inside assignment [loplugin:unnecessaryparen]}}
|
|
|
|
(void)v3;
|
2017-11-23 08:13:06 +01:00
|
|
|
|
|
|
|
S s1;
|
|
|
|
if ((s1)) { // expected-error {{parentheses immediately inside if statement [loplugin:unnecessaryparen]}}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
S s2;
|
|
|
|
if ((s2 = s1)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-11-24 11:43:30 +01:00
|
|
|
|
|
|
|
(void) sizeof (int);
|
|
|
|
(void) sizeof (x); // expect no warning (for whatever reason; for symmetry with above case?)
|
2017-11-27 09:46:06 +01:00
|
|
|
|
|
|
|
// Expecting just one error, not reported twice during TraverseInitListExpr:
|
|
|
|
int a[] = {(x)}; // expected-error {{unnecessary parentheses around identifier [loplugin:unnecessaryparen]}}
|
|
|
|
(void) a;
|
2017-11-30 07:17:53 +01:00
|
|
|
|
|
|
|
(void) (+1); // expected-error {{unnecessary parentheses around signed numeric literal [loplugin:unnecessaryparen]}}
|
|
|
|
(void) (-1); // expected-error {{unnecessary parentheses around signed numeric literal [loplugin:unnecessaryparen]}}
|
|
|
|
|
|
|
|
// For simplicity's sake, even warn about pathological cases that would require adding
|
|
|
|
// whitespace when removing the parentheses (as is also necessary in other cases anyway, like
|
|
|
|
// "throw(x);"); it is unlikely that there are any actual occurrences of code like "-(-1)" that
|
|
|
|
// would benefit from the parentheses readability-wise, compared to "- -1":
|
|
|
|
(void) -(-1); // expected-error {{unnecessary parentheses around signed numeric literal [loplugin:unnecessaryparen]}}
|
2018-01-17 09:01:09 +02:00
|
|
|
|
|
|
|
char *p = nullptr;
|
|
|
|
delete (p); // expected-error {{parentheses immediately inside delete expr [loplugin:unnecessaryparen]}}
|
2017-07-05 08:32:57 +02:00
|
|
|
|
2018-12-04 11:20:03 +02:00
|
|
|
BrowseMode nBits = ( BrowseMode::Modules | BrowseMode::Top ); // expected-error {{parentheses immediately inside vardecl statement [loplugin:unnecessaryparen]}}
|
|
|
|
(void)nBits;
|
2021-05-03 09:29:37 +02:00
|
|
|
|
|
|
|
OUString::number((v2+1)); // expected-error {{parentheses immediately inside single-arg call [loplugin:unnecessaryparen]}}
|
2023-08-23 22:09:07 +02:00
|
|
|
|
|
|
|
(void) ("foo"); // expected-error {{unnecessary parentheses around single-token string literal [loplugin:unnecessaryparen]}}
|
|
|
|
(void) ("foo" "bar");
|
|
|
|
f(("foo")); // expected-error {{parentheses immediately inside single-arg call [loplugin:unnecessaryparen]}}
|
|
|
|
f(("foo" "bar"));
|
|
|
|
(void) ("foo"_s1); // expected-error {{unnecessary parentheses around single-token string literal [loplugin:unnecessaryparen]}}
|
|
|
|
(void) ("foo" "bar"_s1);
|
|
|
|
f(("foo"_s1)); // expected-error {{parentheses immediately inside single-arg call [loplugin:unnecessaryparen]}}
|
|
|
|
f(("foo" "bar"_s1));
|
|
|
|
#if __cplusplus >= 202002L
|
|
|
|
(void) ("foo"_s2); //TODO: expected error {{unnecessary parentheses around single-token string literal [loplugin:unnecessaryparen]}}
|
|
|
|
(void) ("foo" "bar"_s2);
|
|
|
|
f(("foo"_s2)); // TODO: expected error {{parentheses immediately inside single-arg call [loplugin:unnecessaryparen]}}
|
|
|
|
f(("foo" "bar"_s2));
|
|
|
|
#endif
|
2017-11-24 13:08:01 +02:00
|
|
|
};
|
|
|
|
|
2019-11-30 11:44:26 +01:00
|
|
|
struct B { operator bool() const; };
|
2019-11-29 17:42:03 +01:00
|
|
|
|
|
|
|
struct N { bool operator !(); };
|
|
|
|
|
2019-03-05 13:44:17 +02:00
|
|
|
class Foo2
|
|
|
|
{
|
|
|
|
int* p;
|
2019-11-29 17:42:03 +01:00
|
|
|
B b;
|
|
|
|
N n;
|
2019-03-05 13:44:17 +02:00
|
|
|
|
|
|
|
int foo2()
|
|
|
|
{
|
|
|
|
return (p) ? 1 : 0; // expected-error {{unnecessary parentheses around member expr [loplugin:unnecessaryparen]}}
|
|
|
|
}
|
2019-11-29 17:42:03 +01:00
|
|
|
|
|
|
|
static int foo3(Foo2 & foo) {
|
|
|
|
(void) !(foo.p);
|
|
|
|
(void) !(foo.b);
|
|
|
|
(void) !(foo.n);
|
|
|
|
return (foo.p) ? 1 : 0;
|
|
|
|
}
|
2019-03-05 13:44:17 +02:00
|
|
|
};
|
|
|
|
|
2017-07-05 08:32:57 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
|