Search code examples
qtclangqt-creatorqregularexpression

How not to create temporary QRegularExpression objects


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?


Solution

  • 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()) {