Skip to content

CXX-3072 Reduce Warnings - Code Consistency and Quality Improvements #1178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 25, 2024

Conversation

eramongodb
Copy link
Contributor

Summary

Resolves CXX-3072. Verified by this patch.

Similar to mongodb/mongo-c-driver#1543 for the C Driver. Addresses 615 Clang warnings (83 unique warnings) with Clang 19.

scoped_bson_value

A long overdue followup to #955:

It may be worth eventually extracting this helper into a proper a proper component (probably under src/mongocxx/private?), but that work has been deferred for now.

This addresses -Wunused-member-function warnings in options.cpp due to unused functions with internal linkage (default ctor + value_for_init).

C-Style Casts

A large part of this PR is devoted to addressing -Wold-style-cast warnings.

Most cases only needed a trivial change from (T)expr to static_cast<T>(expr). Regarding numeric casts, range checks have not been added: those may be audited in the future when an assertion/contract methodology for the C++ Driver is implemented (directly reusing C Driver's BSON_ASSERT() does not seem ideal).

A notable change is the use of const_cast to workaround the void* parameter (lack of const) in bsoncxx::v_noabi::types::bson_value::view's private member functions. To avoid unnecessarily breaking ABI compatibility, a const_cast is used instead of updating the parameter type, accompanied by a comment documenting the context and its validity (the const is recovered in the _init function + corresponding convert_from_libbson() overloads updated with the missing const for consistency).

Miscellaneous

Other warnings addressed by this PR include:

  • handling of semicolons (both removing redundant ones and adding some following macro use for consistency)
  • missing case statements in switches on enumerations (in error_code.cpp in particular).
    • is the write_concern::level::k_acknowledged do-nothing fallthrough in write_concer.cpp correct?
  • #if defined(X) instead of #if X when X is not guaranteed to be defined.
  • static -> inline for function definitions in headers used by multiple components (where internal linkage does not seem sufficiently motivated).
  • NULL -> nullptr even with C APIs (C idioms shouldn't leak into C++ code).
  • 0 -> nullptr in pointer comparisons and initialization.

Ignored Warnings

The following warnings were observed but deliberately unaddressed/ignored/silenced:

  • -Wdocumentation: addressed by CXX-3070 Address -Wdocumentation Clang warnings #1175.
  • -Walign-mismatch in bsoncxx/v_noabi/bsoncxx/private/stack.hh: unclear how to resolve this warning.
  • -Wexit-time-destructors for exception types: deferred to a future API major release.
  • -Wglobal-constructors + -Wexit-time-destructors for uri::k_default_uri: deferred to a future API major release.
  • -Wundefined-func-template for soft_declval: deferred to v1 migration PRs (CXX-2745).
  • -Wweak-vtables for exception types: deferred to a future API major release.

@eramongodb eramongodb requested a review from kevinAlbs July 24, 2024 16:47
@eramongodb eramongodb self-assigned this Jul 24, 2024
@kevinAlbs kevinAlbs requested a review from Julia-Garland July 24, 2024 16:57
Copy link
Contributor

@Julia-Garland Julia-Garland left a comment

Choose a reason for hiding this comment

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

Besides not being sure about your question on write_concern::level::k_acknowledged LGTM!

@@ -175,6 +175,9 @@ bsoncxx::v_noabi::document::value write_concern::to_document() const {
if (auto t = tag()) {
doc.append(kvp("w", *t));
}
break;

case write_concern::level::k_acknowledged:
Copy link
Contributor

Choose a reason for hiding this comment

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

is the write_concern::level::k_acknowledged do-nothing fallthrough in write_concern.cpp correct?

I checked out the API reference and the MongoDB docs, not sure myself.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I expect this case is unreachable. If this else block is entered, then nodes() did not return a value. That implies acknowledge_level() cannot be k_acknowledged.

I expect this could assert:

            case write_concern::level::k_acknowledged:
                // Acknowledged write concern is expected to return `nodes()` above.
                MONGOCXX_UNREACHABLE;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given prior default: behavior, opting to preserve break; behavior rather than introducing an assertion out of caution given the somewhat indirect relationship between is_acknowledged() and get_w(). However, will include a comment documenting the relationship with the returned optional.

Copy link
Collaborator

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

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

The warning reductions are appreciated. LGTM with a possible use of BSONCXX_UNREACHABLE.

@@ -175,6 +175,9 @@ bsoncxx::v_noabi::document::value write_concern::to_document() const {
if (auto t = tag()) {
doc.append(kvp("w", *t));
}
break;

case write_concern::level::k_acknowledged:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I expect this case is unreachable. If this else block is entered, then nodes() did not return a value. That implies acknowledge_level() cannot be k_acknowledged.

I expect this could assert:

            case write_concern::level::k_acknowledged:
                // Acknowledged write concern is expected to return `nodes()` above.
                MONGOCXX_UNREACHABLE;

@eramongodb eramongodb merged commit 74309fa into mongodb:master Jul 25, 2024
1 check was pending
@eramongodb eramongodb deleted the cxx-3072 branch July 25, 2024 18:28
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