diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2023-06-02 08:56:39 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2023-08-08 15:31:57 +0200 |
commit | dddbb8a219f13344213b0ef6183529922bca7d50 (patch) | |
tree | 9d50c2053f6b4fc69637f090bb5374093196885c /compilerplugins | |
parent | a9b19f78f3cdcbf5c949a85b45877e903114cc54 (diff) |
loplugin:unnecessarygetstr fix false +
encountering when we need to pass a std::string to an OString
Change-Id: I91d2aa1f20e7c2407c708c9ce4ae82fb52934c85
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/152526
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/test/unnecessarygetstr.cxx | 8 | ||||
-rw-r--r-- | compilerplugins/clang/unnecessarygetstr.cxx | 20 |
2 files changed, 21 insertions, 7 deletions
diff --git a/compilerplugins/clang/test/unnecessarygetstr.cxx b/compilerplugins/clang/test/unnecessarygetstr.cxx index bb5fcc20d2ef..c0960557a89b 100644 --- a/compilerplugins/clang/test/unnecessarygetstr.cxx +++ b/compilerplugins/clang/test/unnecessarygetstr.cxx @@ -11,6 +11,7 @@ #include <ostream> #include <string_view> +#include <string> #include <rtl/strbuf.hxx> #include <rtl/string.hxx> @@ -120,4 +121,11 @@ void test(std::string v, OString o) } } +// no warning expected +namespace test6 +{ +void foo(const OString&); +void test(std::string v) { foo(v.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 681070356108..c80877a78554 100644 --- a/compilerplugins/clang/unnecessarygetstr.cxx +++ b/compilerplugins/clang/unnecessarygetstr.cxx @@ -49,7 +49,8 @@ public: .Namespace("rtl") .GlobalNamespace()) { - checkForGetStr(callExpr->getArg(0), "OUString::createFromAscii"); + checkForGetStr(callExpr->getArg(0), "OUString::createFromAscii", + /*isOStringConstructor*/ false); } return true; } @@ -62,22 +63,26 @@ public: if (tc.ClassOrStruct("basic_string").StdNamespace()) { if (constructExpr->getNumArgs() == 1 || constructExpr->getNumArgs() == 2) - checkForGetStr(constructExpr->getArg(0), "string constructor"); + checkForGetStr(constructExpr->getArg(0), "string constructor", + /*isOStringConstructor*/ false); } else if (tc.ClassOrStruct("basic_string_view").StdNamespace()) { if (constructExpr->getNumArgs() == 1) - checkForGetStr(constructExpr->getArg(0), "string_view constructor"); + checkForGetStr(constructExpr->getArg(0), "string_view constructor", + /*isOStringConstructor*/ false); } else if (tc.Class("OString").Namespace("rtl").GlobalNamespace()) { if (constructExpr->getNumArgs() == 1 || constructExpr->getNumArgs() == 2) - checkForGetStr(constructExpr->getArg(0), "OString constructor"); + checkForGetStr(constructExpr->getArg(0), "OString constructor", + /*isOStringConstructor*/ true); } else if (tc.Class("OUString").Namespace("rtl").GlobalNamespace()) { if (constructExpr->getNumArgs() == 2) - checkForGetStr(constructExpr->getArg(0), "OUString constructor"); + checkForGetStr(constructExpr->getArg(0), "OUString constructor", + /*isOStringConstructor*/ false); } return true; } @@ -94,7 +99,7 @@ public: } private: - void checkForGetStr(const Expr* arg, const char* msg) + void checkForGetStr(const Expr* arg, const char* msg, bool isOStringConstructor) { auto e = dyn_cast<CXXMemberCallExpr>(arg->IgnoreImplicit()); if (!e) @@ -117,7 +122,8 @@ private: << msg << e->getSourceRange(); } } - else if (tc2.Class("basic_string").StdNamespace()) + // we do need to use c_str() when passing to an OString + else if (!isOStringConstructor && tc2.Class("basic_string").StdNamespace()) { if (loplugin::DeclCheck(e->getMethodDecl()).Function("c_str")) report(DiagnosticsEngine::Warning, "unnecessary call to 'c_str' when passing to %0", |