comphelper/source/misc/lok.cxx                   |    2 -
 comphelper/source/misc/sequenceashashmap.cxx     |    8 ++---
 compilerplugins/clang/test/unnecessarygetstr.cxx |   25 +++++++++++++++++
 compilerplugins/clang/unnecessarygetstr.cxx      |   32 ++++++++++++++---------
 lingucomponent/source/numbertext/numbertext.cxx  |    2 -
 sc/source/filter/orcus/xmlcontext.cxx            |    2 -
 sfx2/source/doc/autoredactdialog.cxx             |   10 +++----
 sfx2/source/doc/sfxbasemodel.cxx                 |    2 -
 svl/source/undo/undo.cxx                         |    4 +-
 svx/source/xoutdev/xattr.cxx                     |    2 -
 vcl/jsdialog/executor.cxx                        |    4 +-
 vcl/unx/generic/printer/cpdmgr.cxx               |    6 ++--
 12 files changed, 66 insertions(+), 33 deletions(-)

New commits:
commit f712c531336b2c44636a35ad682913550640e0d3
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Fri Apr 21 10:12:53 2023 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Fri Apr 21 21:46:09 2023 +0200

    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.gran...@collabora.co.uk>

diff --git a/comphelper/source/misc/lok.cxx b/comphelper/source/misc/lok.cxx
index 405411db7ba8..b5cbcc74cb54 100644
--- a/comphelper/source/misc/lok.cxx
+++ b/comphelper/source/misc/lok.cxx
@@ -232,7 +232,7 @@ bool isAllowlistedLanguage(const OUString& lang)
                     continue;
 
                 std::cerr << s << " ";
-                aList.emplace_back(OStringToOUString(s.c_str(), 
RTL_TEXTENCODING_UTF8));
+                aList.emplace_back(OStringToOUString(s, 
RTL_TEXTENCODING_UTF8));
             }
             std::cerr << std::endl;
         }
diff --git a/comphelper/source/misc/sequenceashashmap.cxx 
b/comphelper/source/misc/sequenceashashmap.cxx
index 3bbce289ffe0..50a4e0216cd0 100644
--- a/comphelper/source/misc/sequenceashashmap.cxx
+++ b/comphelper/source/misc/sequenceashashmap.cxx
@@ -49,7 +49,7 @@ uno::Any jsonToUnoAny(const boost::property_tree::ptree& 
aTree)
     uno::Sequence<uno::Reference<reflection::XIdlField>> aFields;
     uno::Reference<reflection::XIdlClass> xIdlClass
         = 
css::reflection::theCoreReflection::get(comphelper::getProcessComponentContext())
-              ->forName(OUString::fromUtf8(rType.c_str()));
+              ->forName(OUString::fromUtf8(rType));
     if (xIdlClass.is())
     {
         uno::TypeClass aTypeClass = xIdlClass->getTypeClass();
@@ -91,7 +91,7 @@ uno::Any jsonToUnoAny(const boost::property_tree::ptree& 
aTree)
             else if (aTypeClass == uno::TypeClass_DOUBLE)
                 aAny <<= o3tl::toDouble(rValue);
             else if (aTypeClass == uno::TypeClass_STRING)
-                aAny <<= OUString::fromUtf8(rValue.c_str());
+                aAny <<= OUString::fromUtf8(rValue);
         }
     }
     return aAny;
@@ -321,9 +321,9 @@ std::vector<css::beans::PropertyValue> 
JsonToPropertyValues(const OString& rJson
         const std::string& rValue = rPair.second.get<std::string>("value", "");
 
         beans::PropertyValue aValue;
-        aValue.Name = OUString::fromUtf8(rPair.first.c_str());
+        aValue.Name = OUString::fromUtf8(rPair.first);
         if (rType == "string")
-            aValue.Value <<= OUString::fromUtf8(rValue.c_str());
+            aValue.Value <<= OUString::fromUtf8(rValue);
         else if (rType == "boolean")
             aValue.Value <<= OString(rValue.c_str()).toBoolean();
         else if (rType == "float")
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;
diff --git a/lingucomponent/source/numbertext/numbertext.cxx 
b/lingucomponent/source/numbertext/numbertext.cxx
index 79c8e6810041..4f868adb74d8 100644
--- a/lingucomponent/source/numbertext/numbertext.cxx
+++ b/lingucomponent/source/numbertext/numbertext.cxx
@@ -130,7 +130,7 @@ OUString SAL_CALL NumberText_Impl::getNumberText(const 
OUString& rText, const Lo
 #if defined(_WIN32)
     OUString aResult(o3tl::toU(sResult.c_str()));
 #else
-    OUString aResult = 
OUString::fromUtf8(Numbertext::wstring2string(sResult).c_str());
+    OUString aResult = OUString::fromUtf8(Numbertext::wstring2string(sResult));
 #endif
     return aResult;
 }
diff --git a/sc/source/filter/orcus/xmlcontext.cxx 
b/sc/source/filter/orcus/xmlcontext.cxx
index e9c844890978..1588fad3424f 100644
--- a/sc/source/filter/orcus/xmlcontext.cxx
+++ b/sc/source/filter/orcus/xmlcontext.cxx
@@ -227,7 +227,7 @@ void ScOrcusXMLContextImpl::importXML(const 
ScOrcusImportXMLParam& rParam)
             std::ostringstream os;
             os << "ns" << index;
             std::string alias = os.str();
-            filter.set_namespace_alias(alias.c_str(), nsid);
+            filter.set_namespace_alias(alias, nsid);
         }
 
         // Set cell links.
diff --git a/sfx2/source/doc/autoredactdialog.cxx 
b/sfx2/source/doc/autoredactdialog.cxx
index 5e61849c079a..f84af9622f95 100644
--- a/sfx2/source/doc/autoredactdialog.cxx
+++ b/sfx2/source/doc/autoredactdialog.cxx
@@ -367,14 +367,14 @@ boost::property_tree::ptree redactionTargetToJSON(const 
RedactionTarget* pTarget
 std::unique_ptr<RedactionTarget>
 JSONtoRedactionTarget(const boost::property_tree::ptree::value_type& rValue)
 {
-    OUString sName = 
OUString::fromUtf8(rValue.second.get<std::string>("sName").c_str());
+    OUString sName = 
OUString::fromUtf8(rValue.second.get<std::string>("sName"));
     RedactionTargetType eType
         = 
static_cast<RedactionTargetType>(atoi(rValue.second.get<std::string>("eType").c_str()));
-    OUString sContent = 
OUString::fromUtf8(rValue.second.get<std::string>("sContent").c_str());
+    OUString sContent = 
OUString::fromUtf8(rValue.second.get<std::string>("sContent"));
     bool bCaseSensitive
-        = 
OUString::fromUtf8(rValue.second.get<std::string>("bCaseSensitive").c_str()).toBoolean();
+        = 
OUString::fromUtf8(rValue.second.get<std::string>("bCaseSensitive")).toBoolean();
     bool bWholeWords
-        = 
OUString::fromUtf8(rValue.second.get<std::string>("bWholeWords").c_str()).toBoolean();
+        = 
OUString::fromUtf8(rValue.second.get<std::string>("bWholeWords")).toBoolean();
     sal_uInt32 nID = atoi(rValue.second.get<std::string>("nID").c_str());
 
     return std::unique_ptr<RedactionTarget>(
@@ -607,7 +607,7 @@ SfxAutoRedactDialog::~SfxAutoRedactDialog()
 
         boost::property_tree::write_json(aStream, aTargetsTree, false);
 
-        OUString sUserDataStr(OUString::fromUtf8(aStream.str().c_str()));
+        OUString sUserDataStr(OUString::fromUtf8(aStream.str()));
 
         // Store the dialog data
         SvtViewOptions aDlgOpt(EViewType::Dialog, m_xDialog->get_help_id());
diff --git a/sfx2/source/doc/sfxbasemodel.cxx b/sfx2/source/doc/sfxbasemodel.cxx
index 73f0a3137d7e..8ff60d41ff90 100644
--- a/sfx2/source/doc/sfxbasemodel.cxx
+++ b/sfx2/source/doc/sfxbasemodel.cxx
@@ -3217,7 +3217,7 @@ void SfxBaseModel::impl_store(  const   OUString&         
          sURL
         std::stringstream aErrCode;
         aErrCode << nErrCode;
         throw task::ErrorCodeIOException(
-            "SfxBaseModel::impl_store <" + sURL + "> failed: " + 
OUString::fromUtf8(aErrCode.str().c_str()),
+            "SfxBaseModel::impl_store <" + sURL + "> failed: " + 
OUString::fromUtf8(aErrCode.str()),
             Reference< XInterface >(), sal_uInt32(nErrCode));
     }
 }
diff --git a/svl/source/undo/undo.cxx b/svl/source/undo/undo.cxx
index 1ddb3b35f274..31269438f938 100644
--- a/svl/source/undo/undo.cxx
+++ b/svl/source/undo/undo.cxx
@@ -1212,7 +1212,7 @@ OUString SfxUndoManager::GetUndoActionsInfo() const
     aTree.add_child("actions", aActions);
     std::stringstream aStream;
     boost::property_tree::write_json(aStream, aTree);
-    return OUString::fromUtf8(aStream.str().c_str());
+    return OUString::fromUtf8(aStream.str());
 }
 
 OUString SfxUndoManager::GetRedoActionsInfo() const
@@ -1231,7 +1231,7 @@ OUString SfxUndoManager::GetRedoActionsInfo() const
     aTree.add_child("actions", aActions);
     std::stringstream aStream;
     boost::property_tree::write_json(aStream, aTree);
-    return OUString::fromUtf8(aStream.str().c_str());
+    return OUString::fromUtf8(aStream.str());
 }
 
 bool SfxUndoManager::IsEmptyActions() const
diff --git a/svx/source/xoutdev/xattr.cxx b/svx/source/xoutdev/xattr.cxx
index 64df04216530..8df15b725634 100644
--- a/svx/source/xoutdev/xattr.cxx
+++ b/svx/source/xoutdev/xattr.cxx
@@ -2151,7 +2151,7 @@ namespace
 
             for (const auto& rPair : aTree)
             {
-                aArgs[OUString::fromUtf8(rPair.first.c_str())] = 
OUString::fromUtf8(rPair.second.get_value<std::string>(".").c_str());
+                aArgs[OUString::fromUtf8(rPair.first)] = 
OUString::fromUtf8(rPair.second.get_value<std::string>("."));
             }
         }
         return aArgs;
diff --git a/vcl/jsdialog/executor.cxx b/vcl/jsdialog/executor.cxx
index eb11af9b2884..f387a64ffc37 100644
--- a/vcl/jsdialog/executor.cxx
+++ b/vcl/jsdialog/executor.cxx
@@ -28,8 +28,8 @@ StringMap jsonToStringMap(const char* pJSON)
 
         for (const auto& rPair : aTree)
         {
-            aArgs[OUString::fromUtf8(rPair.first.c_str())]
-                = 
OUString::fromUtf8(rPair.second.get_value<std::string>(".").c_str());
+            aArgs[OUString::fromUtf8(rPair.first)]
+                = OUString::fromUtf8(rPair.second.get_value<std::string>("."));
         }
     }
     return aArgs;
diff --git a/vcl/unx/generic/printer/cpdmgr.cxx 
b/vcl/unx/generic/printer/cpdmgr.cxx
index e8b22111d584..125c3680b4c0 100644
--- a/vcl/unx/generic/printer/cpdmgr.cxx
+++ b/vcl/unx/generic/printer/cpdmgr.cxx
@@ -136,8 +136,8 @@ void CPDManager::printerAdded (GDBusConnection *connection,
     std::stringstream uniqueName;
     uniqueName << pDest->id << ", " << pDest->backend_name;
     rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
-    OUString aPrinterName = OStringToOUString( printerName.str().c_str(), 
aEncoding );
-    OUString aUniqueName = OStringToOUString( uniqueName.str().c_str(), 
aEncoding );
+    OUString aPrinterName = OStringToOUString( printerName.str(), aEncoding );
+    OUString aUniqueName = OStringToOUString( uniqueName.str(), aEncoding );
     current->addNewPrinter(aPrinterName, aUniqueName, pDest);
 }
 
@@ -157,7 +157,7 @@ void CPDManager::printerRemoved (GDBusConnection *,
     std::stringstream uniqueName;
     uniqueName << id << ", " << backend_name;
     rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
-    OUString aUniqueName = OStringToOUString( uniqueName.str().c_str(), 
aEncoding );
+    OUString aUniqueName = OStringToOUString( uniqueName.str(), aEncoding );
     std::unordered_map<OUString, CPDPrinter *>::iterator it = 
pManager->m_aCPDDestMap.find( aUniqueName );
     if (it == pManager->m_aCPDDestMap.end()) {
         SAL_WARN("vcl.unx.print", "CPD trying to remove non-existent printer 
from list");

Reply via email to