diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2023-04-21 10:12:53 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2023-04-21 21:46:09 +0200 |
commit | f712c531336b2c44636a35ad682913550640e0d3 (patch) | |
tree | 17f96584923f9de4945479eb67d65fa7b2a2331e /compilerplugins/clang | |
parent | 90cbe03a0bd65151c197d3d4aeaf48a696f13fad (diff) |
loplugin:unnecessarygetstr extend to checking std::string::c_str
Change-Id: I17398e2a6a31a2c98ba8e54b5c8045f22aee8759
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150749
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins/clang')
-rw-r--r-- | compilerplugins/clang/test/unnecessarygetstr.cxx | 25 | ||||
-rw-r--r-- | compilerplugins/clang/unnecessarygetstr.cxx | 32 |
2 files changed, 45 insertions, 12 deletions
diff --git a/compilerplugins/clang/test/unnecessarygetstr.cxx b/compilerplugins/clang/test/unnecessarygetstr.cxx index d04d2ca28402..12905ec5d233 100644 --- a/compilerplugins/clang/test/unnecessarygetstr.cxx +++ b/compilerplugins/clang/test/unnecessarygetstr.cxx @@ -18,6 +18,8 @@ #include <rtl/ustring.hxx> #include <sal/log.hxx> +namespace test1 +{ void f1(bool, const OString& s); struct Foo { @@ -41,7 +43,10 @@ void test1(Foo& foo) = "xx" + OUString(aVal.getStr(), aVal.getLength(), RTL_TEXTENCODING_ASCII_US); (void)aCompText; } +} +namespace test2 +{ // call to param that takes string_view void f2(bool, std::string_view); struct Foo2 @@ -60,4 +65,24 @@ void test2(Foo2& foo) // expected-error@+1 {{unnecessary call to 'getStr' when passing to string_view arg [loplugin:unnecessarygetstr]}} foo.f2(true, OString::number(12).getStr()); } +} + +namespace test3 +{ +// call to param that takes string_view +void f2(bool, std::string_view); +struct Foo2 +{ + void f2(bool, std::string_view); +}; +void test3(Foo2& foo) +{ + std::string s; + // expected-error@+1 {{unnecessary call to 'c_str' when passing to string_view arg [loplugin:unnecessarygetstr]}} + f2(true, s.c_str()); + // expected-error@+1 {{unnecessary call to 'c_str' when passing to string_view arg [loplugin:unnecessarygetstr]}} + foo.f2(true, s.c_str()); +} +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/compilerplugins/clang/unnecessarygetstr.cxx b/compilerplugins/clang/unnecessarygetstr.cxx index c72c9ae7598d..66f2fa2851fe 100644 --- a/compilerplugins/clang/unnecessarygetstr.cxx +++ b/compilerplugins/clang/unnecessarygetstr.cxx @@ -87,18 +87,26 @@ public: continue; auto const t = e->getObjectType(); auto const tc2 = loplugin::TypeCheck(t); - if (!(tc2.Class("OString").Namespace("rtl").GlobalNamespace() - || tc2.Class("OUString").Namespace("rtl").GlobalNamespace() - || tc2.Class("OStringBuffer").Namespace("rtl").GlobalNamespace() - || tc2.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace() - || tc2.ClassOrStruct("StringNumber").Namespace("rtl").GlobalNamespace())) - continue; - if (!loplugin::DeclCheck(e->getMethodDecl()).Function("getStr")) - continue; - report(DiagnosticsEngine::Warning, - "unnecessary call to 'getStr' when passing to string_view arg", - e->getExprLoc()) - << e->getSourceRange(); + if (tc2.Class("OString").Namespace("rtl").GlobalNamespace() + || tc2.Class("OUString").Namespace("rtl").GlobalNamespace() + || tc2.Class("OStringBuffer").Namespace("rtl").GlobalNamespace() + || tc2.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace() + || tc2.ClassOrStruct("StringNumber").Namespace("rtl").GlobalNamespace()) + { + if (loplugin::DeclCheck(e->getMethodDecl()).Function("getStr")) + report(DiagnosticsEngine::Warning, + "unnecessary call to 'getStr' when passing to string_view arg", + e->getExprLoc()) + << e->getSourceRange(); + } + else if (tc2.Class("basic_string").StdNamespace()) + { + if (loplugin::DeclCheck(e->getMethodDecl()).Function("c_str")) + report(DiagnosticsEngine::Warning, + "unnecessary call to 'c_str' when passing to string_view arg", + e->getExprLoc()) + << e->getSourceRange(); + } } } return true; |