diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2022-02-07 23:21:27 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2022-02-08 09:09:32 +0100 |
commit | b01e0483ee14826057c42dfa58c3d6200a474791 (patch) | |
tree | df3602677ecf67edb532216fd767e63a347d6ac0 /compilerplugins | |
parent | b314b5a0612c9dc7279039dae44a250d4d3ec6b1 (diff) |
Extend loplugin:stringview to O[U]StringBuffer::toString
Change-Id: I7ad212dfff8b34d05e3b45107a1ef033a4efc454
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129651
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/stringview.cxx | 24 | ||||
-rw-r--r-- | compilerplugins/clang/test/stringview.cxx | 14 |
2 files changed, 31 insertions, 7 deletions
diff --git a/compilerplugins/clang/stringview.cxx b/compilerplugins/clang/stringview.cxx index b3a0323d2923..4c31c348b357 100644 --- a/compilerplugins/clang/stringview.cxx +++ b/compilerplugins/clang/stringview.cxx @@ -268,19 +268,29 @@ void StringView::handleCXXConstructExpr(CXXConstructExpr const* expr) void StringView::handleCXXMemberCallExpr(CXXMemberCallExpr const* expr) { - auto const dc = loplugin::DeclCheck(expr->getMethodDecl()).Function("copy"); - if (!dc) + auto const dc1 = loplugin::DeclCheck(expr->getMethodDecl()); + if (auto const dc2 = dc1.Function("copy")) { + if (dc2.Class("OString").Namespace("rtl").GlobalNamespace() + || dc2.Class("OUString").Namespace("rtl").GlobalNamespace()) + { + report(DiagnosticsEngine::Warning, "rather than copy, pass with a view using subView()", + expr->getExprLoc()) + << expr->getSourceRange(); + } return; } - if (!(dc.Class("OString").Namespace("rtl").GlobalNamespace() - || dc.Class("OUString").Namespace("rtl").GlobalNamespace())) + if (auto const dc2 = dc1.Function("toString")) { + if (dc2.Class("OStringBuffer").Namespace("rtl").GlobalNamespace() + || dc2.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace()) + { + report(DiagnosticsEngine::Warning, "rather than call toString, pass with a view", + expr->getExprLoc()) + << expr->getSourceRange(); + } return; } - report(DiagnosticsEngine::Warning, "rather than copy, pass with a view using subView()", - expr->getExprLoc()) - << expr->getSourceRange(); } /** check for calls to O[U]StringBuffer::append that could be passed as a diff --git a/compilerplugins/clang/test/stringview.cxx b/compilerplugins/clang/test/stringview.cxx index c03fbb84cb32..16e294a6eeb7 100644 --- a/compilerplugins/clang/test/stringview.cxx +++ b/compilerplugins/clang/test/stringview.cxx @@ -59,6 +59,20 @@ void f1(OString s1) // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}} ConstructWithView(s1.copy(1)); } +void f1(OUStringBuffer s1) +{ + // expected-error@+1 {{rather than call toString, pass with a view [loplugin:stringview]}} + call_view(s1.toString()); + // expected-error@+1 {{rather than call toString, pass with a view [loplugin:stringview]}} + ConstructWithView(s1.toString()); +} +void f1(OStringBuffer s1) +{ + // expected-error@+1 {{rather than call toString, pass with a view [loplugin:stringview]}} + call_view(s1.toString()); + // expected-error@+1 {{rather than call toString, pass with a view [loplugin:stringview]}} + ConstructWithView(s1.toString()); +} } namespace test2 |