qt6-bb10/src/concurrent
Giuseppe D'Angelo 7466831509 Long live [[nodiscard]] QFile::open
Having already caught some bugs in real code because of unchecked calls
to QFile::open, this commit marks QFile::open (and open() in other
file-I/O classes) as [[nodiscard]].

Since it's going to raise warnings, the plan is to keep the existing
behavior up to and including the next LTS. Then the warnings will switch
on by default. All of this is protected by system of macros to opt-in or
opt-out the behavioral change at any time.

A possible counter-argument for doing this is that QFile::open is also
used for opening files in the the resource system, and that opening
"cannot fail". It clearly can, if the resource is moved away or renamed;
code should at a minimum use a Q_ASSERT in debug builds. Another
counter-argument is the opening of file handles or descriptors; but
again, that opening may fail in case the handle has been closed or if
the flags are incompatible.

---

Why not marking *every* open() override? Because some are not meant to
be called directly -- for instance sockets are supposed to be open via
calls to `connectToHost` or similar.

One notable exception is QIODevice::open() itself. Although rarely
called directly by user code (which just calls open() on a specific
subclass, which likely has an override), it may be called:

1) By code that just takes a `QIODevice *` and does something with it.
   That code is arguably more rare than code using QFile directly.
   Still, being "generic" code, they have an extra responsibility when
   making sure to handle a possible opening failure.

2) By QIODevice subclasses, which are even more rare. However, they
   usually ignore the return from QIODevice::open() as it's
   unconditionally true. (QIODevice::open() doesn't use the protected
   virtual pattern.)

I'll try and tackle QIODevice in a future commit.

[ChangeLog][QtCore][QFileDevice] The open() functions of file-related
I/O classes (such as QFile, QSaveFile, QTemporaryFile) can now be marked
with the "nodiscard" attribute, in order to prevent a category of bugs
where the return value of open() is not checked and the file is then
used. In order to avoid warnings in existing code, the marking can be
opted in or out, by defining QT_USE_NODISCARD_FILE_OPEN or the
QT_NO_USE_NODISCARD_FILE_OPEN macros. By default, Qt will automatically
enable nodiscard on these functions starting from Qt 6.10.

Change-Id: Ied940e1c0a37344f5200b2c51b05cd1afcb2557d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2024-04-17 20:07:47 +02:00
..
doc Correct doc snippet license 2024-02-28 19:15:51 +01:00
CMakeLists.txt Long live [[nodiscard]] QFile::open 2024-04-17 20:07:47 +02:00
qtaskbuilder.h Replace usages of Q_CLANG_QDOC with Q_QDOC 2022-10-21 09:48:36 +02:00
qtaskbuilder.qdoc Doc: Qt Concurrent: Fully qualify \typedef documentation 2024-02-13 08:13:57 +00:00
qtconcurrent_global.h Use SPDX license identifiers 2022-05-16 16:37:38 +02:00
qtconcurrentcompilertest.h Use SPDX license identifiers 2022-05-16 16:37:38 +02:00
qtconcurrentfilter.cpp Add \brief descriptions to QtConcurrent topics 2022-07-20 13:15:59 +02:00
qtconcurrentfilter.h Replace usages of Q_CLANG_QDOC with Q_QDOC 2022-10-21 09:48:36 +02:00
qtconcurrentfilterkernel.h Replace usages of Q_CLANG_QDOC with Q_QDOC 2022-10-21 09:48:36 +02:00
qtconcurrentfunctionwrappers.h Replace usages of Q_CLANG_QDOC with Q_QDOC 2022-10-21 09:48:36 +02:00
qtconcurrentiteratekernel.cpp Fix QThreadPool::maxThreadCount() usage 2024-01-08 18:29:26 +01:00
qtconcurrentiteratekernel.h ResultReporter: fix uninitialized int member 2024-04-11 21:12:13 +02:00
qtconcurrentmap.cpp Add \brief descriptions to QtConcurrent topics 2022-07-20 13:15:59 +02:00
qtconcurrentmap.h Replace usages of Q_CLANG_QDOC with Q_QDOC 2022-10-21 09:48:36 +02:00
qtconcurrentmapkernel.h Replace usages of Q_CLANG_QDOC with Q_QDOC 2022-10-21 09:48:36 +02:00
qtconcurrentmedian.h Replace usages of Q_CLANG_QDOC with Q_QDOC 2022-10-21 09:48:36 +02:00
qtconcurrentreducekernel.h Fix QThreadPool::maxThreadCount() usage 2024-01-08 18:29:26 +01:00
qtconcurrentrun.cpp Doc: Fix \fn template arguments for Qt Concurrent 2023-11-18 13:01:53 +01:00
qtconcurrentrun.h QtConcurrent::run: point return value ignorers to QThreadPool::start(Callable&&) 2023-07-31 20:09:03 +00:00
qtconcurrentrunbase.h Use SPDX license identifiers 2022-05-16 16:37:38 +02:00
qtconcurrentstoredfunctioncall.h Use SPDX license identifiers 2022-05-16 16:37:38 +02:00
qtconcurrenttask.h Replace usages of Q_CLANG_QDOC with Q_QDOC 2022-10-21 09:48:36 +02:00
qtconcurrenttask.qdoc Add \brief descriptions to QtConcurrent topics 2022-07-20 13:15:59 +02:00
qtconcurrentthreadengine.cpp Make qYieldCpu() public API 2023-07-25 07:21:56 -07:00
qtconcurrentthreadengine.h Replace usages of Q_CLANG_QDOC with Q_QDOC 2022-10-21 09:48:36 +02:00