Skip to content

Add [[nodiscard]] to view-returning public methods#1600

Closed
DaviBaum wants to merge 1 commit intomongodb:masterfrom
DaviBaum:add-nodiscard-to-view-methods
Closed

Add [[nodiscard]] to view-returning public methods#1600
DaviBaum wants to merge 1 commit intomongodb:masterfrom
DaviBaum:add-nodiscard-to-view-methods

Conversation

@DaviBaum
Copy link

Summary

  • Annotate public methods returning non-owning view types with [[nodiscard]] across both v1/ and v_noabi/ namespaces in bsoncxx and mongocxx headers.
  • Target return types include document::view, array::view, element::view, string_view, types::view, bson_value::view, view_or_value, and optional<> wrappers around these types.
  • Silently discarding a view return value is almost always a bug (dead code or a missing check). This change surfaces those issues at compile time via -Wunused-result.

Scope

  • 140 header files modified, ~434 annotations added.
  • No runtime behavior changes — [[nodiscard]] is a compile-time warning only.
  • No ABI impact.
  • Conversion operators (operator view(), operator bool()) and XMACRO-generated get_*() methods are intentionally excluded.
  • Formatted with uv run --frozen etc/format.py.

Test plan

  • cmake --build build --target bsoncxx_shared mongocxx_shared passes with zero errors.
  • CI should confirm no new warnings or test failures.

@DaviBaum DaviBaum requested a review from a team as a code owner March 25, 2026 00:08
@DaviBaum DaviBaum requested a review from rcsanchez97 March 25, 2026 00:08
Annotate public methods returning non-owning view types with
[[nodiscard]] to catch accidental discard of return values at
compile time. Covers both v1/ and v_noabi/ namespaces across
bsoncxx and mongocxx headers.

Target return types include document::view, array::view,
element::view, string_view, types::view, bson_value::view,
view_or_value, and optional<> wrappers around these types.
@DaviBaum DaviBaum force-pushed the add-nodiscard-to-view-methods branch from 3c16e5a to 1f8ce2c Compare March 25, 2026 01:22
@kevinAlbs kevinAlbs removed the request for review from rcsanchez97 March 25, 2026 15:56
@kevinAlbs
Copy link
Collaborator

Thank you for the contribution @DaviBaum. Closing since we do not want to proceed with this change. [[nodiscard]] was added in C++17 but the C++ driver API supports C++11. Even for C++17, I expect this could break compile for existing consumers and would require a major version release.

@kevinAlbs kevinAlbs closed this Mar 25, 2026
@DaviBaum DaviBaum deleted the add-nodiscard-to-view-methods branch March 25, 2026 16:42
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.

2 participants