Skip to content

enh(all): Add [[nodiscard]] and [[maybe_unused]] attributes#5156

Open
mikomikotaishi wants to merge 1 commit intopocoproject:mainfrom
mikomikotaishi:main
Open

enh(all): Add [[nodiscard]] and [[maybe_unused]] attributes#5156
mikomikotaishi wants to merge 1 commit intopocoproject:mainfrom
mikomikotaishi:main

Conversation

@mikomikotaishi
Copy link
Contributor

@mikomikotaishi mikomikotaishi commented Jan 8, 2026

Addresses part of #5152. This PR adds [[nodiscard]] attributes to many methods and types that would otherwise be important not to discard.

Unlike POCO_DEPRECATED() which can be disabled, I am not going to put [[nodiscard]] behind a macro as using deprecated symbols is intended while discarding a value is certainly a programming error.

@mikomikotaishi mikomikotaishi force-pushed the main branch 4 times, most recently from c5add77 to 6f042a9 Compare January 8, 2026 16:19
@mikomikotaishi
Copy link
Contributor Author

mikomikotaishi commented Jan 8, 2026

I have also moved POCO_UNUSED to [[maybe_unused]], seeing as it is a standard attribute in C++17, but I think this is probably not needed any more. POCO_UNUSED has been removed entirely as [[maybe_unused]] is a standard attribute and is portably representable.

@mikomikotaishi mikomikotaishi force-pushed the main branch 2 times, most recently from 7dbfa1f to d77d238 Compare January 8, 2026 17:12
@mikomikotaishi
Copy link
Contributor Author

mikomikotaishi commented Jan 9, 2026

Would it be possible to get this merged before the 1.15 release? I understand if not, but it is nearly done. I have covered all methods across the project.

@mikomikotaishi mikomikotaishi force-pushed the main branch 6 times, most recently from 479e634 to 6f154e8 Compare January 9, 2026 10:04
Copy link
Contributor

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CodeQL found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@matejk
Copy link
Contributor

matejk commented Jan 10, 2026

Would it be possible to get this merged before the 1.15 release? I understand if not, but it is nearly done. I have covered all methods across the project.

It is quite a big change. I'll need some time for review.

@mikomikotaishi mikomikotaishi changed the title Add [[nodiscard]] attributes enh(all): Add [[nodiscard]] and [[maybe_unused]] attributes Jan 10, 2026
@matejk
Copy link
Contributor

matejk commented Jan 12, 2026

There are lots of changes. Instead of adding comments to the source code I provide some general observations.

@matejk
Copy link
Contributor

matejk commented Jan 12, 2026

[[nodiscard]] on Enum Types

  enum [[nodiscard]] EventType { ... }
  enum [[nodiscard]] Priority { ... }
  enum [[nodiscard]] SelectMode { ... }
  enum [[nodiscard]] Storage { ... }
  enum [[nodiscard]] Allocation : unsigned char { ... }

This is unusual and mostly useless. [[nodiscard]] on an enum means functions returning that enum type should have their return value checked. But enum values are typically used as parameters, not return values. The attribute on the enum declaration itself doesn't do anything for enum values.

@matejk
Copy link
Contributor

matejk commented Jan 12, 2026

[[nodiscard]] on Internal/Helper Classes

  class [[nodiscard]] FunctorRunnable: public Runnable { ... }
  class [[nodiscard]] ValueHolder { ... }
  class [[nodiscard]] Block { ... }
  class [[nodiscard]] FDCompare { ... }

These are internal classes that users don't directly construct or receive from functions. Adding [[nodiscard]] adds noise without benefit.

@matejk
Copy link
Contributor

matejk commented Jan 12, 2026

[[nodiscard]] on Dereference Operators

  [[nodiscard]] C* operator -> ()
  [[nodiscard]] C& operator * ()

This is wrong. These operators are used for accessing members:

  ptr->doSomething();  // This would warn! The return of -> is "discarded"

The -> operator result IS used implicitly. This will likely cause spurious warnings.

@matejk
Copy link
Contributor

matejk commented Jan 12, 2026

[[nodiscard]] on Nullable and Optional Classes

  class [[nodiscard]] Nullable { ... }
  class [[nodiscard]] Optional { ... }

Reconsider this. While it ensures you use the object, these types are commonly constructed just to be assigned to variables or passed around. The standard library's std::optional is not marked nodiscard.

@mikomikotaishi
Copy link
Contributor Author

mikomikotaishi commented Jan 12, 2026

I decided to put nodiscard on enums and class internal types because I wanted to ensure that data like this is not discarded.

I have gone ahead and applied your requested changes, nonetheless.

@matejk
Copy link
Contributor

matejk commented Jan 13, 2026

@mikomikotaishi , you don't have to force push. It is harder to track changes between the commits when force pushed.

@mikomikotaishi
Copy link
Contributor Author

Noted

@andrewauclair
Copy link
Contributor

While it ensures you use the object, these types are commonly constructed just to be assigned to variables or passed around.

Not sure what you mean by this, but [[nodiscard]] on the enum or class declaration only applies to functions returning that type by value.

@mikomikotaishi
Copy link
Contributor Author

mikomikotaishi commented Jan 15, 2026

Is there anything else I should address in this PR, pending reviews from the other reviewers?

@matejk
Copy link
Contributor

matejk commented Jan 16, 2026

Build with enabled compiler warnings shall be inspected whether introduction of attributes introduced new warnings and then decide how to fix those warnings:

  • change the code at the warning point
  • revert introduction of the attribute

Two jobs with enabled warnings are built on github: macos-clang-cmake-warnings and macos-msvc-cmake-warnings.

@mikomikotaishi
Copy link
Contributor Author

mikomikotaishi commented Jan 17, 2026

Apologies, accidentally force-pushed instead of creating a new commit. (I'll force-push when making amendments from now on and use proper commits when adding something new.)

Anyway, I think I've done what I can to remove the warnings we were getting.

@mikomikotaishi mikomikotaishi force-pushed the main branch 10 times, most recently from 993c2ff to 79b5c6f Compare January 18, 2026 19:38
@mikomikotaishi
Copy link
Contributor Author

Never mind, I see the problem now, I accidentally overwrote the handled variable.

@mikomikotaishi
Copy link
Contributor Author

@matejk Is there anything to do next?

@matejk
Copy link
Contributor

matejk commented Jan 26, 2026

I am quite busy with other projects currently. I'll take a look ASAP. Thank you so much for your contribution so far.

@mikomikotaishi
Copy link
Contributor Author

Hi @matejk, is there still any time to eventually review and merge this?

@matejk
Copy link
Contributor

matejk commented Feb 9, 2026

Hi, sorry for not answering. We are preparing release 1.15.0. Changes in this PR will be included in the next release.

Thank you for your patience.

@mikomikotaishi
Copy link
Contributor Author

So would it be possible to roll out this in a minor release (like 1.15.1)?

@mikomikotaishi
Copy link
Contributor Author

Now that 1.15.0 is rolled out, could we look towards integrating this for 1.15.1 perhaps?

@mikomikotaishi
Copy link
Contributor Author

@matejk also I think the website needs to be updated for 1.15.0, it still says 1.14.2

@mikomikotaishi
Copy link
Contributor Author

I'd also like to ask here (before making an issue that might just be not worth investigating): is there any reason why we primarily use unscoped C-style enums rather than enum class? If possible I think it's worthwhile migrating to enum class, and making it an option during compilation (perhaps remaining with enum would be a better default choice until later on when it could be deprecated).

@matejk
Copy link
Contributor

matejk commented Feb 14, 2026

I'd also like to ask here (before making an issue that might just be not worth investigating): is there any reason why we primarily use unscoped C-style enums rather than enum class? If possible I think it's worthwhile migrating to enum class, and making it an option during compilation (perhaps remaining with enum would be a better default choice until later on when it could be deprecated).

The reason is very simple: lots of Poco code was written before C++11 standard. Probably some of the enums could be converted to enum class. Probably not all.

@matejk
Copy link
Contributor

matejk commented Feb 14, 2026

Now that 1.15.0 is rolled out, could we look towards integrating this for 1.15.1 perhaps?

We'll include into 1.16.0 with the next round of C++ modernisation.

@andrewauclair
Copy link
Contributor

andrewauclair commented Feb 14, 2026

I'd also like to ask here (before making an issue that might just be not worth investigating): is there any reason why we primarily use unscoped C-style enums rather than enum class? If possible I think it's worthwhile migrating to enum class, and making it an option during compilation (perhaps remaining with enum would be a better default choice until later on when it could be deprecated).

As a user of Poco and one who has done these types of changes, I'd suggest a middle ground approach. It's more forgiving on user code to use enum class in combo with using enum. That gives proper type checking without a crap ton of errors and code changes for the scoping part.

@mikomikotaishi
Copy link
Contributor Author

We'll include into 1.16.0 with the next round of C++ modernisation.

Could this still be merged for the time being or will we have to wait until work on 1.16.0 begins before merging this into the library?

That gives proper type checking without a crap ton of errors and code changes for the scoping part.

I agree, I think the primary goal of moving towards enum class is at having the benefits of type safety. Perhaps it could be a compile option on whether to enable using enum (by default it should be on for backwards compatibility, but I think it's better to move towards eventually deprecating it with using).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants