From ae0b080c019025c2a949cf9468294ecf2b4eacb1 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 4 Aug 2021 10:11:55 -0700 Subject: [PATCH] QStringLiteral: suppress the clang-tidy warning about const_cast We can't add the comment inside the macro, so we solve the problem by adding an extra level of indirection. Pick-to: 6.2 Change-Id: Ib8fbfcfeb48a49ca945dfffd169829b3610f6a34 Reviewed-by: Rui Oliveira Reviewed-by: Marc Mutz --- src/corelib/text/qstringliteral.h | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/corelib/text/qstringliteral.h b/src/corelib/text/qstringliteral.h index d86e6eafd9..d9804b31d6 100644 --- a/src/corelib/text/qstringliteral.h +++ b/src/corelib/text/qstringliteral.h @@ -56,14 +56,24 @@ QT_BEGIN_NAMESPACE // core language feature, so just use u"" here unconditionally: #define QT_UNICODE_LITERAL(str) u"" str -#define QStringLiteral(str) \ - (QString(QStringPrivate(nullptr, \ - const_cast(QT_UNICODE_LITERAL(str)), \ - sizeof(QT_UNICODE_LITERAL(str))/2 - 1))) \ - /**/ using QStringPrivate = QArrayDataPointer; +namespace QtPrivate { +template +static Q_ALWAYS_INLINE QStringPrivate qMakeStringPrivate(const char16_t (&literal)[N]) +{ + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) + auto str = const_cast(literal); + return { nullptr, str, N - 1 }; +} +} + +#define QStringLiteral(str) \ + (QString(QtPrivate::qMakeStringPrivate(QT_UNICODE_LITERAL(str)))) \ + /**/ + + QT_END_NAMESPACE #endif // QSTRINGLITERAL_H