Add OIIO_NODISCARD to ImageInput create/open/read methods#5111
Add OIIO_NODISCARD to ImageInput create/open/read methods#5111mvanhorn wants to merge 1 commit intoAcademySoftwareFoundation:mainfrom
Conversation
|
|
Annotates ImageInput::create(), open(), read_image(), and read_scanlines() methods with OIIO_NODISCARD so callers get a compiler warning when they ignore return values that indicate success or failure. Partial fix for AcademySoftwareFoundation#4781 - scoped to ImageInput class per maintainer guidance to "pick just one or two classes" and iterate. Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
6ac7875 to
50ed282
Compare
|
Did you not run the CI before submitting the PR? Many of the CI job variants are failing -- for the obvious reason that OIIO itself calls these functions, often without checking the return code (I know, bad), so those will now be errors and break our build. Changes to the function declarations to add these annotations must be accompanied by changes to the call sites where we have up until now neglected the return values. That's part of why I wanted to go slowly, a few calls at a time, because we'll want to carefully review and ensure that all the places that we must add the error checks are handling those previously-unchecked errors sensibly, so we want small, easily reviewable PRs for that sweeping set of changes. As for why some job variants pass and others fail, I suspect that is reflecting which compiler, version, and C++ standard they are using. |
|
I like the idea of migrating to heavy use of nodiscard, but I fear how much user code will have broken builds as a result (as our own was, revealed by the CI!). Maybe there's a way to gently transition people? I think there are two classes of return values we may wish to annotate as "nodiscard":
Definitely for case 1, we should not hesitate to annotate with OIIO_NODISCARD now. But for case 2, I wonder what people think of the merits of creating a second macro #ifndef OIIO_CHECK_DISCARDED_ERROR_RETURN
# define OIIO_CHECK_DISCARDED_ERROR_RETURN 0
#endif
#if OIIO_CHECK_DISCARDED_ERROR_RETURN
# define OIIO_NODISCARD_ERROR OIIO_NODISCARD
#else
# define OIIO_NODISCARD_ERROR
#endifSo what this would do is:
So, for example, a method like Opinions? |
It looks like clang is always flagging the nodiscard errors, but gcc never is. We should look into why this is the case -- I would have expected more recent gcc versions to also turn them into errors. Have we done something wrong, or perhaps is something else needed for clang? |
|
You're right - I should have run CI before submitting. Apologies for the noise. The two-tier approach makes sense. I'll rework this to:
On the clang vs gcc question: gcc only treats I'll push the reworked version with call site fixes shortly. |
|
I prefer OIIO_NODISCARD_ERROR rather than OIIO_NODISCARD_GENTLE, in case we later identify other cases (aside from error status returns) that we want to independently select as gentle or harsh. |
|
Good call on OIIO_NODISCARD_ERROR - clearer that it's specific to error-status returns and leaves room for other nodiscard categories later. Will use that naming in the rework. |
Summary
Adds
OIIO_NODISCARDtoImageInput::create(),open(),read_image(), andread_scanlines()methods. Callers ignoring return values from these methods now get a compiler warning.Why this matters
As noted in #4781, it's easy to accidentally ignore the bool return from
read_image()oropen(). These methods return false on failure, and silently proceeding with bad data leads to hard-to-debug issues.create()returns a unique_ptr that should never be discarded.Changes
src/include/OpenImageIO/imageio.h: AddedOIIO_NODISCARDto 14 method declarations in theImageInputclass, coveringcreate()(2 overloads),open()(4 overloads),read_image()(4 overloads), andread_scanlines()(4 overloads).Scoped to
ImageInputonly per @lgritz's guidance to start with one class and iterate.Testing
Partial fix for #4781
This contribution was developed with AI assistance (Claude Code).