[pim/trojita] src: Make QRegularExpression objects static

Espen Sandøy Hustad <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 516880ad64023ed8b40f2f77dc287af62a98407e by Espen Sandøy Hustad.
Committed on 03/08/2026 at 18:56.
Pushed by ehustad into branch 'master'.

Make QRegularExpression objects static

Fix Clazy warning: Don't create temporary QRegularExpression
objects. Use a static QRegularExpression object instead
[-Wclazy-use-static-qregularexpression]

M  +2    -1    src/Gui/ComposeWidget.cpp
M  +2    -1    src/Gui/Window.cpp
M  +2    -2    src/Imap/Encoders.cpp
M  +2    -2    src/qwwsmtpclient/qwwsmtpclient.cpp

https://invent.kde.org/pim/trojita/-/commit/516880ad64023ed8b40f2f77dc287af62a98407e

diff --git a/src/Gui/ComposeWidget.cpp b/src/Gui/ComposeWidget.cpp
index f91756792..b94afa165 100644
--- a/src/Gui/ComposeWidget.cpp
+++ b/src/Gui/ComposeWidget.cpp
@@ -747,7 +747,8 @@ void ComposeWidget::closeEvent(QCloseEvent *ce)
                 // Some characters are best avoided in file names. This is probably not a definitive list, but the hope is that
                 // it's going to be more readable than an unformatted hash or similar stuff.  The list of characters was taken
                 // from http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words .
-                filename.replace(QRegularExpression(QLatin1String("[/\\\\:\"|<>*?]")), QStringLiteral("_"));
+                static const QRegularExpression re(QLatin1String("[/\\\\:\"|<>*?]"));
+                filename.replace(re, QStringLiteral("_"));
                 path = QFileDialog::getSaveFileName(this, tr("Save as"), path + QLatin1Char('/') + filename + QLatin1String(".draft"),
                                                     tr("Drafts") + QLatin1String(" (*.draft)"));
                 if (path.isEmpty()) { // cancelled save
diff --git a/src/Gui/Window.cpp b/src/Gui/Window.cpp
index 18c22c460..8f8c072fa 100644
--- a/src/Gui/Window.cpp
+++ b/src/Gui/Window.cpp
@@ -2196,8 +2196,9 @@ QStringList MainWindow::copyrightHolders(QFile *file) const
         const QString prefix(QStringLiteral("Copyright (C) "));
         if (line.startsWith(prefix)) {
             const int pos = prefix.size();
+            static const QRegularExpression re(QLatin1String("(\\d) - (\\d)"));
             ret << QChar(0xa9 /* COPYRIGHT SIGN */) + QLatin1Char(' ') +
-                        line.mid(pos).replace(QRegularExpression(QLatin1String("(\\d) - (\\d)")),
+                        line.mid(pos).replace(re,
                         QLatin1String("\\1") + QChar(0x2014 /* EM DASH */) + QLatin1String("\\2"));
         }
     }
diff --git a/src/Imap/Encoders.cpp b/src/Imap/Encoders.cpp
index fade1a3e7..916f4d8d1 100644
--- a/src/Imap/Encoders.cpp
+++ b/src/Imap/Encoders.cpp
@@ -278,7 +278,7 @@ namespace {
     /** @short Decode a header in the RFC 2047 format into a unicode string */
     static QString decodeWordSequence(const QByteArray& input)
     {
-        QRegularExpression whitespace(QLatin1String("^\\s+$"));
+        static const QRegularExpression whitespace(QLatin1String("^\\s+$"));
 
         // the regexp library operates on unicode strings, unfortunately
         QString str = QString::fromUtf8(input);
@@ -287,7 +287,7 @@ namespace {
 
         // This wasn't matching for QRegExp, but perhaps it does work for QRegularExpression
         //QRegularExpression encodedWord("\\b=\\?\\S+\\?\\S+\\?\\S*\\?=\\b");
-        QRegularExpression encodedWord(QLatin1String("\"?=\\?(\\S+)\\?(\\S+)\\?(.*)\\?=\"?"),
+        static const QRegularExpression encodedWord(QLatin1String("\"?=\\?(\\S+)\\?(\\S+)\\?(.*)\\?=\"?"),
                                        QRegularExpression::InvertedGreedinessOption);
         // we set InvertedGreedinessOption, to match sequences which do not have white space in between 2 encoded words; otherwise by default greedy matching is performed
         // eg. "Sm=?ISO-8859-1?B?9g==?=rg=?ISO-8859-1?B?5Q==?=sbord" will match "=?ISO-8859-1?B?9g==?=rg=?ISO-8859-1?B?5Q==?=" as a single encoded word without InvertedGreedinessOption
diff --git a/src/qwwsmtpclient/qwwsmtpclient.cpp b/src/qwwsmtpclient/qwwsmtpclient.cpp
index 8fe81ca32..da348d4f8 100644
--- a/src/qwwsmtpclient/qwwsmtpclient.cpp
+++ b/src/qwwsmtpclient/qwwsmtpclient.cpp
@@ -149,8 +149,8 @@ void QwwSmtpClientPrivate::_q_readFromSocket() {
     while (socket->canReadLine()) {
         QString line = socket->readLine();
         emit q->logReceived(line.toUtf8());
-        QRegularExpression rx("(*ANYCRLF)^(\\d+)-(.*)$", QRegularExpression::MultilineOption);        // multiline response (aka 250-XYZ)
-        QRegularExpression rxlast("(*ANYCRLF)^(\\d+) (.*)$", QRegularExpression::MultilineOption);    // single or last line response (aka 250 XYZ)
+        static const QRegularExpression rx("(*ANYCRLF)^(\\d+)-(.*)$", QRegularExpression::MultilineOption);        // multiline response (aka 250-XYZ)
+        static const QRegularExpression rxlast("(*ANYCRLF)^(\\d+) (.*)$", QRegularExpression::MultilineOption);    // single or last line response (aka 250 XYZ)
         // multiline
         QRegularExpressionMatch mid_match = rx.match(line);
         if (mid_match.hasMatch()) {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.