Skip to content

Add [[nodiscard]] annotations to generated C++ code #242

@AaronWebster

Description

@AaronWebster

Summary

Consider adding [[nodiscard]] annotations to generated C++ code for methods like Ok(), Read(), TryToWrite(), etc. to help catch bugs where return values are accidentally ignored.

Background

This was originally part of PR #235 (optimization for conditional fields), but was requested to be split out for separate discussion since [[nodiscard]] is a C++17 feature and there may need to be a broader discussion about C++14 vs C++17 support.

Considerations

  • [[nodiscard]] was introduced in C++17
  • Current Emboss may support C++14 users
  • Options to consider:
    1. Require C++17 minimum and add [[nodiscard]] unconditionally
    2. Use a macro that expands to [[nodiscard]] on C++17+ and nothing on C++14
    3. Keep C++14 compatibility and don't add [[nodiscard]]

Affected methods

Key methods that would benefit from [[nodiscard]]:

  • Ok() - return value indicates validity
  • Read() / UncheckedRead() - return values are the point of the call
  • TryToWrite() / TryToCopyFrom() - return value indicates success
  • IsComplete() - return value indicates completeness
  • CouldWriteValue() - return value indicates writability

cc @EricRahm (requested this be discussed separately)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions