diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2022-02-17 19:06:17 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2022-02-17 21:45:49 +0100 |
commit | ee373f34ae1509e8d9fffaf4b5140ee9c35e8d41 (patch) | |
tree | 4b6b22600308a75dcfb9683775c9f976fddcbd7d /compilerplugins | |
parent | d1a2b80b9dc146c7fe63d2657e5506f49d6e5c0d (diff) |
Extend loplugin:stringview to O[U]StringBuffer::makeStringAndClear
...at least when called on an rvalue. (The lvalue case would often be trickier
to act upon, if the cleared object is still used later on.)
Change-Id: I006e618da004b2127e9ed7381911c2d7b00b1169
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130110
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/stringview.cxx | 16 | ||||
-rw-r--r-- | compilerplugins/clang/test/stringview.cxx | 27 |
2 files changed, 43 insertions, 0 deletions
diff --git a/compilerplugins/clang/stringview.cxx b/compilerplugins/clang/stringview.cxx index 1f7783a3f3b3..94ba6f150f1b 100644 --- a/compilerplugins/clang/stringview.cxx +++ b/compilerplugins/clang/stringview.cxx @@ -282,6 +282,22 @@ void StringView::handleCXXMemberCallExpr(CXXMemberCallExpr const* expr) } return; } + if (auto const dc2 = dc1.Function("makeStringAndClear")) + { + if (dc2.Class("OStringBuffer").Namespace("rtl").GlobalNamespace() + || dc2.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace()) + { + auto const obj = expr->getImplicitObjectArgument(); + if (!(obj->isLValue() || obj->getType()->isPointerType())) + { + report(DiagnosticsEngine::Warning, + "rather than call makeStringAndClear on an rvalue, pass with a view", + expr->getExprLoc()) + << expr->getSourceRange(); + } + } + return; + } if (auto const dc2 = dc1.Function("toString")) { if (dc2.Class("OStringBuffer").Namespace("rtl").GlobalNamespace() diff --git a/compilerplugins/clang/test/stringview.cxx b/compilerplugins/clang/test/stringview.cxx index a679ec59ea45..7e637175e259 100644 --- a/compilerplugins/clang/test/stringview.cxx +++ b/compilerplugins/clang/test/stringview.cxx @@ -10,6 +10,7 @@ #include <sal/config.h> #include <string_view> +#include <utility> #include <rtl/strbuf.hxx> #include <rtl/string.hxx> @@ -81,6 +82,32 @@ void f1(OStringBuffer s1) // expected-error@+1 {{rather than call toString, pass with a view [loplugin:stringview]}} ConstructWithView(s1.toString()); } +void makeStringAndClear(OUStringBuffer s) +{ + call_view(s.makeStringAndClear()); + ConstructWithView(s.makeStringAndClear()); + call_view((&s)->makeStringAndClear()); + ConstructWithView((&s)->makeStringAndClear()); + // expected-error@+1 {{rather than call makeStringAndClear on an rvalue, pass with a view [loplugin:stringview]}} + call_view(std::move(s).makeStringAndClear()); + // expected-error@+1 {{rather than call makeStringAndClear on an rvalue, pass with a view [loplugin:stringview]}} + ConstructWithView(std::move(s).makeStringAndClear()); + // expected-error@+1 {{rather than call makeStringAndClear on an rvalue, pass with a view [loplugin:stringview]}} + call_view((s).copy(1).makeStringAndClear()); + // expected-error@+1 {{rather than call makeStringAndClear on an rvalue, pass with a view [loplugin:stringview]}} + ConstructWithView(s.copy(1).makeStringAndClear()); +} +void makeStringAndClear(OStringBuffer s) +{ + call_view(s.makeStringAndClear()); + ConstructWithView(s.makeStringAndClear()); + call_view((&s)->makeStringAndClear()); + ConstructWithView((&s)->makeStringAndClear()); + // expected-error@+1 {{rather than call makeStringAndClear on an rvalue, pass with a view [loplugin:stringview]}} + call_view(std::move(s).makeStringAndClear()); + // expected-error@+1 {{rather than call makeStringAndClear on an rvalue, pass with a view [loplugin:stringview]}} + ConstructWithView(std::move(s).makeStringAndClear()); +} } namespace test2 |