From 38cd3cb1261ee43e70cc5659ac72149a523995d3 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 21 Jun 2023 11:37:03 +0200 Subject: [PATCH] Short live Q_NODISCARD_(CTOR_)X! Wrappers around P1301 [[nodiscard("reason")]]. [ChangeLog][QtCore][Q_NODISCARD_X/Q_NODISCARD_CTOR_X] Added as wrappers around C++20 [[nodiscard("reason")]]. Task-number: QTBUG-114767 Change-Id: Ie566d9c9d500ef632c7e243af97081f83506a752 Reviewed-by: Qt CI Bot Reviewed-by: Ivan Solovev --- src/corelib/global/qcompilerdetection.h | 13 +++++++++++++ src/corelib/global/qcompilerdetection.qdoc | 15 +++++++++++++++ tests/auto/corelib/global/qglobal/tst_qglobal.cpp | 13 +++++++++---- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/corelib/global/qcompilerdetection.h b/src/corelib/global/qcompilerdetection.h index e733b80a9b..51b9bc1018 100644 --- a/src/corelib/global/qcompilerdetection.h +++ b/src/corelib/global/qcompilerdetection.h @@ -959,6 +959,13 @@ # ifndef Q_NODISCARD_CTOR # define Q_NODISCARD_CTOR [[nodiscard]] # endif +// [[nodiscard("reason")]] (P1301) +# ifndef Q_NODISCARD_X +# define Q_NODISCARD_X(message) [[nodiscard(message)]] +# endif +# ifndef Q_NODISCARD_CTOR_X +# define Q_NODISCARD_CTOR_X(message) [[nodiscard(message)]] +# endif #endif #if __has_cpp_attribute(maybe_unused) @@ -1010,9 +1017,15 @@ #ifndef Q_REQUIRED_RESULT # define Q_REQUIRED_RESULT #endif +#ifndef Q_NODISCARD_X +# define Q_NODISCARD_X(message) Q_REQUIRED_RESULT +#endif #ifndef Q_NODISCARD_CTOR # define Q_NODISCARD_CTOR #endif +#ifndef Q_NODISCARD_CTOR_X +# define Q_NODISCARD_CTOR_X(message) Q_NODISCARD_CTOR +#endif #ifndef Q_DECL_DEPRECATED # define Q_DECL_DEPRECATED #endif diff --git a/src/corelib/global/qcompilerdetection.qdoc b/src/corelib/global/qcompilerdetection.qdoc index 5074d8f6c1..1f521e31f6 100644 --- a/src/corelib/global/qcompilerdetection.qdoc +++ b/src/corelib/global/qcompilerdetection.qdoc @@ -409,4 +409,19 @@ implement the feature. If your supported platforms all allow \c{[[nodiscard]]} on constructors, we strongly recommend you use the C++ attribute directly instead of this macro. + + \sa Q_NODISCARD_CTOR_X +*/ + +/*! + \macro Q_NODISCARD_X(message) + \macro Q_NODISCARD_CTOR_X(message) + \relates + \since 6.7 + + \brief Expand to \c{[[nodiscard(message)]]} on compilers that support the feature. + + Otherwise they expand to [[nodiscard]] and Q_NODISCARD_CTOR, respectively. + + \sa`Q_NODISCARD_CTOR */ diff --git a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp index 3d9759ff5b..e175db24b1 100644 --- a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp +++ b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp @@ -37,7 +37,7 @@ private slots: void qRoundDoubles(); void PRImacros(); void testqToUnderlying(); - void nodiscardConstructor(); + void nodiscard(); }; extern "C" { // functions in qglobal.c @@ -692,16 +692,19 @@ void tst_QGlobal::testqToUnderlying() QCOMPARE(qToUnderlying(EE2), 456UL); } -void tst_QGlobal::nodiscardConstructor() +void tst_QGlobal::nodiscard() { - // Syntax-only test, just to make sure that Q_NODISCARD_CTOR compiles + // Syntax-only test, just to make sure that the Q_NODISCARD_* compile // on all platforms. // Other code is just to silence all various compiler warnings about // unused private members or methods. class Test { public: - Q_NODISCARD_CTOR explicit Test(int val) : m_val(val) {} + Q_NODISCARD_CTOR_X("Why construct a Test instead of just passing the int through?") + explicit Test(int val) : m_val(val) {} + Q_NODISCARD_CTOR explicit Test(float val) : m_val(int(val)) {} + Q_NODISCARD_X("Why call get() if you don't use the returned value, hu?") // NOT idiomatic use! int get() const { return m_val; } private: @@ -710,6 +713,8 @@ void tst_QGlobal::nodiscardConstructor() Test t{42}; QCOMPARE(t.get(), 42); + Test t2{42.0f}; + QCOMPARE(t2.get(), 42); } QTEST_APPLESS_MAIN(tst_QGlobal)