How not to create temporary QRegularExpression objects

1.8k views Asked by At

I am getting this warning in Qt Creator:

Don't create temporary QRegularExpression objects. Use a static QRegularExpression object instead [clazy-use-static-qregularexpression]

And it's regarding the code snippet below:

QRegularExpression re("SEARCHING...",QRegularExpression::CaseInsensitiveOption);
QRegularExpressionMatch match = re.match(frame);
if (match.hasMatch()) {

It's not obvious to me, how should I use the QRegularExpression instead?

2

There are 2 answers

7
JarMan On BEST ANSWER

That's a clazy warning message which you can find a description of here. It's just suggesting that you don't want to keep recreating the QRegularExpression every time you enter that function because the expression is always the same. So doing something like this will work, and improve the performance of your code:

static QRegularExpression re("SEARCHING...", QRegularExpression::CaseInsensitiveOption);
QRegularExpressionMatch match = re.match(frame);
if (match.hasMatch()) {
0
Eduardo Gonzalez On

My solution was to use Q_GLOBAL_STATIC_WITH_ARGS

I defined the global expression in the source file that I use it.

Q_GLOBAL_STATIC_WITH_ARGS(QRegularExpression, regExp, {"SEARCHING..."});

And I used like this:

auto match = regExp->match(frame);
if (match.hasMatch()) {