summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2017-11-30 07:17:53 +0100
committerStephan Bergmann <sbergman@redhat.com>2017-11-30 19:29:21 +0100
commitb15f79a8f8de27c9d186ae1fbd0c86f194aed0ac (patch)
tree3c4dc647e8ccf78c41f66e4993f3a0490c15a137 /compilerplugins
parent32efde5cef2b8516a9decd0bf7091d7def1da971 (diff)
loplugin:unnecessaryparen: signed numeric literals
Change-Id: I75c8224452ca9c3711a2ccaca9ecf549fa59cb64 Reviewed-on: https://gerrit.libreoffice.org/45549 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/test/unnecessaryparen.cxx9
-rw-r--r--compilerplugins/clang/unnecessaryparen.cxx13
2 files changed, 22 insertions, 0 deletions
diff --git a/compilerplugins/clang/test/unnecessaryparen.cxx b/compilerplugins/clang/test/unnecessaryparen.cxx
index 78e2096abf9e..b7117a126edb 100644
--- a/compilerplugins/clang/test/unnecessaryparen.cxx
+++ b/compilerplugins/clang/test/unnecessaryparen.cxx
@@ -82,6 +82,15 @@ int main()
// Expecting just one error, not reported twice during TraverseInitListExpr:
int a[] = {(x)}; // expected-error {{unnecessary parentheses around identifier [loplugin:unnecessaryparen]}}
(void) a;
+
+ (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]}}
};
struct S2 {
diff --git a/compilerplugins/clang/unnecessaryparen.cxx b/compilerplugins/clang/unnecessaryparen.cxx
index 692e23706cc9..db241024dba4 100644
--- a/compilerplugins/clang/unnecessaryparen.cxx
+++ b/compilerplugins/clang/unnecessaryparen.cxx
@@ -180,6 +180,19 @@ bool UnnecessaryParen::VisitParenExpr(const ParenExpr* parenExpr)
<< parenExpr->getSourceRange();
handled_.insert(parenExpr);
}
+ } else if (auto const e = dyn_cast<UnaryOperator>(subExpr)) {
+ auto const op = e->getOpcode();
+ if (op == UO_Plus || op == UO_Minus) {
+ auto const e2 = e->getSubExpr();
+ if (isa<IntegerLiteral>(e2) || isa<FloatingLiteral>(e2) || isa<ImaginaryLiteral>(e2)) {
+ report(
+ DiagnosticsEngine::Warning,
+ "unnecessary parentheses around signed numeric literal",
+ parenExpr->getLocStart())
+ << parenExpr->getSourceRange();
+ handled_.insert(parenExpr);
+ }
+ }
} else if (isa<CXXNamedCastExpr>(subExpr)) {
report(
DiagnosticsEngine::Warning, "unnecessary parentheses around cast",