summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2021-04-20 21:07:42 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2021-04-21 13:15:32 +0200
commit7049328fb2d656d8454d4f704ad75d057e766c0b (patch)
tree0e8f2e1cce68c6a07c82d5e8496a64f716fecfde /compilerplugins
parenta003e4ff69263c7feb8e97e3291e5579fbd181ac (diff)
loplugin:stringadd replace OUStringLiteral temporaries with OUString::Concat
Change-Id: I656f06a74d9f0180ae460264563d6a935c7d2c60 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114377 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/stringadd.cxx15
-rw-r--r--compilerplugins/clang/test/stringadd.cxx9
2 files changed, 16 insertions, 8 deletions
diff --git a/compilerplugins/clang/stringadd.cxx b/compilerplugins/clang/stringadd.cxx
index b4994ab60ec6..5723b5bb6e3b 100644
--- a/compilerplugins/clang/stringadd.cxx
+++ b/compilerplugins/clang/stringadd.cxx
@@ -242,10 +242,15 @@ bool StringAdd::VisitCXXOperatorCallExpr(CXXOperatorCallExpr const* operatorCall
return;
auto tc3 = loplugin::TypeCheck(e->getType());
if (!tc3.Class("OUString").Namespace("rtl").GlobalNamespace()
- && !tc3.Class("OString").Namespace("rtl").GlobalNamespace())
+ && !tc3.Class("OString").Namespace("rtl").GlobalNamespace()
+ && !tc3.Class("OUStringLiteral").Namespace("rtl").GlobalNamespace()
+ && !tc3.Class("OStringLiteral").Namespace("rtl").GlobalNamespace()
+ && !tc3.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace()
+ && !tc3.Class("OStringBuffer").Namespace("rtl").GlobalNamespace())
return;
report(DiagnosticsEngine::Warning,
- ("avoid constructing %0 from %1 on %select{L|R}2HS of + (where %select{R|L}2HS is of"
+ ("rather use O[U]String::Concat than constructing %0 from %1 on %select{L|R}2HS of "
+ "+ (where %select{R|L}2HS is of"
" type %3)"),
compat::getBeginLoc(e))
<< e->getType().getLocalUnqualifiedType() << e->getSubExprAsWritten()->getType() << arg
@@ -348,7 +353,8 @@ bool StringAdd::isSideEffectFree(Expr const* expr)
return true;
// Expr::HasSideEffects does not like stuff that passes through OUStringLiteral
auto dc2 = loplugin::DeclCheck(constructExpr->getConstructor()->getParent());
- if (dc2.Class("OUStringLiteral").Namespace("rtl").GlobalNamespace())
+ if (dc2.Class("OUStringLiteral").Namespace("rtl").GlobalNamespace()
+ || dc2.Class("OStringLiteral").Namespace("rtl").GlobalNamespace())
return true;
}
@@ -356,7 +362,8 @@ bool StringAdd::isSideEffectFree(Expr const* expr)
if (auto functionalCastExpr = dyn_cast<CXXFunctionalCastExpr>(expr))
{
auto tc = loplugin::TypeCheck(functionalCastExpr->getType());
- if (tc.Class("OUStringLiteral").Namespace("rtl").GlobalNamespace())
+ if (tc.Class("OUStringLiteral").Namespace("rtl").GlobalNamespace()
+ || tc.Class("OStringLiteral").Namespace("rtl").GlobalNamespace())
return isSideEffectFree(functionalCastExpr->getSubExpr());
}
diff --git a/compilerplugins/clang/test/stringadd.cxx b/compilerplugins/clang/test/stringadd.cxx
index e17b207fcb64..a953e44062bb 100644
--- a/compilerplugins/clang/test/stringadd.cxx
+++ b/compilerplugins/clang/test/stringadd.cxx
@@ -36,6 +36,7 @@ void f1(OUString s1, int i, OString o)
s2 += OUString::number(i);
// expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
s2 += XXX1;
+ // expected-error@+2 {{rather use O[U]String::Concat than constructing 'rtl::OUStringLiteral<4>' from 'const char16_t [4]' on LHS of + (where RHS is of type 'const char [4]') [loplugin:stringadd]}}
// expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
s2 += OUStringLiteral(XXX1u) + XXX2;
@@ -200,9 +201,9 @@ void f1(OUString s, OUString t, int i, const char* pChar)
{
// no warning expected
t = t + "xxx";
- // expected-error@+1 {{avoid constructing 'rtl::OUString' from 'const char [4]' on RHS of + (where LHS is of type 'rtl::OUString') [loplugin:stringadd]}}
+ // expected-error@+1 {{rather use O[U]String::Concat than constructing 'rtl::OUString' from 'const char [4]' on RHS of + (where LHS is of type 'rtl::OUString') [loplugin:stringadd]}}
s = s + OUString("xxx");
- // expected-error@+1 {{avoid constructing 'rtl::OUString' from 'const rtl::OUString' on RHS of + (where LHS is of type 'rtl::OUString') [loplugin:stringadd]}}
+ // expected-error@+1 {{rather use O[U]String::Concat than constructing 'rtl::OUString' from 'const rtl::OUString' on RHS of + (where LHS is of type 'rtl::OUString') [loplugin:stringadd]}}
s = s + OUString(getByRef());
// no warning expected
@@ -220,9 +221,9 @@ void f1(OUString s, OUString t, int i, const char* pChar)
void f2(char ch)
{
OString s;
- // expected-error@+1 {{avoid constructing 'rtl::OString' from 'const char [4]' on RHS of + (where LHS is of type 'rtl::OString') [loplugin:stringadd]}}
+ // expected-error@+1 {{rather use O[U]String::Concat than constructing 'rtl::OString' from 'const char [4]' on RHS of + (where LHS is of type 'rtl::OString') [loplugin:stringadd]}}
s = s + OString("xxx");
- // expected-error@+1 {{avoid constructing 'rtl::OString' from 'char' on RHS of + (where LHS is of type 'rtl::OString') [loplugin:stringadd]}}
+ // expected-error@+1 {{rather use O[U]String::Concat than constructing 'rtl::OString' from 'char' on RHS of + (where LHS is of type 'rtl::OString') [loplugin:stringadd]}}
s = s + OString(ch);
}
}