Add [[nodiscard]] to overloads of static events that return winrt::event_token#1559
Add [[nodiscard]] to overloads of static events that return winrt::event_token#1559YexuanXiao wants to merge 1 commit intomicrosoft:masterfrom
Conversation
|
Users may want to permanently subscribe to an event for the lifetime of their program, in which case discarding the event token seems intentional. |
|
It does not affect non-static events, and their overloads that return winrt::event_token still do not have this attribute. Non-static events can be released through the object they are associated with, so discarding the return value does not cause a leak, whereas static events do not have such an object. The impact of this patch is very small; if users wish to discard the return value, they can use (void)expr. |
|
I think we should only add [[nodiscard]] if dropping the value is always considered an error |
|
I'm not entirely sure which static events might need to be subscribed to permanently, but at least the Clipboard example performs proper cleanup and I don't believe that well-written code should allow such a leak. |
Some events in WinRT are static, such as Clipboard.ContentChanged.For these events, the only way to release a delegate is through
winrt::event_token, because the delegate is not stored in any user-controllable object. Therefore, if thewinrt::event_tokenreturned by these events is discarded, the delegate will leak. This PR adds the[[nodiscard]]attribute to the overloads that returnwinrt::event_tokenfor static events to indicate that their return value should not be ignored, just like the overloads that return a revoker.