From 3bdb551225bd4a663af39c66fdd1f27b12b7d90a Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Nov 2025 09:14:05 -0600 Subject: [PATCH 01/39] Silence output on check for GNU parallel binary --- etc/run-clang-tidy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/run-clang-tidy.sh b/etc/run-clang-tidy.sh index 47efcd3900..88632651bb 100755 --- a/etc/run-clang-tidy.sh +++ b/etc/run-clang-tidy.sh @@ -11,7 +11,7 @@ if [[ "${distro_id:?}" != rhel* ]]; then exit 1 fi -if ! command -V parallel >/dev/null; then +if ! command -v parallel >/dev/null; then sudo yum install -q -y parallel fi From 6c28bb8de2385ae833d3adf58260ca8111bd7a7b Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Nov 2025 09:14:05 -0600 Subject: [PATCH 02/39] CXX-2338 migrate clang-tidy to rhel9-latest distro --- .evergreen/config_generator/components/clang_tidy.py | 6 +++--- .evergreen/config_generator/etc/distros.py | 2 +- .evergreen/generated_configs/tasks.yml | 4 ++-- etc/run-clang-tidy.sh | 4 +--- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/.evergreen/config_generator/components/clang_tidy.py b/.evergreen/config_generator/components/clang_tidy.py index ca04269503..9f0cb0764f 100644 --- a/.evergreen/config_generator/components/clang_tidy.py +++ b/.evergreen/config_generator/components/clang_tidy.py @@ -6,7 +6,7 @@ from config_generator.components.funcs.install_c_driver import InstallCDriver from config_generator.components.funcs.setup import Setup -from config_generator.etc.distros import compiler_to_vars, find_small_distro +from config_generator.etc.distros import compiler_to_vars, find_large_distro from config_generator.etc.function import Function from config_generator.etc.utils import bash_exec @@ -40,8 +40,8 @@ def functions(): def tasks(): - distro_name = 'rhel80' - distro = find_small_distro(distro_name) + distro_name = 'rhel9-latest' + distro = find_large_distro(distro_name) return [ EvgTask( diff --git a/.evergreen/config_generator/etc/distros.py b/.evergreen/config_generator/etc/distros.py index 2fbafc5987..3a6e64b8f8 100644 --- a/.evergreen/config_generator/etc/distros.py +++ b/.evergreen/config_generator/etc/distros.py @@ -63,6 +63,7 @@ def ls_distro(name, **kwargs): ] RHEL_DISTROS = [ + *ls_distro(name='rhel9-latest', os='rhel', os_type='linux', os_ver='9'), *ls_distro(name='rhel7.9', os='rhel', os_type='linux', os_ver='7.9'), *ls_distro(name='rhel80', os='rhel', os_type='linux', os_ver='8.0'), *ls_distro(name='rhel84', os='rhel', os_type='linux', os_ver='8.4'), @@ -72,7 +73,6 @@ def ls_distro(name, **kwargs): *ls_distro(name='rhel93', os='rhel', os_type='linux', os_ver='9.3'), *ls_distro(name='rhel94', os='rhel', os_type='linux', os_ver='9.4'), *ls_distro(name='rhel95', os='rhel', os_type='linux', os_ver='9.5'), - *ls_distro(name='rhel92', os='rhel', os_type='linux', os_ver='9.0'), ] RHEL_ARM64_DISTROS = [ diff --git a/.evergreen/generated_configs/tasks.yml b/.evergreen/generated_configs/tasks.yml index 1c7b38a3b6..e57460c152 100644 --- a/.evergreen/generated_configs/tasks.yml +++ b/.evergreen/generated_configs/tasks.yml @@ -151,8 +151,8 @@ tasks: - func: benchmarks-compile - func: benchmarks-run - name: clang-tidy - run_on: rhel80-small - tags: [clang-tidy, rhel80] + run_on: rhel9-latest-large + tags: [clang-tidy, rhel9-latest] commands: - func: setup - func: install_c_driver diff --git a/etc/run-clang-tidy.sh b/etc/run-clang-tidy.sh index 88632651bb..bc523806ae 100755 --- a/etc/run-clang-tidy.sh +++ b/etc/run-clang-tidy.sh @@ -11,9 +11,7 @@ if [[ "${distro_id:?}" != rhel* ]]; then exit 1 fi -if ! command -v parallel >/dev/null; then - sudo yum install -q -y parallel -fi +command -V parallel >/dev/null # shellcheck source=/dev/null . .evergreen/scripts/install-build-tools.sh From 2f728ecbdd888d3d01bd803dcc7074efd2b7dde3 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Nov 2025 09:14:05 -0600 Subject: [PATCH 03/39] Filter clang-tidy task to library sources only --- etc/run-clang-tidy.sh | 44 +++++++++---------------------------------- 1 file changed, 9 insertions(+), 35 deletions(-) diff --git a/etc/run-clang-tidy.sh b/etc/run-clang-tidy.sh index bc523806ae..f0e689d10c 100755 --- a/etc/run-clang-tidy.sh +++ b/etc/run-clang-tidy.sh @@ -19,7 +19,8 @@ install_build_tools export CMAKE_GENERATOR="Ninja" uv tool install -q clang-tidy -clang-tidy --version +version="$(clang-tidy --version | perl -lne 'print $1 if m|LLVM version (\d+\.\d+\.\d+)|')" +echo "clang-tidy version: ${version:?}" # Use ccache if available. if [[ -f "../mongoc/.evergreen/scripts/find-ccache.sh" ]]; then @@ -36,41 +37,14 @@ cmake_config_flags=( ) # Generate the compilation database file. -cmake -S . -B build "${cmake_config_flags[@]}" +cmake "${cmake_config_flags[@]}" -B build -# Some files (i.e. headers) may need to be generated during the build step. -cmake --build build +mapfile -t sources < <(find src -type f \( -name *.cc -o -name *.cpp \) | perl -lne 'print if m$.*/(?:bsoncxx|mongocxx)/lib/.*$') -# -# Each check has a name and the checks to run can be chosen using the -checks= option, which specifies a comma-separated -# list of positive and negative (prefixed with -) globs. For example: -# -# $ clang-tidy test.cpp -checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus* -# -# will disable all default checks (-*) and enable all clang-analyzer-* checks except for clang-analyzer-cplusplus* ones. -# -# The -list-checks option lists all the enabled checks. When used without -checks=, it shows checks enabled by default. -# Use -checks=* to see all available checks or with any other value of -checks= to see which checks are enabled by this -# value. -# -# see https://clang.llvm.org/extra/clang-tidy -# - -echo "Running clang-tidy with configuration:" -clang-tidy -p=build -dump-config - -find_args=( - -type f - \( -name *.hh -o -name *.hpp -o -name *.cpp \) # All sources including headers. - -not -path "*/third_party/*" # Excluding third party headers. - -not -path "*/config/*.hpp" # Excluding config headers. - -not -path "*bsoncxx/v_noabi/bsoncxx/enums/*.hpp" # Excluding X macro headers. +clang_tidy_flags=( + --quiet + -p build + --header-filter '.*/(?:bsoncxx|mongocxx)/(?:include|lib)/.*' ) -echo "Scanning the following files:" -find src "${find_args[@]}" - -# TODO: update clang-tidy config and address warnings. -{ - find src "${find_args[@]}" | parallel clang-tidy --quiet -p=build {} 2>/dev/null -} || true +printf "%s\n" "${sources[@]}" | parallel -q "${parallel_flags[@]}" clang-tidy "${clang_tidy_flags[@]:?}" {} 2>/dev/null From e0f3d40c030d82cded2c45ffa5976cf3cf3392cc Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Nov 2025 09:14:05 -0600 Subject: [PATCH 04/39] clang-tidy: dump config for 21.1.1 --- .clang-tidy | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 119 insertions(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index fcff55586e..84b103c20c 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,2 +1,120 @@ -Checks: 'clang-diagnostic-*,clang-analyzer-*,-*,cert-*,cppcoreguidelines-*,clang-analyzer-*' +--- +Checks: 'clang-diagnostic-*,clang-analyzer-*,clang-diagnostic-*,clang-analyzer-*,-*,cert-*,cppcoreguidelines-*,clang-analyzer-*' WarningsAsErrors: '' +HeaderFileExtensions: + - '' + - h + - hh + - hpp + - hxx +ImplementationFileExtensions: + - c + - cc + - cpp + - cxx +HeaderFilterRegex: '' +ExcludeHeaderFilterRegex: '' +FormatStyle: none +User: ec2-user +CheckOptions: + cert-arr39-c.WarnOnOffsetDividedBySizeOf: 'true' + cert-arr39-c.WarnOnSizeOfCompareToConstant: 'false' + cert-arr39-c.WarnOnSizeOfConstant: 'false' + cert-arr39-c.WarnOnSizeOfInLoopTermination: 'true' + cert-arr39-c.WarnOnSizeOfIntegerExpression: 'false' + cert-arr39-c.WarnOnSizeOfPointer: 'false' + cert-arr39-c.WarnOnSizeOfPointerToAggregate: 'false' + cert-arr39-c.WarnOnSizeOfThis: 'false' + cert-ctr56-cpp.IgnoreInheritedVirtualFunctions: 'false' + cert-dcl16-c.IgnoreMacros: 'true' + cert-dcl16-c.NewSuffixes: 'L;LL;LU;LLU' + cert-dcl37-c.AggressiveDependentMemberLookup: 'false' + cert-dcl37-c.AllowedIdentifiers: '' + cert-dcl37-c.Invert: 'false' + cert-dcl51-cpp.AggressiveDependentMemberLookup: 'false' + cert-dcl51-cpp.AllowedIdentifiers: '' + cert-dcl51-cpp.Invert: 'false' + cert-err09-cpp.CheckThrowTemporaries: 'true' + cert-err09-cpp.MaxSize: '18446744073709551615' + cert-err09-cpp.WarnOnLargeObjects: 'false' + cert-err33-c.AllowCastToVoid: 'true' + cert-err33-c.CheckedFunctions: '^::aligned_alloc$;^::asctime_s$;^::at_quick_exit$;^::atexit$;^::bsearch$;^::bsearch_s$;^::btowc$;^::c16rtomb$;^::c32rtomb$;^::calloc$;^::clock$;^::cnd_broadcast$;^::cnd_init$;^::cnd_signal$;^::cnd_timedwait$;^::cnd_wait$;^::ctime_s$;^::fclose$;^::fflush$;^::fgetc$;^::fgetpos$;^::fgets$;^::fgetwc$;^::fopen$;^::fopen_s$;^::fprintf$;^::fprintf_s$;^::fputc$;^::fputs$;^::fputwc$;^::fputws$;^::fread$;^::freopen$;^::freopen_s$;^::fscanf$;^::fscanf_s$;^::fseek$;^::fsetpos$;^::ftell$;^::fwprintf$;^::fwprintf_s$;^::fwrite$;^::fwscanf$;^::fwscanf_s$;^::getc$;^::getchar$;^::getenv$;^::getenv_s$;^::gets_s$;^::getwc$;^::getwchar$;^::gmtime$;^::gmtime_s$;^::localtime$;^::localtime_s$;^::malloc$;^::mbrtoc16$;^::mbrtoc32$;^::mbsrtowcs$;^::mbsrtowcs_s$;^::mbstowcs$;^::mbstowcs_s$;^::memchr$;^::mktime$;^::mtx_init$;^::mtx_lock$;^::mtx_timedlock$;^::mtx_trylock$;^::mtx_unlock$;^::printf_s$;^::putc$;^::putwc$;^::raise$;^::realloc$;^::remove$;^::rename$;^::scanf$;^::scanf_s$;^::setlocale$;^::setvbuf$;^::signal$;^::snprintf$;^::snprintf_s$;^::sprintf$;^::sprintf_s$;^::sscanf$;^::sscanf_s$;^::strchr$;^::strerror_s$;^::strftime$;^::strpbrk$;^::strrchr$;^::strstr$;^::strtod$;^::strtof$;^::strtoimax$;^::strtok$;^::strtok_s$;^::strtol$;^::strtold$;^::strtoll$;^::strtoul$;^::strtoull$;^::strtoumax$;^::strxfrm$;^::swprintf$;^::swprintf_s$;^::swscanf$;^::swscanf_s$;^::thrd_create$;^::thrd_detach$;^::thrd_join$;^::thrd_sleep$;^::time$;^::timespec_get$;^::tmpfile$;^::tmpfile_s$;^::tmpnam$;^::tmpnam_s$;^::tss_create$;^::tss_get$;^::tss_set$;^::ungetc$;^::ungetwc$;^::vfprintf$;^::vfprintf_s$;^::vfscanf$;^::vfscanf_s$;^::vfwprintf$;^::vfwprintf_s$;^::vfwscanf$;^::vfwscanf_s$;^::vprintf_s$;^::vscanf$;^::vscanf_s$;^::vsnprintf$;^::vsnprintf_s$;^::vsprintf$;^::vsprintf_s$;^::vsscanf$;^::vsscanf_s$;^::vswprintf$;^::vswprintf_s$;^::vswscanf$;^::vswscanf_s$;^::vwprintf_s$;^::vwscanf$;^::vwscanf_s$;^::wcrtomb$;^::wcschr$;^::wcsftime$;^::wcspbrk$;^::wcsrchr$;^::wcsrtombs$;^::wcsrtombs_s$;^::wcsstr$;^::wcstod$;^::wcstof$;^::wcstoimax$;^::wcstok$;^::wcstok_s$;^::wcstol$;^::wcstold$;^::wcstoll$;^::wcstombs$;^::wcstombs_s$;^::wcstoul$;^::wcstoull$;^::wcstoumax$;^::wcsxfrm$;^::wctob$;^::wctrans$;^::wctype$;^::wmemchr$;^::wprintf_s$;^::wscanf$;^::wscanf_s$' + cert-err33-c.CheckedReturnTypes: '^::std::error_code$;^::std::error_condition$;^::std::errc$;^::std::expected$;^::boost::system::error_code$' + cert-err61-cpp.CheckThrowTemporaries: 'true' + cert-err61-cpp.MaxSize: '18446744073709551615' + cert-err61-cpp.WarnOnLargeObjects: 'false' + cert-int09-c.AllowExplicitSequentialInitialValues: 'true' + cert-int09-c.AllowExplicitZeroFirstInitialValue: 'true' + cert-msc24-c.CustomFunctions: '' + cert-msc24-c.ReportDefaultFunctions: 'true' + cert-msc24-c.ReportMoreUnsafeFunctions: 'true' + cert-msc32-c.DisallowedSeedTypes: 'time_t,std::time_t' + cert-msc33-c.CustomFunctions: '' + cert-msc33-c.ReportDefaultFunctions: 'true' + cert-msc33-c.ReportMoreUnsafeFunctions: 'true' + cert-msc51-cpp.DisallowedSeedTypes: 'time_t,std::time_t' + cert-msc54-cpp.AsyncSafeFunctionSet: POSIX + cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField: 'false' + cert-oop57-cpp.MemCmpNames: '' + cert-oop57-cpp.MemCpyNames: '' + cert-oop57-cpp.MemSetNames: '' + cert-sig30-c.AsyncSafeFunctionSet: POSIX + cert-str34-c.CharTypdefsToIgnore: '' + cert-str34-c.DiagnoseSignedUnsignedCharComparisons: 'false' + cppcoreguidelines-avoid-c-arrays.AllowStringArrays: 'false' + cppcoreguidelines-avoid-do-while.IgnoreMacros: 'false' + cppcoreguidelines-avoid-goto.IgnoreMacros: 'false' + cppcoreguidelines-avoid-magic-numbers.IgnoreAllFloatingPointValues: 'false' + cppcoreguidelines-avoid-magic-numbers.IgnoreBitFieldsWidths: 'true' + cppcoreguidelines-avoid-magic-numbers.IgnorePowersOf2IntegerValues: 'false' + cppcoreguidelines-avoid-magic-numbers.IgnoreTypeAliases: 'false' + cppcoreguidelines-avoid-magic-numbers.IgnoreUserDefinedLiterals: 'false' + cppcoreguidelines-avoid-magic-numbers.IgnoredFloatingPointValues: '1.0;100.0;' + cppcoreguidelines-avoid-magic-numbers.IgnoredIntegerValues: '1;2;3;4;' + cppcoreguidelines-avoid-non-const-global-variables.AllowInternalLinkage: 'false' + cppcoreguidelines-explicit-virtual-functions.AllowOverrideAndFinal: 'false' + cppcoreguidelines-explicit-virtual-functions.FinalSpelling: final + cppcoreguidelines-explicit-virtual-functions.IgnoreDestructors: 'false' + cppcoreguidelines-explicit-virtual-functions.IgnoreTemplateInstantiations: 'false' + cppcoreguidelines-explicit-virtual-functions.OverrideSpelling: override + cppcoreguidelines-init-variables.IncludeStyle: llvm + cppcoreguidelines-init-variables.MathHeader: '' + cppcoreguidelines-macro-usage.AllowedRegexp: '^DEBUG_*' + cppcoreguidelines-macro-usage.CheckCapsOnly: 'false' + cppcoreguidelines-macro-usage.IgnoreCommandLineMacros: 'true' + cppcoreguidelines-missing-std-forward.ForwardFunction: '::std::forward' + cppcoreguidelines-narrowing-conversions.IgnoreConversionFromTypes: '' + cppcoreguidelines-narrowing-conversions.PedanticMode: 'false' + cppcoreguidelines-narrowing-conversions.WarnOnEquivalentBitWidth: 'true' + cppcoreguidelines-narrowing-conversions.WarnOnFloatingPointNarrowingConversion: 'true' + cppcoreguidelines-narrowing-conversions.WarnOnIntegerNarrowingConversion: 'true' + cppcoreguidelines-narrowing-conversions.WarnOnIntegerToFloatingPointNarrowingConversion: 'true' + cppcoreguidelines-narrowing-conversions.WarnWithinTemplateInstantiation: 'false' + cppcoreguidelines-no-malloc.Allocations: '::malloc;::calloc' + cppcoreguidelines-no-malloc.Deallocations: '::free' + cppcoreguidelines-no-malloc.Reallocations: '::realloc' + cppcoreguidelines-no-suspend-with-lock.LockGuards: '::std::unique_lock;::std::scoped_lock;::std::shared_lock;::std::lock_guard' + cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic: 'true' + cppcoreguidelines-non-private-member-variables-in-classes.IgnorePublicMemberVariables: 'false' + cppcoreguidelines-owning-memory.LegacyResourceConsumers: '::free;::realloc;::freopen;::fclose' + cppcoreguidelines-owning-memory.LegacyResourceProducers: '::malloc;::aligned_alloc;::realloc;::calloc;::fopen;::freopen;::tmpfile' + cppcoreguidelines-pro-bounds-constant-array-index.GslHeader: '' + cppcoreguidelines-pro-bounds-constant-array-index.IncludeStyle: llvm + cppcoreguidelines-pro-type-const-cast.StrictMode: 'false' + cppcoreguidelines-pro-type-member-init.IgnoreArrays: 'false' + cppcoreguidelines-pro-type-member-init.UseAssignment: 'false' + cppcoreguidelines-pro-type-static-cast-downcast.StrictMode: 'true' + cppcoreguidelines-rvalue-reference-param-not-moved.AllowPartialMove: 'false' + cppcoreguidelines-rvalue-reference-param-not-moved.IgnoreNonDeducedTemplateTypes: 'false' + cppcoreguidelines-rvalue-reference-param-not-moved.IgnoreUnnamedParams: 'false' + cppcoreguidelines-rvalue-reference-param-not-moved.MoveFunction: '::std::move' + cppcoreguidelines-special-member-functions.AllowImplicitlyDeletedCopyOrMove: 'false' + cppcoreguidelines-special-member-functions.AllowMissingMoveFunctions: 'false' + cppcoreguidelines-special-member-functions.AllowMissingMoveFunctionsWhenCopyIsDeleted: 'false' + cppcoreguidelines-special-member-functions.AllowSoleDefaultDtor: 'false' + cppcoreguidelines-special-member-functions.IgnoreMacros: 'true' + cppcoreguidelines-use-default-member-init.IgnoreMacros: 'true' + cppcoreguidelines-use-default-member-init.UseAssignment: 'false' + cppcoreguidelines-use-enum-class.IgnoreUnscopedEnumsInClasses: 'false' +SystemHeaders: false +... From 35e38646dae5b802b01db4b37a3e3cf0d338ff3a Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Nov 2025 09:14:05 -0600 Subject: [PATCH 05/39] clang-tidy: set User to empty string --- .clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index 84b103c20c..14fdec2e3a 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -15,7 +15,7 @@ ImplementationFileExtensions: HeaderFilterRegex: '' ExcludeHeaderFilterRegex: '' FormatStyle: none -User: ec2-user +User: '' CheckOptions: cert-arr39-c.WarnOnOffsetDividedBySizeOf: 'true' cert-arr39-c.WarnOnSizeOfCompareToConstant: 'false' From 1dbcb5ba0f8b2e9648c047c856fd28bf9eac93fb Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Nov 2025 09:14:05 -0600 Subject: [PATCH 06/39] clang-tidy: exclude non-includable headers --- .clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index 14fdec2e3a..1d2b29388f 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -13,7 +13,7 @@ ImplementationFileExtensions: - cpp - cxx HeaderFilterRegex: '' -ExcludeHeaderFilterRegex: '' +ExcludeHeaderFilterRegex: '(?:.*/bsoncxx/enums/.*|.*/(?:prelude|postlude)\.hpp$)' FormatStyle: none User: '' CheckOptions: From fc3b988fd23bd65c2dda64f212f7c152f53c448c Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Nov 2025 09:14:05 -0600 Subject: [PATCH 07/39] clang-tidy: convert checks string into list --- .clang-tidy | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index 1d2b29388f..c8954c4617 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,5 +1,13 @@ --- -Checks: 'clang-diagnostic-*,clang-analyzer-*,clang-diagnostic-*,clang-analyzer-*,-*,cert-*,cppcoreguidelines-*,clang-analyzer-*' +Checks: + - clang-diagnostic-* + - clang-analyzer-* + - clang-diagnostic-* + - clang-analyzer-* + - -* + - cert-* + - cppcoreguidelines-* + - clang-analyzer-*' WarningsAsErrors: '' HeaderFileExtensions: - '' From 4e8c3bc8e5923c6a3ff21b82de807be5a3e51bd1 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 12 Nov 2025 09:14:05 -0600 Subject: [PATCH 08/39] clang-tidy: remove redundant checks in list --- .clang-tidy | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index c8954c4617..776cc38656 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,9 +1,5 @@ --- Checks: - - clang-diagnostic-* - - clang-analyzer-* - - clang-diagnostic-* - - clang-analyzer-* - -* - cert-* - cppcoreguidelines-* From be72decb4659cf51fb022d053adc23c48daba8fa Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 3 Dec 2025 09:37:07 -0600 Subject: [PATCH 09/39] Upgrade all not-currently-warning default warnings to errors --- .clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index 776cc38656..3a655e4f0d 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -4,7 +4,7 @@ Checks: - cert-* - cppcoreguidelines-* - clang-analyzer-*' -WarningsAsErrors: '' +WarningsAsErrors: '*,-cert-err58-cpp,-cert-oop54-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-init-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-prefer-member-initializer,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-static-cast-downcast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-default-member-init,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h From e19782bc356433ee42b2748067fcd019cbc03b37 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 3 Dec 2025 09:37:07 -0600 Subject: [PATCH 10/39] Address cert-oop54-cpp warnings --- .clang-tidy | 2 +- src/bsoncxx/lib/bsoncxx/v1/element/view.cpp | 1 + src/bsoncxx/lib/bsoncxx/v1/types/value.cpp | 1 + .../lib/mongocxx/v_noabi/mongocxx/collection.cpp | 14 ++++++++------ .../lib/mongocxx/v_noabi/mongocxx/database.cpp | 14 ++++++++------ .../mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp | 15 +++++++++------ .../v_noabi/mongocxx/options/transaction.cpp | 8 +++++--- .../v_noabi/mongocxx/search_index_model.cpp | 1 + .../v_noabi/mongocxx/search_index_view.cpp | 1 + 9 files changed, 35 insertions(+), 22 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 3a655e4f0d..34e9ffc6d3 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -4,7 +4,7 @@ Checks: - cert-* - cppcoreguidelines-* - clang-analyzer-*' -WarningsAsErrors: '*,-cert-err58-cpp,-cert-oop54-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-init-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-prefer-member-initializer,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-static-cast-downcast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-default-member-init,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-init-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-prefer-member-initializer,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-static-cast-downcast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-default-member-init,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h diff --git a/src/bsoncxx/lib/bsoncxx/v1/element/view.cpp b/src/bsoncxx/lib/bsoncxx/v1/element/view.cpp index 44eb11ac01..f596c7f3b4 100644 --- a/src/bsoncxx/lib/bsoncxx/v1/element/view.cpp +++ b/src/bsoncxx/lib/bsoncxx/v1/element/view.cpp @@ -196,6 +196,7 @@ view::view(view const& other) noexcept { new (impl::with(this)) impl{impl::with(other)}; } +// NOLINTNEXTLINE(cert-oop54-cpp): handled by impl. view& view::operator=(view const& other) noexcept { *impl::with(this) = impl::with(other); return *this; diff --git a/src/bsoncxx/lib/bsoncxx/v1/types/value.cpp b/src/bsoncxx/lib/bsoncxx/v1/types/value.cpp index 97e8575336..16754a6eb4 100644 --- a/src/bsoncxx/lib/bsoncxx/v1/types/value.cpp +++ b/src/bsoncxx/lib/bsoncxx/v1/types/value.cpp @@ -155,6 +155,7 @@ value::value(value const& other) { new (impl::with(this)) impl{impl::with(other)}; } +// NOLINTNEXTLINE(cert-oop54-cpp): handled by impl. value& value::operator=(value const& other) { *impl::with(this) = impl::with(other); return *this; diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/collection.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/collection.cpp index cf11cf4c46..05e71c7270 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/collection.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/collection.cpp @@ -272,12 +272,14 @@ collection::collection(collection const& c) { } collection& collection::operator=(collection const& c) { - if (!c) { - _impl.reset(); - } else if (!*this) { - _impl = bsoncxx::make_unique(c._get_impl()); - } else { - *_impl = c._get_impl(); + if (this != &c) { + if (!c._impl) { + _impl.reset(); + } else if (!_impl) { + _impl = bsoncxx::make_unique(c._get_impl()); + } else { + *_impl = c._get_impl(); + } } return *this; diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/database.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/database.cpp index 6b84625d28..fbd759835b 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/database.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/database.cpp @@ -103,12 +103,14 @@ database::database(database const& d) { } database& database::operator=(database const& d) { - if (!d) { - _impl.reset(); - } else if (!*this) { - _impl = bsoncxx::make_unique(d._get_impl()); - } else { - *_impl = d._get_impl(); + if (this != &d) { + if (!d) { + _impl.reset(); + } else if (!*this) { + _impl = bsoncxx::make_unique(d._get_impl()); + } else { + *_impl = d._get_impl(); + } } return *this; diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp index 906b1af6e8..f09342f938 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp @@ -172,13 +172,16 @@ bucket::bucket(bucket const& b) { } bucket& bucket::operator=(bucket const& b) { - if (!b) { - _impl.reset(); - } else if (!*this) { - _impl = bsoncxx::make_unique(b._get_impl()); - } else { - *_impl = b._get_impl(); + if (this != &b) { + if (!b._impl) { + _impl.reset(); + } else if (!_impl) { + _impl = bsoncxx::make_unique(b._get_impl()); + } else { + *_impl = b._get_impl(); + } } + return *this; } diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/transaction.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/transaction.cpp index aa19b18110..bbd7e006f1 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/transaction.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/transaction.cpp @@ -30,11 +30,13 @@ transaction::transaction() : _impl{bsoncxx::make_unique()} {} transaction::transaction(transaction&&) noexcept = default; transaction& transaction::operator=(transaction&&) noexcept = default; -transaction::transaction(transaction const& other) - : _impl{bsoncxx::make_unique(other._get_impl().get_transaction_opt_t())} {} +transaction::transaction(transaction const& other) : _impl{bsoncxx::make_unique(other._get_impl())} {} transaction& transaction::operator=(transaction const& other) { - _impl = bsoncxx::make_unique(other._get_impl().get_transaction_opt_t()); + if (this != &other) { + _impl = bsoncxx::make_unique(other._get_impl()); + } + return *this; } diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_model.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_model.cpp index 1fe96fc70a..8dfc619b16 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_model.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_model.cpp @@ -23,6 +23,7 @@ search_index_model& search_index_model::operator=(search_index_model&&) noexcept search_index_model::search_index_model(search_index_model const& other) : _impl(bsoncxx::make_unique(other._get_impl())) {} +// NOLINTNEXTLINE(cert-oop54-cpp): handled by impl. search_index_model& search_index_model::operator=(search_index_model const& other) { _get_impl() = other._get_impl(); return *this; diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_view.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_view.cpp index f360220ab0..3c3987a320 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_view.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_view.cpp @@ -19,6 +19,7 @@ search_index_view& search_index_view::operator=(search_index_view&&) noexcept = search_index_view::search_index_view(search_index_view const& other) : _impl(bsoncxx::make_unique(other._get_impl())) {} +// NOLINTNEXTLINE(cert-oop54-cpp): handled by impl. search_index_view& search_index_view::operator=(search_index_view const& other) { _get_impl() = other._get_impl(); return *this; From 9df3fff343e2f2a5d3b71655831f3de63c9b5108 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 3 Dec 2025 09:37:07 -0600 Subject: [PATCH 11/39] Address cppcoreguidelines-init-variables warnings --- .clang-tidy | 2 +- src/bsoncxx/lib/bsoncxx/private/version.cpp | 2 +- .../bsoncxx/v_noabi/bsoncxx/builder/core.cpp | 10 +++--- .../lib/bsoncxx/v_noabi/bsoncxx/json.cpp | 9 +++--- .../mongocxx/v_noabi/mongocxx/collection.cpp | 4 +-- .../lib/mongocxx/v_noabi/mongocxx/cursor.cpp | 16 ++++++---- .../mongocxx/events/topology_description.cpp | 5 ++- .../v_noabi/mongocxx/gridfs/bucket.cpp | 18 +++++------ .../lib/mongocxx/v_noabi/mongocxx/uri.cpp | 31 +++++++++---------- 9 files changed, 47 insertions(+), 50 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 34e9ffc6d3..396fd3b838 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -4,7 +4,7 @@ Checks: - cert-* - cppcoreguidelines-* - clang-analyzer-*' -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-init-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-prefer-member-initializer,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-static-cast-downcast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-default-member-init,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-prefer-member-initializer,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-static-cast-downcast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-default-member-init,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h diff --git a/src/bsoncxx/lib/bsoncxx/private/version.cpp b/src/bsoncxx/lib/bsoncxx/private/version.cpp index d96c792c44..522a4f1915 100644 --- a/src/bsoncxx/lib/bsoncxx/private/version.cpp +++ b/src/bsoncxx/lib/bsoncxx/private/version.cpp @@ -30,7 +30,7 @@ std::vector split_version(bsoncxx::stdx::string_view input) { std::vector ret; - std::size_t pos; + std::size_t pos = {}; while ((pos = input.find_first_not_of(digits)) != npos) { auto const str = input.substr(0u, pos); diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp index 59b19e3176..39a82cc79a 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp @@ -91,8 +91,8 @@ class core::impl { throw bsoncxx::v_noabi::exception{error_code::k_cannot_perform_document_operation_on_array}; } - uint32_t buf_len; - uint8_t* buf_ptr = bson_destroy_with_steal(_root.get(), true, &buf_len); + uint32_t buf_len = {}; + auto const buf_ptr = bson_destroy_with_steal(_root.get(), true, &buf_len); bson_init(_root.get()); return bsoncxx::v_noabi::document::value{buf_ptr, buf_len, bson_free_deleter}; @@ -104,8 +104,8 @@ class core::impl { throw bsoncxx::v_noabi::exception{error_code::k_cannot_perform_array_operation_on_document}; } - uint32_t buf_len; - uint8_t* buf_ptr = bson_destroy_with_steal(_root.get(), true, &buf_len); + uint32_t buf_len = {}; + auto const buf_ptr = bson_destroy_with_steal(_root.get(), true, &buf_len); bson_init(_root.get()); return bsoncxx::v_noabi::array::value{buf_ptr, buf_len, bson_free_deleter}; @@ -351,7 +351,7 @@ core& core::append(types::b_binary const& value) { uint8_t* core::append(binary_sub_type sub_type, uint32_t length) { stdx::string_view key = _impl->next_key(); - uint8_t* allocated_bytes; + uint8_t* allocated_bytes = {}; if (!bson_append_binary_uninit( _impl->back(), diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp index 6f0296d506..4f42b18118 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp @@ -48,8 +48,8 @@ std::string to_json_helper(document::view view, decltype(bson_as_legacy_extended throw exception(error_code::k_failed_converting_bson_to_json); } - size_t size; - auto result = converter(&bson, &size); + size_t size = {}; + auto const result = converter(&bson, &size); if (!result) { throw exception(error_code::k_failed_converting_bson_to_json); @@ -101,9 +101,8 @@ document::value from_json(stdx::string_view json) { if (!result) throw exception(error_code::k_json_parse_failure, error.message); - std::uint32_t length; - std::uint8_t* buf = bson_destroy_with_steal(result, true, &length); - + std::uint32_t length = {}; + auto const buf = bson_destroy_with_steal(result, true, &length); return document::value{buf, length, bson_free_deleter}; } diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/collection.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/collection.cpp index 05e71c7270..4f1bd4abd6 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/collection.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/collection.cpp @@ -1228,10 +1228,10 @@ cursor collection::_distinct( throw bsoncxx::v_noabi::exception{bsoncxx::v_noabi::error_code::k_internal_error}; } - bson_t const* error_document; - cursor fake_cursor{libmongoc::cursor_new_from_command_reply_with_opts( _get_impl().client_impl->client_t, fake_reply.inout_ptr(), nullptr)}; + + bson_t const* error_document = {}; if (libmongoc::cursor_error_document(fake_cursor._impl->cursor_t, &error, &error_document)) { if (error_document) { bsoncxx::v_noabi::document::value error_doc{ diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/cursor.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/cursor.cpp index e87ad84d21..ac507f2832 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/cursor.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/cursor.cpp @@ -39,13 +39,16 @@ void cursor::iterator::operator++(int) { } cursor::iterator& cursor::iterator::operator++() { - bson_t const* out; - bson_t const* error_document; - bson_error_t error; + bson_t const* out = {}; if (libmongoc::cursor_next(_cursor->_impl->cursor_t, &out)) { _cursor->_impl->doc = bsoncxx::v_noabi::document::view{bson_get_data(out), out->len}; - } else if (libmongoc::cursor_error_document(_cursor->_impl->cursor_t, &error, &error_document)) { + return *this; + } + + bson_t const* error_document = {}; + bson_error_t error = {}; + if (libmongoc::cursor_error_document(_cursor->_impl->cursor_t, &error, &error_document)) { _cursor->_impl->mark_dead(); if (error_document) { bsoncxx::v_noabi::document::value error_doc{ @@ -54,9 +57,10 @@ cursor::iterator& cursor::iterator::operator++() { } else { throw_exception(error); } - } else { - _cursor->_impl->mark_nothing_left(); } + + _cursor->_impl->mark_nothing_left(); + return *this; } diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/events/topology_description.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/events/topology_description.cpp index d1fdb6ce34..9605cb05b8 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/events/topology_description.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/events/topology_description.cpp @@ -93,9 +93,8 @@ bool topology_description::has_writable_server() const { } topology_description::server_descriptions topology_description::servers() const { - std::vector v; - std::size_t n; - auto sds = libmongoc::topology_description_get_servers(static_cast(_td), &n); + std::size_t n = {}; + auto const sds = libmongoc::topology_description_get_servers(static_cast(_td), &n); return server_descriptions{reinterpret_cast(sds), n}; } diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp index f09342f938..3af28232cd 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp @@ -44,11 +44,9 @@ namespace gridfs { namespace { std::int32_t read_chunk_size_from_files_document(bsoncxx::v_noabi::document::view files_doc) { - std::int64_t const k_max_document_size = 16 * 1024 * 1024; - std::int64_t chunk_size; - - auto chunk_size_ele = files_doc["chunkSize"]; + static constexpr std::int64_t k_max_document_size = {16 * 1024 * 1024}; + auto const chunk_size_ele = files_doc["chunkSize"]; if (!chunk_size_ele) { throw gridfs_exception{ error_code::k_gridfs_file_corrupted, @@ -56,6 +54,7 @@ std::int32_t read_chunk_size_from_files_document(bsoncxx::v_noabi::document::vie "k_int32 or k_int64"}; } + std::int64_t chunk_size = {}; if (chunk_size_ele.type() == bsoncxx::v_noabi::type::k_int64) { chunk_size = chunk_size_ele.get_int64().value; } else if (chunk_size_ele.type() == bsoncxx::v_noabi::type::k_int32) { @@ -83,9 +82,7 @@ std::int32_t read_chunk_size_from_files_document(bsoncxx::v_noabi::document::vie } std::int64_t read_length_from_files_document(bsoncxx::v_noabi::document::view const files_doc) { - auto length_ele = files_doc["length"]; - std::int64_t length; - + auto const length_ele = files_doc["length"]; if (!length_ele) { throw gridfs_exception{ error_code::k_gridfs_file_corrupted, @@ -93,6 +90,7 @@ std::int64_t read_length_from_files_document(bsoncxx::v_noabi::document::view co "k_int32 or k_int64"}; } + std::int64_t length = {}; if (length_ele.type() == bsoncxx::v_noabi::type::k_int64) { length = length_ele.get_int64().value; } else if (length_ele.type() == bsoncxx::v_noabi::type::k_int32) { @@ -371,7 +369,7 @@ downloader bucket::_open_download_stream( } if (end) { - int64_t end_i64; + int64_t end_i64 = {}; if (!size_t_to_int64_safe(*end, end_i64)) { throw gridfs_exception{error_code::k_invalid_parameter, "expected end to not be greater than max int64"}; } @@ -409,7 +407,7 @@ void bucket::_download_to_stream( bsoncxx::v_noabi::stdx::optional end) { downloader download_stream = _open_download_stream(session, id, start, end); - std::size_t chunk_size; + std::size_t chunk_size = {}; if (!int32_to_size_t_safe(download_stream.chunk_size(), chunk_size)) { throw gridfs_exception{error_code::k_invalid_parameter, "expected chunk size to be in bounds of size_t"}; } @@ -417,7 +415,7 @@ void bucket::_download_to_stream( start.emplace(0); } if (!end) { - std::size_t file_length_sz; + std::size_t file_length_sz = {}; if (!int64_to_size_t_safe(download_stream.file_length(), file_length_sz)) { throw gridfs_exception{error_code::k_invalid_parameter, "expected file length to be in bounds of int64"}; } diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp index 416a781a48..f3bf9ae6fd 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp @@ -137,14 +137,10 @@ mongocxx::v_noabi::write_concern uri::write_concern() const { static bsoncxx::v_noabi::stdx::optional _string_option( mongoc_uri_t* uri, std::string opt_name) { - char const* value; - - value = libmongoc::uri_get_option_as_utf8(uri, opt_name.c_str(), nullptr); - if (!value) { - return bsoncxx::v_noabi::stdx::nullopt; + if (auto const value = libmongoc::uri_get_option_as_utf8(uri, opt_name.c_str(), nullptr)) { + return value; } - - return bsoncxx::v_noabi::stdx::string_view{value}; + return {}; } static bsoncxx::v_noabi::stdx::optional _int32_option(mongoc_uri_t* uri, std::string opt_name) { @@ -181,14 +177,15 @@ bsoncxx::v_noabi::stdx::optional uri::srv_max_hosts() const { static bsoncxx::v_noabi::stdx::optional _credential_document_option( mongoc_uri_t* uri, std::string opt_name) { - bson_iter_t iter; - uint8_t const* data; - uint32_t len; - bson_t const* options_bson = libmongoc::uri_get_credentials(uri); + auto const options_bson = libmongoc::uri_get_credentials(uri); + bson_iter_t iter = {}; if (!bson_iter_init_find_case(&iter, options_bson, opt_name.c_str()) || !BSON_ITER_HOLDS_DOCUMENT(&iter)) { return bsoncxx::v_noabi::stdx::nullopt; } + + uint8_t const* data = {}; + uint32_t len = {}; bson_iter_document(&iter, &len, &data); return bsoncxx::v_noabi::document::view(data, len); } @@ -203,17 +200,17 @@ bsoncxx::v_noabi::stdx::optional uri::auth_mec } std::vector uri::compressors() const { - bson_t const* compressors; - std::vector result; - bson_iter_t iter; - - compressors = libmongoc::uri_get_compressors(_impl->uri_t); + auto const compressors = libmongoc::uri_get_compressors(_impl->uri_t); if (!compressors) { // Should not happen. libmongoc will return an empty document even if there were no // compressors present in the URI. - return result; + return {}; } + + bson_iter_t iter = {}; bson_iter_init(&iter, compressors); + + std::vector result; while (bson_iter_next(&iter)) { result.push_back(bsoncxx::v_noabi::stdx::string_view{bson_iter_key(&iter), bson_iter_key_len(&iter)}); } From e72fc6fffb3e8cd0ac22774f38740cf209ee8a56 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 3 Dec 2025 09:37:07 -0600 Subject: [PATCH 12/39] Address cppcoreguidelines-pro-type-static-cast-downcast warning --- .clang-tidy | 2 +- .../lib/mongocxx/v_noabi/mongocxx/options/index.cpp | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 396fd3b838..ff887d8564 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -4,7 +4,7 @@ Checks: - cert-* - cppcoreguidelines-* - clang-analyzer-*' -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-prefer-member-initializer,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-static-cast-downcast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-default-member-init,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-prefer-member-initializer,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-default-member-init,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/index.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/index.cpp index 0b9dec2c3b..0a1f2e7b1b 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/index.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/index.cpp @@ -297,10 +297,8 @@ index::operator bsoncxx::v_noabi::document::view_or_value() { if (_storage_engine) { root.append(kvp("storageEngine", *_storage_engine)); } else if (_storage_options) { - if (_storage_options->type() == MONGOC_INDEX_STORAGE_OPT_WIREDTIGER) { - options::index::wiredtiger_storage_options const* wt_options = - static_cast(_storage_options.get()); - + if (auto const wt_options = + dynamic_cast(_storage_options.get())) { bsoncxx::v_noabi::document::view_or_value storage_doc; if (wt_options->config_string()) { storage_doc = From 14b6d78bfc2679bb3ea377f5d064e36ba148b850 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 3 Dec 2025 09:37:07 -0600 Subject: [PATCH 13/39] Address cppcoreguidelines-prefer-member-initializer warnings --- .clang-tidy | 2 +- src/mongocxx/lib/mongocxx/v_noabi/mongocxx/client.cpp | 4 +--- src/mongocxx/lib/mongocxx/v_noabi/mongocxx/database.cpp | 4 +--- .../mongocxx/v_noabi/mongocxx/result/rewrap_many_datakey.cpp | 5 ++--- 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index ff887d8564..c6e8d87532 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -4,7 +4,7 @@ Checks: - cert-* - cppcoreguidelines-* - clang-analyzer-*' -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-prefer-member-initializer,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-default-member-init,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-default-member-init,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/client.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/client.cpp index 0bbfd3b202..4c6f06ad3c 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/client.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/client.cpp @@ -50,9 +50,7 @@ using bsoncxx::v_noabi::builder::basic::kvp; namespace { class database_names { public: - explicit database_names(char** names) { - _names = names; - } + explicit database_names(char** names) : _names{names} {} ~database_names() { bson_strfreev(_names); diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/database.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/database.cpp index fbd759835b..69ed742a7e 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/database.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/database.cpp @@ -54,9 +54,7 @@ namespace { class collection_names { public: - explicit collection_names(char** names) { - _names = names; - } + explicit collection_names(char** names) : _names{names} {} ~collection_names() { bson_strfreev(_names); diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/result/rewrap_many_datakey.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/result/rewrap_many_datakey.cpp index 44c2c48cc1..d77548e542 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/result/rewrap_many_datakey.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/result/rewrap_many_datakey.cpp @@ -19,9 +19,8 @@ namespace mongocxx { namespace v_noabi { namespace result { -rewrap_many_datakey::rewrap_many_datakey(mongocxx::v_noabi::result::bulk_write bulk_write_result_doc) { - _result = std::move(bulk_write_result_doc); -} +rewrap_many_datakey::rewrap_many_datakey(mongocxx::v_noabi::result::bulk_write bulk_write_result_doc) + : _result{std::move(bulk_write_result_doc)} {} bsoncxx::v_noabi::stdx::optional const& rewrap_many_datakey::result() { return _result; From 429a3de6173aeb57db470dc8f3cf6e7d89ac2078 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 3 Dec 2025 09:37:07 -0600 Subject: [PATCH 14/39] Address cppcoreguidelines-use-default-member-init warnings --- .clang-tidy | 2 +- .../bsoncxx/v_noabi/bsoncxx/builder/core.cpp | 21 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index c6e8d87532..b6b367c924 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -4,7 +4,7 @@ Checks: - cert-* - cppcoreguidelines-* - clang-analyzer-*' -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-default-member-init,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp index 39a82cc79a..7d37fea92d 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp @@ -69,7 +69,7 @@ class managed_bson_t { class core::impl { public: - impl(bool is_array) : _depth(0), _root_is_array(is_array), _n(0), _has_user_key(false) {} + impl(bool is_array) : _root_is_array(is_array) {} void reinit() { while (!_stack.empty()) { @@ -209,8 +209,7 @@ class core::impl { frame(frame const&) = delete; frame& operator=(frame const&) = delete; - frame(bson_t* parent, char const* key, std::int32_t len, bool is_array) - : n(0), is_array(is_array), parent(parent) { + frame(bson_t* parent, char const* key, std::int32_t len, bool is_array) : is_array(is_array), parent(parent) { if (is_array) { if (!bson_append_array_begin(parent, key, len, &bson)) { throw bsoncxx::v_noabi::exception{error_code::k_cannot_begin_appending_array}; @@ -234,16 +233,16 @@ class core::impl { } } - std::size_t n; - bool is_array; - bson_t bson; - bson_t* parent; + std::size_t n = {}; + bool is_array = {}; + bson_t bson = {}; + bson_t* parent = {}; }; - std::size_t _depth; + std::size_t _depth = {}; - bool _root_is_array; - std::size_t _n; + bool _root_is_array = {}; + std::size_t _n = {}; managed_bson_t _root; // The bottom frame of _stack has _root as its parent. @@ -254,7 +253,7 @@ class core::impl { stdx::string_view _user_key_view; std::string _user_key_owned; - bool _has_user_key; + bool _has_user_key = {}; }; core::core(bool is_array) { From fc9014126ad03f2628756ff30b922e878f16d790 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 3 Dec 2025 09:37:07 -0600 Subject: [PATCH 15/39] Address cppcoreguidelines-non-private-member-variables-in-classes warnings --- src/mongocxx/lib/mongocxx/v1/pipeline.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/mongocxx/lib/mongocxx/v1/pipeline.cpp b/src/mongocxx/lib/mongocxx/v1/pipeline.cpp index 5eff59b5ff..340d14cc88 100644 --- a/src/mongocxx/lib/mongocxx/v1/pipeline.cpp +++ b/src/mongocxx/lib/mongocxx/v1/pipeline.cpp @@ -38,11 +38,16 @@ namespace v1 { class pipeline::impl { private: static_assert(INT32_MAX == std::int32_t{2147483647}, ""); - std::array _idx = {}; // Access via `this->idx()`. - public: - scoped_bson _doc; + std::array _idx = {}; + std::int32_t _count = 0; + scoped_bson _doc; + + public: + scoped_bson const& doc() const { + return _doc; + } char const* idx() { (void)std::snprintf(_idx.data(), _idx.size(), "%" PRId32, _count); @@ -122,7 +127,7 @@ pipeline& pipeline::operator=(pipeline const& other) { pipeline::pipeline() : _impl{new impl{}} {} bsoncxx::v1::array::view pipeline::view_array() const { - return impl::with(this)->_doc.array_view(); + return impl::with(this)->doc().array_view(); } pipeline& pipeline::append_stage(bsoncxx::v1::document::view v) { @@ -296,7 +301,7 @@ pipeline& pipeline::unwind(bsoncxx::v1::stdx::string_view v) { } scoped_bson const& pipeline::internal::doc(pipeline const& self) { - return impl::with(self)._doc; + return impl::with(self).doc(); } } // namespace v1 From d1cb01429907902a7ebed5df1b7e7108bef02eea Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 3 Dec 2025 09:37:07 -0600 Subject: [PATCH 16/39] Address misc-throw-by-value-catch-by-reference warnings --- src/mongocxx/lib/mongocxx/v1/exception.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mongocxx/lib/mongocxx/v1/exception.cpp b/src/mongocxx/lib/mongocxx/v1/exception.cpp index a142de5aab..560cfc6d10 100644 --- a/src/mongocxx/lib/mongocxx/v1/exception.cpp +++ b/src/mongocxx/lib/mongocxx/v1/exception.cpp @@ -333,7 +333,7 @@ void throw_exception(bson_error_t const& error, bsoncxx::v1::document::value doc // Client-side error. auto ex = make_exception(error); exception::internal::set_error_labels(ex, doc); - throw ex; + throw std::move(ex); } } // namespace v1 From 83cd0b85ca0a49f4762bc2908e5582dee673138a Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 3 Dec 2025 10:26:09 -0600 Subject: [PATCH 17/39] Encapsulate v1::pipeline::impl::_idx invariants in a separate class --- src/mongocxx/lib/mongocxx/v1/pipeline.cpp | 38 +++++++++++------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/mongocxx/lib/mongocxx/v1/pipeline.cpp b/src/mongocxx/lib/mongocxx/v1/pipeline.cpp index 340d14cc88..d24681f453 100644 --- a/src/mongocxx/lib/mongocxx/v1/pipeline.cpp +++ b/src/mongocxx/lib/mongocxx/v1/pipeline.cpp @@ -37,11 +37,21 @@ namespace v1 { class pipeline::impl { private: - static_assert(INT32_MAX == std::int32_t{2147483647}, ""); + class idx_type { + private: + // Non-negative values only. + static_assert(INT32_MAX == std::int32_t{2147483647}, ""); + std::array _buffer = {}; + std::int32_t _count = {}; + + public: + char const* operator()() { + (void)std::snprintf(_buffer.data(), _buffer.size(), "%" PRId32, _count); + ++_count; + return _buffer.data(); + } + } _idx; - std::array _idx = {}; - - std::int32_t _count = 0; scoped_bson _doc; public: @@ -49,34 +59,24 @@ class pipeline::impl { return _doc; } - char const* idx() { - (void)std::snprintf(_idx.data(), _idx.size(), "%" PRId32, _count); - return _idx.data(); - } - void append(bsoncxx::v1::document::view doc) { - _doc += scoped_bson{BCON_NEW(this->idx(), BCON_DOCUMENT(scoped_bson_view{doc}.bson()))}; - ++_count; + _doc += scoped_bson{BCON_NEW(this->_idx(), BCON_DOCUMENT(scoped_bson_view{doc}.bson()))}; } void append(char const* name, bsoncxx::v1::document::view doc) { - _doc += scoped_bson{BCON_NEW(this->idx(), "{", name, BCON_DOCUMENT(scoped_bson_view{doc}.bson()), "}")}; - ++_count; + _doc += scoped_bson{BCON_NEW(this->_idx(), "{", name, BCON_DOCUMENT(scoped_bson_view{doc}.bson()), "}")}; } void append(char const* name, bsoncxx::v1::stdx::string_view v) { - _doc += scoped_bson{BCON_NEW(this->idx(), "{", name, BCON_UTF8(std::string{v}.c_str()), "}")}; - ++_count; + _doc += scoped_bson{BCON_NEW(this->_idx(), "{", name, BCON_UTF8(std::string{v}.c_str()), "}")}; } void append(char const* name, std::int32_t v) { - _doc += scoped_bson{BCON_NEW(this->idx(), "{", name, BCON_INT32(v), "}")}; - ++_count; + _doc += scoped_bson{BCON_NEW(this->_idx(), "{", name, BCON_INT32(v), "}")}; } void append(char const* name, std::int64_t v) { - _doc += scoped_bson{BCON_NEW(this->idx(), "{", name, BCON_INT64(v), "}")}; - ++_count; + _doc += scoped_bson{BCON_NEW(this->_idx(), "{", name, BCON_INT64(v), "}")}; } static impl const& with(pipeline const& self) { From 615296e3e1da6c9f47ce3bd98649e1441ee7c97e Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 4 Dec 2025 15:50:39 -0600 Subject: [PATCH 18/39] Resolve merge conflict with upstream/master in advance --- .../v_noabi/mongocxx/events/topology_description.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/events/topology_description.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/events/topology_description.cpp index 9605cb05b8..3d05e57d3a 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/events/topology_description.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/events/topology_description.cpp @@ -94,8 +94,9 @@ bool topology_description::has_writable_server() const { topology_description::server_descriptions topology_description::servers() const { std::size_t n = {}; - auto const sds = libmongoc::topology_description_get_servers(static_cast(_td), &n); - return server_descriptions{reinterpret_cast(sds), n}; + auto const sds = + libmongoc::topology_description_get_servers(v1::events::topology_description::internal::as_mongoc(_td), &n); + return {reinterpret_cast(sds), n}; } } // namespace events From e6d518f771c0463a4c3d7323f7feb56f88926b2e Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 4 Dec 2025 15:50:39 -0600 Subject: [PATCH 19/39] Disable cppcoreguidelines-pro-type-union-access --- .clang-tidy | 1 + 1 file changed, 1 insertion(+) diff --git a/.clang-tidy b/.clang-tidy index b6b367c924..4422f7cbd5 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -4,6 +4,7 @@ Checks: - cert-* - cppcoreguidelines-* - clang-analyzer-*' + - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' From 624d0be55df8c28ed18330e547bf282e2223fdec Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 4 Dec 2025 15:50:39 -0600 Subject: [PATCH 20/39] Disable cppcoreguidelines-pro-bounds-array-to-pointer-decay --- .clang-tidy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index 4422f7cbd5..f3cd080952 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -4,8 +4,9 @@ Checks: - cert-* - cppcoreguidelines-* - clang-analyzer-*' + - -cppcoreguidelines-pro-bounds-array-to-pointer-decay # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h From 6f579ccbf3c1a08f3c8e2af97b13812a854a4b5b Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 4 Dec 2025 15:50:39 -0600 Subject: [PATCH 21/39] Disable cppcoreguidelines-pro-bounds-pointer-arithmetic --- .clang-tidy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index f3cd080952..15467d9298 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -5,8 +5,9 @@ Checks: - cppcoreguidelines-* - clang-analyzer-*' - -cppcoreguidelines-pro-bounds-array-to-pointer-decay # Noise. + - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h From e534c746477f3911a7bd6dc2328fc1454d7dd8e2 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 4 Dec 2025 15:50:39 -0600 Subject: [PATCH 22/39] Suppress cppcoreguidelines-pro-bounds-constant-array-index --- .clang-tidy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index 15467d9298..801b8f13a6 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -5,9 +5,10 @@ Checks: - cppcoreguidelines-* - clang-analyzer-*' - -cppcoreguidelines-pro-bounds-array-to-pointer-decay # Noise. + - -cppcoreguidelines-pro-bounds-constant-array-index # Noise. - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h From b136bc5a54bde3f3b8ddf9b1e7924376b61e678a Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 4 Dec 2025 15:50:39 -0600 Subject: [PATCH 23/39] Suppress cppcoreguidelines-owning-memory warnings --- .clang-tidy | 2 +- src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/value.cpp | 2 +- src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/value.cpp | 2 +- src/mongocxx/lib/mongocxx/v1/apm.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/count_options.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/data_key_options.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/delete_many_options.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/delete_one_options.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/distinct_options.cpp | 4 ++++ .../lib/mongocxx/v1/estimated_document_count_options.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/hint.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/insert_many_options.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/insert_one_options.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/pipeline.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/range_options.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/rewrap_many_datakey_options.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/tls.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v_noabi/mongocxx/pool.cpp | 2 +- 18 files changed, 60 insertions(+), 4 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 801b8f13a6..02d0438815 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -8,7 +8,7 @@ Checks: - -cppcoreguidelines-pro-bounds-constant-array-index # Noise. - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/value.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/value.cpp index 96a23d8670..008b550415 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/value.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/value.cpp @@ -34,7 +34,7 @@ static_assert(!is_explicitly_convertible::value, namespace { void uint8_t_deleter(std::uint8_t* ptr) { - delete[] ptr; + delete[] ptr; // NOLINT(cppcoreguidelines-owning-memory): custom deleter. } } // namespace diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/value.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/value.cpp index a9135c5143..547af84dcb 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/value.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/value.cpp @@ -43,7 +43,7 @@ static_assert(!is_implicitly_convertible::val namespace { void uint8_t_deleter(std::uint8_t* ptr) { - delete[] ptr; + delete[] ptr; // NOLINT(cppcoreguidelines-owning-memory): custom deleter. } } // namespace diff --git a/src/mongocxx/lib/mongocxx/v1/apm.cpp b/src/mongocxx/lib/mongocxx/v1/apm.cpp index 09c12b46ad..801b968843 100644 --- a/src/mongocxx/lib/mongocxx/v1/apm.cpp +++ b/src/mongocxx/lib/mongocxx/v1/apm.cpp @@ -76,6 +76,8 @@ class apm::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + apm::~apm() { delete impl::with(this); } @@ -102,6 +104,8 @@ apm& apm::operator=(apm const& other) { apm::apm() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + apm& apm::on_command_started(std::function fn) { impl::with(this)->_command_started = std::move(fn); return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/count_options.cpp b/src/mongocxx/lib/mongocxx/v1/count_options.cpp index 58e5c4064b..f2fde3746e 100644 --- a/src/mongocxx/lib/mongocxx/v1/count_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/count_options.cpp @@ -62,6 +62,8 @@ class count_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + count_options::~count_options() { delete impl::with(_impl); } @@ -88,6 +90,8 @@ count_options& count_options::operator=(count_options const& other) { count_options::count_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + count_options& count_options::collation(bsoncxx::v1::document::value collation) { impl::with(this)->_collation = std::move(collation); return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/data_key_options.cpp b/src/mongocxx/lib/mongocxx/v1/data_key_options.cpp index dbb548bbb4..8590e76b2f 100644 --- a/src/mongocxx/lib/mongocxx/v1/data_key_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/data_key_options.cpp @@ -60,6 +60,8 @@ class data_key_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + data_key_options::~data_key_options() { delete impl::with(this); } @@ -86,6 +88,8 @@ data_key_options& data_key_options::operator=(data_key_options const& other) { data_key_options::data_key_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + data_key_options& data_key_options::master_key(bsoncxx::v1::document::value master_key) { impl::with(this)->_master_key = std::move(master_key); return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/delete_many_options.cpp b/src/mongocxx/lib/mongocxx/v1/delete_many_options.cpp index f95b5635ac..867cb73b52 100644 --- a/src/mongocxx/lib/mongocxx/v1/delete_many_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/delete_many_options.cpp @@ -57,6 +57,8 @@ class delete_many_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + delete_many_options::~delete_many_options() { delete impl::with(this); } @@ -84,6 +86,8 @@ delete_many_options& delete_many_options::operator=(delete_many_options const& o delete_many_options::delete_many_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + delete_many_options& delete_many_options::collation(bsoncxx::v1::document::value collation) { impl::with(this)->_collation = std::move(collation); return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/delete_one_options.cpp b/src/mongocxx/lib/mongocxx/v1/delete_one_options.cpp index 8fce6ff003..a82689acd5 100644 --- a/src/mongocxx/lib/mongocxx/v1/delete_one_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/delete_one_options.cpp @@ -57,6 +57,8 @@ class delete_one_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + delete_one_options::~delete_one_options() { delete impl::with(this); } @@ -83,6 +85,8 @@ delete_one_options& delete_one_options::operator=(delete_one_options const& othe delete_one_options::delete_one_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + delete_one_options& delete_one_options::collation(bsoncxx::v1::document::value collation) { impl::with(this)->_collation = std::move(collation); return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/distinct_options.cpp b/src/mongocxx/lib/mongocxx/v1/distinct_options.cpp index 7c83460d19..2ef956d647 100644 --- a/src/mongocxx/lib/mongocxx/v1/distinct_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/distinct_options.cpp @@ -57,6 +57,8 @@ class distinct_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + distinct_options::~distinct_options() { delete impl::with(this); } @@ -83,6 +85,8 @@ distinct_options& distinct_options::operator=(distinct_options const& other) { distinct_options::distinct_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + distinct_options& distinct_options::collation(bsoncxx::v1::document::value collation) { impl::with(this)->_collation = std::move(collation); return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/estimated_document_count_options.cpp b/src/mongocxx/lib/mongocxx/v1/estimated_document_count_options.cpp index a98737f52d..1aa05c54be 100644 --- a/src/mongocxx/lib/mongocxx/v1/estimated_document_count_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/estimated_document_count_options.cpp @@ -55,6 +55,8 @@ class estimated_document_count_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + estimated_document_count_options::~estimated_document_count_options() { delete impl::with(this); } @@ -85,6 +87,8 @@ estimated_document_count_options& estimated_document_count_options::operator=( estimated_document_count_options::estimated_document_count_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + estimated_document_count_options& estimated_document_count_options::max_time(std::chrono::milliseconds max_time) { impl::with(this)->_max_time = max_time; return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/hint.cpp b/src/mongocxx/lib/mongocxx/v1/hint.cpp index 68b159a194..fb478cf67b 100644 --- a/src/mongocxx/lib/mongocxx/v1/hint.cpp +++ b/src/mongocxx/lib/mongocxx/v1/hint.cpp @@ -54,6 +54,8 @@ class hint::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + hint::~hint() { delete impl::with(_impl); } @@ -80,6 +82,8 @@ hint& hint::operator=(hint const& other) { hint::hint() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + hint::hint(std::string str) : hint{} { impl::with(this)->_str = std::move(str); } diff --git a/src/mongocxx/lib/mongocxx/v1/insert_many_options.cpp b/src/mongocxx/lib/mongocxx/v1/insert_many_options.cpp index c1d23d7359..ea1f2daf6a 100644 --- a/src/mongocxx/lib/mongocxx/v1/insert_many_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/insert_many_options.cpp @@ -54,6 +54,8 @@ class insert_many_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + insert_many_options::~insert_many_options() { delete impl::with(this); } @@ -81,6 +83,8 @@ insert_many_options& insert_many_options::operator=(insert_many_options const& o insert_many_options::insert_many_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + insert_many_options& insert_many_options::bypass_document_validation(bool bypass_document_validation) { impl::with(this)->_bypass_document_validation = bypass_document_validation; return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/insert_one_options.cpp b/src/mongocxx/lib/mongocxx/v1/insert_one_options.cpp index 93c4d47b97..9ed20652e8 100644 --- a/src/mongocxx/lib/mongocxx/v1/insert_one_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/insert_one_options.cpp @@ -53,6 +53,8 @@ class insert_one_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + insert_one_options::~insert_one_options() { delete impl::with(this); } @@ -79,6 +81,8 @@ insert_one_options& insert_one_options::operator=(insert_one_options const& othe insert_one_options::insert_one_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + insert_one_options& insert_one_options::bypass_document_validation(bool bypass_document_validation) { impl::with(this)->_bypass_document_validation = bypass_document_validation; return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/pipeline.cpp b/src/mongocxx/lib/mongocxx/v1/pipeline.cpp index d24681f453..f84c1df2f7 100644 --- a/src/mongocxx/lib/mongocxx/v1/pipeline.cpp +++ b/src/mongocxx/lib/mongocxx/v1/pipeline.cpp @@ -100,6 +100,8 @@ class pipeline::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + pipeline::~pipeline() { delete impl::with(this); } @@ -126,6 +128,8 @@ pipeline& pipeline::operator=(pipeline const& other) { pipeline::pipeline() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + bsoncxx::v1::array::view pipeline::view_array() const { return impl::with(this)->doc().array_view(); } diff --git a/src/mongocxx/lib/mongocxx/v1/range_options.cpp b/src/mongocxx/lib/mongocxx/v1/range_options.cpp index 118ae0fe08..9ba5aedfea 100644 --- a/src/mongocxx/lib/mongocxx/v1/range_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/range_options.cpp @@ -55,6 +55,8 @@ class range_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + range_options::~range_options() { delete impl::with(this); } @@ -81,6 +83,8 @@ range_options& range_options::operator=(range_options const& other) { range_options::range_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + range_options& range_options::min(bsoncxx::v1::types::value value) { impl::with(this)->_min = std::move(value); return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/rewrap_many_datakey_options.cpp b/src/mongocxx/lib/mongocxx/v1/rewrap_many_datakey_options.cpp index ec3a6ce4a8..0b56476d8e 100644 --- a/src/mongocxx/lib/mongocxx/v1/rewrap_many_datakey_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/rewrap_many_datakey_options.cpp @@ -53,6 +53,8 @@ class rewrap_many_datakey_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + rewrap_many_datakey_options::~rewrap_many_datakey_options() { delete impl::with(this); } @@ -81,6 +83,8 @@ rewrap_many_datakey_options& rewrap_many_datakey_options::operator=(rewrap_many_ rewrap_many_datakey_options::rewrap_many_datakey_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + rewrap_many_datakey_options& rewrap_many_datakey_options::provider(std::string provider) { impl::with(this)->_provider = std::move(provider); return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/tls.cpp b/src/mongocxx/lib/mongocxx/v1/tls.cpp index 8fd0393c18..fb47015988 100644 --- a/src/mongocxx/lib/mongocxx/v1/tls.cpp +++ b/src/mongocxx/lib/mongocxx/v1/tls.cpp @@ -56,6 +56,8 @@ class tls::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + tls::~tls() { delete impl::with(this); } @@ -80,6 +82,8 @@ tls& tls::operator=(tls const& other) { tls::tls() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + tls& tls::pem_file(std::string v) { impl::with(this)->_pem_file = std::move(v); return *this; diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/pool.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/pool.cpp index 8359ec9016..ee5f406f52 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/pool.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/pool.cpp @@ -51,7 +51,7 @@ void pool::_release(client* client) { libmongoc::client_pool_push(_impl->client_pool_t, client->_get_impl().client_t); // prevent client destructor from destroying the underlying mongoc_client_t client->_get_impl().client_t = nullptr; - delete client; + delete client; // NOLINT(cppcoreguidelines-owning-memory): custom deleter. } pool::~pool() = default; From ea16657e79015a0844f8a8c7bd8f94ea66b3c28c Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 4 Dec 2025 15:50:39 -0600 Subject: [PATCH 24/39] Suppress cppcoreguidelines-pro-type-reinterpret-cast --- .clang-tidy | 2 +- src/bsoncxx/lib/bsoncxx/v1/element/view.cpp | 3 +++ src/bsoncxx/lib/bsoncxx/v1/types/value.cpp | 3 +++ src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp | 6 +++++- src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/oid.cpp | 1 + src/mongocxx/lib/mongocxx/v1/events/command_failed.cpp | 2 +- src/mongocxx/lib/mongocxx/v1/events/command_started.cpp | 2 +- src/mongocxx/lib/mongocxx/v1/events/command_succeeded.cpp | 2 +- src/mongocxx/lib/mongocxx/v1/events/server_closed.cpp | 2 +- .../lib/mongocxx/v1/events/server_description_changed.cpp | 2 +- src/mongocxx/lib/mongocxx/v1/events/server_opening.cpp | 2 +- src/mongocxx/lib/mongocxx/v1/events/topology_closed.cpp | 2 +- .../lib/mongocxx/v1/events/topology_description_changed.cpp | 2 +- src/mongocxx/lib/mongocxx/v1/events/topology_opening.cpp | 2 +- .../v_noabi/mongocxx/events/topology_description.cpp | 2 +- .../lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp | 4 +++- .../lib/mongocxx/v_noabi/mongocxx/gridfs/uploader.cpp | 6 +++--- src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp | 2 ++ 18 files changed, 31 insertions(+), 16 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 02d0438815..5416bb558d 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -8,7 +8,7 @@ Checks: - -cppcoreguidelines-pro-bounds-constant-array-index # Noise. - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h diff --git a/src/bsoncxx/lib/bsoncxx/v1/element/view.cpp b/src/bsoncxx/lib/bsoncxx/v1/element/view.cpp index f596c7f3b4..7d135ea753 100644 --- a/src/bsoncxx/lib/bsoncxx/v1/element/view.cpp +++ b/src/bsoncxx/lib/bsoncxx/v1/element/view.cpp @@ -156,6 +156,8 @@ class alignas(BSONCXX_PRIVATE_MAX_ALIGN_T) view::impl { throw v1::exception{code::invalid_data}; } + // Helpers to access the inline PIMPL object. + // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) static impl const& with(view const& v) { return *reinterpret_cast(v._storage.data()); } @@ -167,6 +169,7 @@ class alignas(BSONCXX_PRIVATE_MAX_ALIGN_T) view::impl { static impl* with(view* v) { return reinterpret_cast(v->_storage.data()); } + // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) }; void view::impl::check() const { diff --git a/src/bsoncxx/lib/bsoncxx/v1/types/value.cpp b/src/bsoncxx/lib/bsoncxx/v1/types/value.cpp index 16754a6eb4..0ca6de85d1 100644 --- a/src/bsoncxx/lib/bsoncxx/v1/types/value.cpp +++ b/src/bsoncxx/lib/bsoncxx/v1/types/value.cpp @@ -111,6 +111,8 @@ class alignas(BSONCXX_PRIVATE_MAX_ALIGN_T) value::impl { return _value.value; } + // Helpers to access the inline PIMPL object. + // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) static impl const& with(value const& self) { return *reinterpret_cast(self._storage.data()); } @@ -126,6 +128,7 @@ class alignas(BSONCXX_PRIVATE_MAX_ALIGN_T) value::impl { static impl* with(value* self) { return reinterpret_cast(self->_storage.data()); } + // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) }; value::impl::~impl() { diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp index 4f42b18118..f5f8dbf777 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp @@ -95,8 +95,12 @@ std::string to_json(array::view view, ExtendedJsonMode mode) { document::value from_json(stdx::string_view json) { bson_error_t error; + bson_t* result = bson_new_from_json( - reinterpret_cast(json.data()), static_cast(json.size()), &error); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast): bson vs. bsoncxx compatibility. + reinterpret_cast(json.data()), + static_cast(json.size()), + &error); if (!result) throw exception(error_code::k_json_parse_failure, error.message); diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/oid.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/oid.cpp index 0230740186..d740c6112f 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/oid.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/oid.cpp @@ -54,6 +54,7 @@ oid::oid(stdx::string_view const& str) try : _oid{str} { throw v_noabi::exception{v_noabi::error_code::k_invalid_oid}; } +// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast): v1 vs. v_noabi compatibility. oid::oid(char const* bytes, std::size_t len) try : _oid{reinterpret_cast(bytes), len} { } catch (v1::exception const&) { throw v_noabi::exception{v_noabi::error_code::k_invalid_oid}; diff --git a/src/mongocxx/lib/mongocxx/v1/events/command_failed.cpp b/src/mongocxx/lib/mongocxx/v1/events/command_failed.cpp index bd0d2b6c32..ab51e64f8a 100644 --- a/src/mongocxx/lib/mongocxx/v1/events/command_failed.cpp +++ b/src/mongocxx/lib/mongocxx/v1/events/command_failed.cpp @@ -62,7 +62,7 @@ bsoncxx::v1::stdx::optional command_failed::service_id() const bsoncxx::v1::stdx::optional ret; if (auto const id = libmongoc::apm_command_failed_get_service_id(to_mongoc(_impl))) { - ret.emplace(reinterpret_cast(id), bsoncxx::v1::oid::k_oid_length); + ret.emplace(id->bytes, bsoncxx::v1::oid::k_oid_length); } return ret; diff --git a/src/mongocxx/lib/mongocxx/v1/events/command_started.cpp b/src/mongocxx/lib/mongocxx/v1/events/command_started.cpp index 5e711cdadd..9727e88702 100644 --- a/src/mongocxx/lib/mongocxx/v1/events/command_started.cpp +++ b/src/mongocxx/lib/mongocxx/v1/events/command_started.cpp @@ -62,7 +62,7 @@ bsoncxx::v1::stdx::optional command_started::service_id() cons bsoncxx::v1::stdx::optional ret; if (auto const id = libmongoc::apm_command_started_get_service_id(to_mongoc(_impl))) { - ret.emplace(reinterpret_cast(id), bsoncxx::v1::oid::k_oid_length); + ret.emplace(id->bytes, bsoncxx::v1::oid::k_oid_length); } return ret; diff --git a/src/mongocxx/lib/mongocxx/v1/events/command_succeeded.cpp b/src/mongocxx/lib/mongocxx/v1/events/command_succeeded.cpp index b25cac5ae1..fe54fe6abe 100644 --- a/src/mongocxx/lib/mongocxx/v1/events/command_succeeded.cpp +++ b/src/mongocxx/lib/mongocxx/v1/events/command_succeeded.cpp @@ -62,7 +62,7 @@ bsoncxx::v1::stdx::optional command_succeeded::service_id() co bsoncxx::v1::stdx::optional ret; if (auto const id = libmongoc::apm_command_succeeded_get_service_id(to_mongoc(_impl))) { - ret.emplace(reinterpret_cast(id), bsoncxx::v1::oid::k_oid_length); + ret.emplace(id->bytes, bsoncxx::v1::oid::k_oid_length); } return ret; diff --git a/src/mongocxx/lib/mongocxx/v1/events/server_closed.cpp b/src/mongocxx/lib/mongocxx/v1/events/server_closed.cpp index 185d80fa2b..8d0a0f5d22 100644 --- a/src/mongocxx/lib/mongocxx/v1/events/server_closed.cpp +++ b/src/mongocxx/lib/mongocxx/v1/events/server_closed.cpp @@ -48,7 +48,7 @@ std::uint16_t server_closed::port() const { bsoncxx::v1::oid server_closed::topology_id() const { bson_oid_t id = {}; libmongoc::apm_server_closed_get_topology_id(to_mongoc(_impl), &id); - return bsoncxx::v1::oid{reinterpret_cast(&id), sizeof(id)}; + return bsoncxx::v1::oid{id.bytes, sizeof(id)}; } server_closed::server_closed(void const* impl) : _impl{impl} {} diff --git a/src/mongocxx/lib/mongocxx/v1/events/server_description_changed.cpp b/src/mongocxx/lib/mongocxx/v1/events/server_description_changed.cpp index 209df289fb..bc216e5700 100644 --- a/src/mongocxx/lib/mongocxx/v1/events/server_description_changed.cpp +++ b/src/mongocxx/lib/mongocxx/v1/events/server_description_changed.cpp @@ -50,7 +50,7 @@ std::uint16_t server_description_changed::port() const { bsoncxx::v1::oid server_description_changed::topology_id() const { bson_oid_t id = {}; libmongoc::apm_server_changed_get_topology_id(to_mongoc(_impl), &id); - return bsoncxx::v1::oid{reinterpret_cast(&id), sizeof(id)}; + return bsoncxx::v1::oid{id.bytes, sizeof(id)}; } v1::events::server_description server_description_changed::previous_description() const { diff --git a/src/mongocxx/lib/mongocxx/v1/events/server_opening.cpp b/src/mongocxx/lib/mongocxx/v1/events/server_opening.cpp index b1685289b1..a5b4f95048 100644 --- a/src/mongocxx/lib/mongocxx/v1/events/server_opening.cpp +++ b/src/mongocxx/lib/mongocxx/v1/events/server_opening.cpp @@ -48,7 +48,7 @@ std::uint16_t server_opening::port() const { bsoncxx::v1::oid server_opening::topology_id() const { bson_oid_t id = {}; libmongoc::apm_server_opening_get_topology_id(to_mongoc(_impl), &id); - return bsoncxx::v1::oid{reinterpret_cast(&id), sizeof(id)}; + return bsoncxx::v1::oid{id.bytes, sizeof(id)}; } server_opening::server_opening(void const* impl) : _impl{impl} {} diff --git a/src/mongocxx/lib/mongocxx/v1/events/topology_closed.cpp b/src/mongocxx/lib/mongocxx/v1/events/topology_closed.cpp index 6eb1d75a74..eed809fe77 100644 --- a/src/mongocxx/lib/mongocxx/v1/events/topology_closed.cpp +++ b/src/mongocxx/lib/mongocxx/v1/events/topology_closed.cpp @@ -39,7 +39,7 @@ mongoc_apm_topology_closed_t const* to_mongoc(void const* ptr) { bsoncxx::v1::oid topology_closed::topology_id() const { bson_oid_t id = {}; libmongoc::apm_topology_closed_get_topology_id(to_mongoc(_impl), &id); - return bsoncxx::v1::oid{reinterpret_cast(&id), sizeof(id)}; + return bsoncxx::v1::oid{id.bytes, sizeof(id)}; } topology_closed::topology_closed(void const* impl) : _impl{impl} {} diff --git a/src/mongocxx/lib/mongocxx/v1/events/topology_description_changed.cpp b/src/mongocxx/lib/mongocxx/v1/events/topology_description_changed.cpp index 93b6fef324..e7c9991d26 100644 --- a/src/mongocxx/lib/mongocxx/v1/events/topology_description_changed.cpp +++ b/src/mongocxx/lib/mongocxx/v1/events/topology_description_changed.cpp @@ -41,7 +41,7 @@ mongoc_apm_topology_changed_t const* to_mongoc(void const* ptr) { bsoncxx::v1::oid topology_description_changed::topology_id() const { bson_oid_t id = {}; libmongoc::apm_topology_changed_get_topology_id(to_mongoc(_impl), &id); - return bsoncxx::v1::oid{reinterpret_cast(&id), sizeof(id)}; + return bsoncxx::v1::oid{id.bytes, sizeof(id)}; } v1::events::topology_description topology_description_changed::previous_description() const { diff --git a/src/mongocxx/lib/mongocxx/v1/events/topology_opening.cpp b/src/mongocxx/lib/mongocxx/v1/events/topology_opening.cpp index 5a1790ce8d..0891ae53d0 100644 --- a/src/mongocxx/lib/mongocxx/v1/events/topology_opening.cpp +++ b/src/mongocxx/lib/mongocxx/v1/events/topology_opening.cpp @@ -39,7 +39,7 @@ mongoc_apm_topology_opening_t const* to_mongoc(void const* ptr) { bsoncxx::v1::oid topology_opening::topology_id() const { bson_oid_t id = {}; libmongoc::apm_topology_opening_get_topology_id(to_mongoc(_impl), &id); - return bsoncxx::v1::oid{reinterpret_cast(&id), sizeof(id)}; + return bsoncxx::v1::oid{id.bytes, sizeof(id)}; } topology_opening::topology_opening(void const* impl) : _impl{impl} {} diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/events/topology_description.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/events/topology_description.cpp index 4b24d25434..39f7488647 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/events/topology_description.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/events/topology_description.cpp @@ -75,7 +75,7 @@ topology_description::server_descriptions topology_description::servers() const std::size_t n = {}; auto const sds = libmongoc::topology_description_get_servers(v1::events::topology_description::internal::as_mongoc(_td), &n); - return {reinterpret_cast(sds), n}; + return {static_cast(sds), n}; } } // namespace events diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp index 3af28232cd..35dc993ea1 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp @@ -123,7 +123,7 @@ bucket::bucket(database const& db, options::gridfs::bucket const& options) { throw logic_error{error_code::k_invalid_parameter, "non-empty bucket name required"}; } - std::int32_t default_chunk_size_bytes = 255 * 1024; + std::int32_t default_chunk_size_bytes = 255 * 1024; // NOLINT(cppcoreguidelines-avoid-magic-numbers): 255 KiB. if (auto chunk_size_bytes = options.chunk_size_bytes()) { default_chunk_size_bytes = *chunk_size_bytes; } @@ -265,6 +265,7 @@ void bucket::_upload_from_stream_with_id( std::unique_ptr buffer = bsoncxx::make_unique(static_cast(chunk_size)); do { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast): stdlib vs. mongocxx compatibility. source->read(reinterpret_cast(buffer.get()), static_cast(chunk_size)); upload_stream.write(buffer.get(), static_cast(source->gcount())); } while (*source); @@ -427,6 +428,7 @@ void bucket::_download_to_stream( while (bytes_expected > 0) { std::size_t const bytes_read = download_stream.read(buffer.get(), static_cast(std::min(bytes_expected, chunk_size))); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast): stdlib vs. mongocxx compatibility. destination->write(reinterpret_cast(buffer.get()), static_cast(bytes_read)); bytes_expected -= bytes_read; } diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/uploader.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/uploader.cpp index 0f97933fdd..be9a99c0c4 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/uploader.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/uploader.cpp @@ -32,9 +32,9 @@ namespace { std::size_t chunks_collection_documents_max_length(std::size_t chunk_size) { - // 16 * 1000 * 1000 is used instead of 16 * 1024 * 1024 to ensure that the command document sent - // to the server has space for the other fields. - return 16 * 1000 * 1000 / chunk_size; + // 16 * 1000 * 1000 (16 MB) is used instead of 16 * 1024 * 1024 (16 MiB) to ensure that the command document sent to + // the server has space for the other fields. NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) + return 16u * 1000u * 1000u / chunk_size; } } // namespace diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp index 00eec93c24..e9fd1e3d6f 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -43,6 +44,7 @@ std::atomic current_instance{nullptr}; // Sentinel value denoting the current instance has been destroyed. instance* sentinel() { alignas(instance) static unsigned char value[sizeof(instance)]; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast): identity only. return reinterpret_cast(value); } From 5dd83af16ec41bfbfcfb0820615c7cb338eeeeed Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 4 Dec 2025 15:50:39 -0600 Subject: [PATCH 25/39] Address cppcoreguidelines-avoid-magic-numbers warnings --- .clang-tidy | 2 +- src/bsoncxx/lib/bsoncxx/private/itoa.cpp | 15 ++++++++-- src/bsoncxx/lib/bsoncxx/private/itoa.hh | 6 +++- src/bsoncxx/lib/bsoncxx/v1/oid.cpp | 2 +- .../lib/bsoncxx/v_noabi/bsoncxx/vector.cpp | 6 ++-- src/mongocxx/lib/mongocxx/v1/exception.cpp | 28 +++++++++++-------- src/mongocxx/lib/mongocxx/v1/pipeline.cpp | 3 +- 7 files changed, 41 insertions(+), 21 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 5416bb558d..b8c72fbb5a 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -8,7 +8,7 @@ Checks: - -cppcoreguidelines-pro-bounds-constant-array-index # Noise. - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h diff --git a/src/bsoncxx/lib/bsoncxx/private/itoa.cpp b/src/bsoncxx/lib/bsoncxx/private/itoa.cpp index fa6bc04d11..2fd2471553 100644 --- a/src/bsoncxx/lib/bsoncxx/private/itoa.cpp +++ b/src/bsoncxx/lib/bsoncxx/private/itoa.cpp @@ -1032,17 +1032,25 @@ itoa& itoa::operator=(std::uint32_t new_val) { return *this; } +// Index the precomputed table by powers of 10 with additional offsets to account for intermediate null terminators. +// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers) void itoa::_init() { if (_val < 10u) { + // Index by (single digit + null terminator). _str = k_index_table + (2u * _val); _len = 1u; } else if (_val < 100u) { - _str = k_index_table + (2u * 10u) + (3u * (_val - 10u)); + // Index by (double digit + null terminator) and skip lower entries. + _str = k_index_table + (3u * (_val - 10u)) + (2u * 10u); _len = 2u; } else if (_val < 1000u) { - _str = k_index_table + (2u * 10u) + (3u * 90u) + (4 * (_val - 100u)); + // Index by (triple digit + null terminator) and skip lower entries. + _str = k_index_table + (4u * (_val - 100u)) + (2u * 10u) + (3u * 90u); _len = 3u; - } else { + } + + // Fallback to traditional algorithm. + else { int size = static_cast(sizeof(_buf) - 1u); int i = size; @@ -1058,5 +1066,6 @@ void itoa::_init() { _len = static_cast(size - i); } } +// NOLINTEND(cppcoreguidelines-avoid-magic-numbers) } // namespace bsoncxx diff --git a/src/bsoncxx/lib/bsoncxx/private/itoa.hh b/src/bsoncxx/lib/bsoncxx/private/itoa.hh index 1a6ceeeb65..acbda2f099 100644 --- a/src/bsoncxx/lib/bsoncxx/private/itoa.hh +++ b/src/bsoncxx/lib/bsoncxx/private/itoa.hh @@ -23,10 +23,14 @@ namespace bsoncxx { class itoa { private: + // Exact value of UINT32_MAX for `sizeof()` below. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers). + static_assert(UINT32_MAX == std::uint32_t{4294967295}, ""); + std::uint32_t _val; char const* _str; std::uint8_t _len; - char _buf[11]; + char _buf[sizeof("4294967295")]; public: ~itoa() = default; diff --git a/src/bsoncxx/lib/bsoncxx/v1/oid.cpp b/src/bsoncxx/lib/bsoncxx/v1/oid.cpp index 20fcf67ce5..94f3dd29a6 100644 --- a/src/bsoncxx/lib/bsoncxx/v1/oid.cpp +++ b/src/bsoncxx/lib/bsoncxx/v1/oid.cpp @@ -95,7 +95,7 @@ oid::oid(v1::stdx::string_view str) { std::string oid::to_string() const { bson_oid_t oid; std::memcpy(oid.bytes, _bytes.data(), sizeof(oid.bytes)); - char str[25]; + char str[2u * k_oid_length + 1u]; // Two hex digits per byte + null terminator: 25 characters. bson_oid_to_string(&oid, str); return std::string(str); } diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/vector.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/vector.cpp index 4f39c448b9..931d0da9c1 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/vector.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/vector.cpp @@ -94,13 +94,13 @@ void f_float32::write_frame(std::uint8_t* binary_data, std::uint32_t, std::size_ } void f_packed_bit::write_frame(std::uint8_t* binary_data, std::uint32_t binary_data_length, std::size_t element_count) { + static constexpr std::uint8_t mask = {0x7u}; + binary_data[binary_data_length - 1] = UINT8_C(0); detail::write_header( binary_data, detail::make_header( - detail::element_type::unsigned_integer, - detail::element_size::bits_1, - std::uint8_t(std::size_t{7u} & -element_count))); + detail::element_type::unsigned_integer, detail::element_size::bits_1, std::uint8_t(mask & -element_count))); } void formats::f_int8::validate(types::b_binary const& binary) { diff --git a/src/mongocxx/lib/mongocxx/v1/exception.cpp b/src/mongocxx/lib/mongocxx/v1/exception.cpp index 560cfc6d10..f4fd9316fe 100644 --- a/src/mongocxx/lib/mongocxx/v1/exception.cpp +++ b/src/mongocxx/lib/mongocxx/v1/exception.cpp @@ -219,11 +219,19 @@ v1::exception make_exception(bson_error_t const& error) { auto const message = static_cast(error.message); auto const has_message = message[0] != '\0'; + // Undocumented: see mongoc-error-private.h. + enum : int { + MONGOC_ERROR_CATEGORY_BSON = 1, // BSON_ERROR_CATEGORY + MONGOC_ERROR_CATEGORY = 2, + MONGOC_ERROR_CATEGORY_SERVER = 3, + MONGOC_ERROR_CATEGORY_CRYPT = 4, + MONGOC_ERROR_CATEGORY_SASL = 5, + }; + // Undocumented: see mongoc-error-private.h. switch (raw_category) { - // MONGOC_ERROR_CATEGORY_BSON / BSON_ERROR_CATEGORY // Unlikely. Convert to MONGOC_ERROR_BSON_INVALID (18). - case 1: { + case MONGOC_ERROR_CATEGORY_BSON: { std::string what; what += "bson error code "; what += std::to_string(code); @@ -234,8 +242,8 @@ v1::exception make_exception(bson_error_t const& error) { return v1::exception::internal::make(MONGOC_ERROR_BSON_INVALID, mongoc_error_category(), what.c_str()); } - // MONGOC_ERROR_CATEGORY - case 2: { + // Throw as a mongoc error code. + case MONGOC_ERROR_CATEGORY: { if (has_message) { return v1::exception::internal::make(code, mongoc_error_category(), message); } else { @@ -243,9 +251,8 @@ v1::exception make_exception(bson_error_t const& error) { } } - // MONGOC_ERROR_CATEGORY_SERVER // Unlikely. Throw as `v1::exception` but use the correct error category. - case 3: { + case MONGOC_ERROR_CATEGORY_SERVER: { if (has_message) { return v1::exception::internal::make(code, v1::server_error::internal::category(), message); } else { @@ -253,8 +260,8 @@ v1::exception make_exception(bson_error_t const& error) { } } - // MONGOC_ERROR_CATEGORY_CRYPT - case 4: { + // Throw as a libmongocrypt error code. + case MONGOC_ERROR_CATEGORY_CRYPT: { if (has_message) { return v1::exception::internal::make(code, mongocrypt_error_category(), message); } else { @@ -262,9 +269,8 @@ v1::exception make_exception(bson_error_t const& error) { } } - // MONGOC_ERROR_CATEGORY_SASL // Unlikely. Convert to MONGOC_ERROR_CLIENT_AUTHENTICATE (11). - case 5: { + case MONGOC_ERROR_CATEGORY_SASL: { std::string what; what += "sasl error code "; what += std::to_string(code); @@ -276,7 +282,7 @@ v1::exception make_exception(bson_error_t const& error) { MONGOC_ERROR_CLIENT_AUTHENTICATE, mongoc_error_category(), what.c_str()); } - // Unlikely. + // Unlikely. Throw as an unknown error code. default: { std::string what; what += "unknown error category "; diff --git a/src/mongocxx/lib/mongocxx/v1/pipeline.cpp b/src/mongocxx/lib/mongocxx/v1/pipeline.cpp index f84c1df2f7..ba6383684f 100644 --- a/src/mongocxx/lib/mongocxx/v1/pipeline.cpp +++ b/src/mongocxx/lib/mongocxx/v1/pipeline.cpp @@ -39,7 +39,8 @@ class pipeline::impl { private: class idx_type { private: - // Non-negative values only. + // Exact value of INT32_MAX for `sizeof()` below. Non-negative values only. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) static_assert(INT32_MAX == std::int32_t{2147483647}, ""); std::array _buffer = {}; std::int32_t _count = {}; From 6d037beb3ccd2b78764bc7dedca38ea219b91d90 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 4 Dec 2025 15:50:39 -0600 Subject: [PATCH 26/39] Address cppcoreguidelines-pro-type-const-cast warnings --- .clang-tidy | 2 +- .../include/mongocxx/v_noabi/mongocxx/client.hpp | 3 +++ .../mongocxx/v_noabi/mongocxx/client_session.hpp | 15 +++++++++++---- .../mongocxx/v_noabi/mongocxx/collection.hpp | 3 +++ .../mongocxx/v_noabi/mongocxx/database.hpp | 3 +++ .../mongocxx/v_noabi/mongocxx/gridfs/bucket.hpp | 3 +++ .../v_noabi/mongocxx/gridfs/downloader.hpp | 3 +++ .../v_noabi/mongocxx/gridfs/uploader.hpp | 3 +++ .../v_noabi/mongocxx/options/transaction.hpp | 4 ++++ .../v_noabi/mongocxx/search_index_model.hpp | 4 +++- .../v_noabi/mongocxx/search_index_view.hpp | 4 +++- .../lib/mongocxx/v1/data_key_options.cpp | 5 ++++- .../lib/mongocxx/v_noabi/mongocxx/client.cpp | 14 +++++++++----- .../mongocxx/v_noabi/mongocxx/client_session.cpp | 16 ++++------------ .../lib/mongocxx/v_noabi/mongocxx/collection.cpp | 14 +++++++++----- .../lib/mongocxx/v_noabi/mongocxx/database.cpp | 14 +++++++++----- .../mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp | 14 +++++++++----- .../v_noabi/mongocxx/gridfs/downloader.cpp | 14 +++++++++----- .../v_noabi/mongocxx/gridfs/uploader.cpp | 14 +++++++++----- .../v_noabi/mongocxx/options/transaction.cpp | 14 +++++++++----- .../v_noabi/mongocxx/search_index_model.cpp | 14 +++++++++----- .../v_noabi/mongocxx/search_index_view.cpp | 14 +++++++++----- 22 files changed, 129 insertions(+), 65 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index b8c72fbb5a..bda41a8938 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -8,7 +8,7 @@ Checks: - -cppcoreguidelines-pro-bounds-constant-array-index # Noise. - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h diff --git a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/client.hpp b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/client.hpp index aa8a1129ed..75e48090fb 100644 --- a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/client.hpp +++ b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/client.hpp @@ -468,6 +468,9 @@ class client { class impl; + template + static auto _get_impl(Self& self) -> decltype(*self._impl); + impl& _get_impl(); impl const& _get_impl() const; diff --git a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/client_session.hpp b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/client_session.hpp index ffae8d8ee5..0ad65e91fa 100644 --- a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/client_session.hpp +++ b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/client_session.hpp @@ -60,12 +60,18 @@ class client_session { /// /// Move constructs a session. /// - MONGOCXX_ABI_EXPORT_CDECL() client_session(client_session&&) noexcept; + /// @par Postconditions: + /// - `other` is in an assign-or-destroy-only state. + /// + MONGOCXX_ABI_EXPORT_CDECL() client_session(client_session&& other) noexcept; /// /// Move assigns a session. /// - MONGOCXX_ABI_EXPORT_CDECL(client_session&) operator=(client_session&&) noexcept; + /// @par Postconditions: + /// - `other` is in an assign-or-destroy-only state. + /// + MONGOCXX_ABI_EXPORT_CDECL(client_session&) operator=(client_session&& other) noexcept; client_session(client_session const&) = delete; client_session& operator=(client_session const&) = delete; @@ -223,8 +229,9 @@ class client_session { client_session(mongocxx::v_noabi::client const* client, options::client_session const& options); - impl& _get_impl(); - impl const& _get_impl() const; + impl const& _get_impl() const { + return *_impl; + } std::unique_ptr _impl; }; diff --git a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/collection.hpp b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/collection.hpp index 8b14128498..665d7ae146 100644 --- a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/collection.hpp +++ b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/collection.hpp @@ -2009,6 +2009,9 @@ class collection { class impl; + template + static auto _get_impl(Self& self) -> decltype(*self._impl); + impl& _get_impl(); impl const& _get_impl() const; diff --git a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/database.hpp b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/database.hpp index 4481ae6e51..e4156119f2 100644 --- a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/database.hpp +++ b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/database.hpp @@ -565,6 +565,9 @@ class database { class impl; + template + static auto _get_impl(Self& self) -> decltype(*self._impl); + impl& _get_impl(); impl const& _get_impl() const; diff --git a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/gridfs/bucket.hpp b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/gridfs/bucket.hpp index cc16ab9733..ec4a62c27e 100644 --- a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/gridfs/bucket.hpp +++ b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/gridfs/bucket.hpp @@ -684,6 +684,9 @@ class bucket { class impl; + template + static auto _get_impl(Self& self) -> decltype(*self._impl); + impl& _get_impl(); impl const& _get_impl() const; diff --git a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/gridfs/downloader.hpp b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/gridfs/downloader.hpp index 7df9f0c2e9..0f029ed835 100644 --- a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/gridfs/downloader.hpp +++ b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/gridfs/downloader.hpp @@ -165,6 +165,9 @@ class downloader { class impl; + template + static auto _get_impl(Self& self) -> decltype(*self._impl); + impl& _get_impl(); impl const& _get_impl() const; diff --git a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/gridfs/uploader.hpp b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/gridfs/uploader.hpp index 1b94753d4e..128d632eff 100644 --- a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/gridfs/uploader.hpp +++ b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/gridfs/uploader.hpp @@ -163,6 +163,9 @@ class uploader { class impl; + template + static auto _get_impl(Self& self) -> decltype(*self._impl); + impl& _get_impl(); impl const& _get_impl() const; diff --git a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/transaction.hpp b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/transaction.hpp index 8c6be8e82d..9e1cd87307 100644 --- a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/transaction.hpp +++ b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/transaction.hpp @@ -156,8 +156,12 @@ class transaction { class impl; + template + static auto _get_impl(Self& self) -> decltype(*self._impl); + impl& _get_impl(); impl const& _get_impl() const; + std::unique_ptr _impl; }; diff --git a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/search_index_model.hpp b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/search_index_model.hpp index 07552fc799..3d019bd566 100644 --- a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/search_index_model.hpp +++ b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/search_index_model.hpp @@ -100,8 +100,10 @@ class search_index_model { private: class impl; - impl const& _get_impl() const; + template + static auto _get_impl(Self& self) -> decltype(*self._impl); + impl const& _get_impl() const; impl& _get_impl(); private: diff --git a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/search_index_view.hpp b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/search_index_view.hpp index 1759664cd2..1c59b90f55 100644 --- a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/search_index_view.hpp +++ b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/search_index_view.hpp @@ -259,8 +259,10 @@ class search_index_view { std::vector _create_many_helper(bsoncxx::v_noabi::array::view created_indexes); - impl const& _get_impl() const; + template + static auto _get_impl(Self& self) -> decltype(*self._impl); + impl const& _get_impl() const; impl& _get_impl(); private: diff --git a/src/mongocxx/lib/mongocxx/v1/data_key_options.cpp b/src/mongocxx/lib/mongocxx/v1/data_key_options.cpp index 8590e76b2f..0d115a7ccf 100644 --- a/src/mongocxx/lib/mongocxx/v1/data_key_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/data_key_options.cpp @@ -138,7 +138,10 @@ data_key_options::internal::to_mongoc(data_key_options const& self) { _impl._key_alt_names.end(), std::back_inserter(names), [](std::string const& name) { - return const_cast(name.c_str()); // For copy only. + // mongoc_client_encryption_datakey_opts_set_keyaltnames() deep-copies the elements of `keyaltnames` + // without modification. + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) + return const_cast(name.c_str()); }); libmongoc::client_encryption_datakey_opts_set_keyaltnames( diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/client.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/client.cpp index 4c6f06ad3c..a1ed52fab6 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/client.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/client.cpp @@ -311,16 +311,20 @@ client::_watch(client_session const* session, pipeline const& pipe, options::cha _get_impl().client_t, to_scoped_bson_view(container), to_scoped_bson_view(options_builder))}; } -client::impl const& client::_get_impl() const { - if (!_impl) { +template +auto client::_get_impl(Self& self) -> decltype(*self._impl) { + if (!self._impl) { throw logic_error{error_code::k_invalid_client_object}; } - return *_impl; + return *self._impl; +} + +client::impl const& client::_get_impl() const { + return _get_impl(*this); } client::impl& client::_get_impl() { - auto cthis = const_cast(this); - return const_cast(cthis->_get_impl()); + return _get_impl(*this); } } // namespace v_noabi diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/client_session.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/client_session.cpp index 8d7f16dbec..c917b2556d 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/client_session.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/client_session.cpp @@ -20,6 +20,8 @@ #include +#include + namespace mongocxx { namespace v_noabi { @@ -29,9 +31,9 @@ client_session::client_session( mongocxx::v_noabi::options::client_session const& options) : _impl(bsoncxx::make_unique(client, options)) {} -client_session::client_session(client_session&&) noexcept = default; +client_session::client_session(client_session&& other) noexcept = default; -client_session& client_session::operator=(client_session&&) noexcept = default; +client_session& client_session::operator=(client_session&& other) noexcept = default; client_session::~client_session() noexcept = default; @@ -91,15 +93,5 @@ bool client_session::get_dirty() const noexcept { return _impl->get_dirty(); } -client_session::impl const& client_session::_get_impl() const { - // Never null. - return *_impl; -} - -client_session::impl& client_session::_get_impl() { - auto cthis = const_cast(this); - return const_cast(cthis->_get_impl()); -} - } // namespace v_noabi } // namespace mongocxx diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/collection.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/collection.cpp index 4f1bd4abd6..01fe8441d1 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/collection.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/collection.cpp @@ -1436,16 +1436,20 @@ bsoncxx::v_noabi::stdx::optional collection::_exec_insert_m return result::insert_many{std::move(result.value()), inserted_ids.extract()}; } -collection::impl const& collection::_get_impl() const { - if (!_impl) { +template +auto collection::_get_impl(Self& self) -> decltype(*self._impl) { + if (!self._impl) { throw logic_error{error_code::k_invalid_collection_object}; } - return *_impl; + return *self._impl; +} + +collection::impl const& collection::_get_impl() const { + return _get_impl(*this); } collection::impl& collection::_get_impl() { - auto cthis = const_cast(this); - return const_cast(cthis->_get_impl()); + return _get_impl(*this); } } // namespace v_noabi diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/database.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/database.cpp index 69ed742a7e..da3cb173bd 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/database.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/database.cpp @@ -420,16 +420,20 @@ database::_watch(client_session const* session, pipeline const& pipe, options::c _get_impl().database_t, to_scoped_bson_view(container), to_scoped_bson_view(options_builder))}; } -database::impl const& database::_get_impl() const { - if (!_impl) { +template +auto database::_get_impl(Self& self) -> decltype(*self._impl) { + if (!self._impl) { throw logic_error{error_code::k_invalid_database_object}; } - return *_impl; + return *self._impl; +} + +database::impl const& database::_get_impl() const { + return _get_impl(*this); } database::impl& database::_get_impl() { - auto cthis = const_cast(this); - return const_cast(cthis->_get_impl()); + return _get_impl(*this); } } // namespace v_noabi diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp index 35dc993ea1..86a99d5c64 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp @@ -556,16 +556,20 @@ void bucket::create_indexes_if_nonexistent(client_session const* session) { _get_impl().indexes_created = true; } -bucket::impl const& bucket::_get_impl() const { - if (!_impl) { +template +auto bucket::_get_impl(Self& self) -> decltype(*self._impl) { + if (!self._impl) { throw logic_error{error_code::k_invalid_gridfs_bucket_object}; } - return *_impl; + return *self._impl; +} + +bucket::impl const& bucket::_get_impl() const { + return _get_impl(*this); } bucket::impl& bucket::_get_impl() { - auto cthis = const_cast(this); - return const_cast(cthis->_get_impl()); + return _get_impl(*this); } } // namespace gridfs diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/downloader.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/downloader.cpp index be4a49fdac..3dc27b3dec 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/downloader.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/downloader.cpp @@ -176,16 +176,20 @@ void downloader::fetch_chunk() { ++_get_impl().chunks_seen; } -downloader::impl const& downloader::_get_impl() const { - if (!_impl) { +template +auto downloader::_get_impl(Self& self) -> decltype(*self._impl) { + if (!self._impl) { throw logic_error{error_code::k_invalid_gridfs_downloader_object}; } - return *_impl; + return *self._impl; +} + +downloader::impl const& downloader::_get_impl() const { + return _get_impl(*this); } downloader::impl& downloader::_get_impl() { - auto cthis = const_cast(this); - return const_cast(cthis->_get_impl()); + return _get_impl(*this); } } // namespace gridfs diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/uploader.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/uploader.cpp index be9a99c0c4..a664a2137f 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/uploader.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/uploader.cpp @@ -201,16 +201,20 @@ void uploader::flush_chunks() { _get_impl().chunks_collection_documents.clear(); } -uploader::impl const& uploader::_get_impl() const { - if (!_impl) { +template +auto uploader::_get_impl(Self& self) -> decltype(*self._impl) { + if (!self._impl) { throw logic_error{error_code::k_invalid_gridfs_uploader_object}; } - return *_impl; + return *self._impl; +} + +uploader::impl const& uploader::_get_impl() const { + return _get_impl(*this); } uploader::impl& uploader::_get_impl() { - auto cthis = const_cast(this); - return const_cast(cthis->_get_impl()); + return _get_impl(*this); } } // namespace gridfs diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/transaction.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/transaction.cpp index bbd7e006f1..5062211286 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/transaction.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/transaction.cpp @@ -78,16 +78,20 @@ bsoncxx::v_noabi::stdx::optional transaction::max_com return _impl->max_commit_time_ms(); } -transaction::impl const& transaction::_get_impl() const { - if (!_impl) { +template +auto transaction::_get_impl(Self& self) -> decltype(*self._impl) { + if (!self._impl) { throw logic_error{error_code::k_invalid_transaction_options_object}; } - return *_impl; + return *self._impl; +} + +transaction::impl const& transaction::_get_impl() const { + return _get_impl(*this); } transaction::impl& transaction::_get_impl() { - auto cthis = const_cast(this); - return const_cast(cthis->_get_impl()); + return _get_impl(*this); } } // namespace options diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_model.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_model.cpp index 8dfc619b16..f2fa249452 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_model.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_model.cpp @@ -48,16 +48,20 @@ search_index_model& search_index_model::type(bsoncxx::v_noabi::string::view_or_v return *this; } -search_index_model::impl const& search_index_model::_get_impl() const { - if (!_impl) { +template +auto search_index_model::_get_impl(Self& self) -> decltype(*self._impl) { + if (!self._impl) { throw logic_error{error_code::k_invalid_search_index_model}; } - return *_impl; + return *self._impl; +} + +search_index_model::impl const& search_index_model::_get_impl() const { + return _get_impl(*this); } search_index_model::impl& search_index_model::_get_impl() { - auto cthis = const_cast(this); - return const_cast(cthis->_get_impl()); + return _get_impl(*this); } } // namespace v_noabi diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_view.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_view.cpp index 3c3987a320..c8ebb9ab9e 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_view.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/search_index_view.cpp @@ -119,16 +119,20 @@ void search_index_view::update_one( _get_impl().update_one(&session, name, definition); } -search_index_view::impl const& search_index_view::_get_impl() const { - if (!_impl) { +template +auto search_index_view::_get_impl(Self& self) -> decltype(*self._impl) { + if (!self._impl) { throw mongocxx::v_noabi::logic_error{error_code::k_invalid_search_index_view}; } - return *_impl; + return *self._impl; +} + +search_index_view::impl const& search_index_view::_get_impl() const { + return _get_impl(*this); } search_index_view::impl& search_index_view::_get_impl() { - auto cthis = const_cast(this); - return const_cast(cthis->_get_impl()); + return _get_impl(*this); } } // namespace v_noabi From 8737378552a4b6dce279310a83723d91ad492f64 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 4 Dec 2025 15:50:39 -0600 Subject: [PATCH 27/39] Address cppcoreguidelines-pro-type-member-init warnings --- .clang-tidy | 2 +- src/bsoncxx/lib/bsoncxx/private/itoa.hh | 8 ++++---- src/bsoncxx/lib/bsoncxx/v1/element/view.cpp | 8 +++++++- src/bsoncxx/lib/bsoncxx/v1/oid.cpp | 6 ++++++ src/bsoncxx/lib/bsoncxx/v1/types/value.cpp | 12 +++++++++--- .../lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp | 6 ++---- 6 files changed, 29 insertions(+), 13 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index bda41a8938..1e564d0384 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -8,7 +8,7 @@ Checks: - -cppcoreguidelines-pro-bounds-constant-array-index # Noise. - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h diff --git a/src/bsoncxx/lib/bsoncxx/private/itoa.hh b/src/bsoncxx/lib/bsoncxx/private/itoa.hh index acbda2f099..6f31a5265e 100644 --- a/src/bsoncxx/lib/bsoncxx/private/itoa.hh +++ b/src/bsoncxx/lib/bsoncxx/private/itoa.hh @@ -27,10 +27,10 @@ class itoa { // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers). static_assert(UINT32_MAX == std::uint32_t{4294967295}, ""); - std::uint32_t _val; - char const* _str; - std::uint8_t _len; - char _buf[sizeof("4294967295")]; + std::uint32_t _val = {}; + char const* _str = {}; + std::uint8_t _len = {}; + char _buf[sizeof("4294967295")] = {}; public: ~itoa() = default; diff --git a/src/bsoncxx/lib/bsoncxx/v1/element/view.cpp b/src/bsoncxx/lib/bsoncxx/v1/element/view.cpp index 7d135ea753..d58b944b4a 100644 --- a/src/bsoncxx/lib/bsoncxx/v1/element/view.cpp +++ b/src/bsoncxx/lib/bsoncxx/v1/element/view.cpp @@ -195,8 +195,10 @@ void view::impl::check() const { view::~view() = default; +// _storage: initialized with placement new. +// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) view::view(view const& other) noexcept { - new (impl::with(this)) impl{impl::with(other)}; + new (_storage.data()) impl{impl::with(other)}; } // NOLINTNEXTLINE(cert-oop54-cpp): handled by impl. @@ -205,10 +207,14 @@ view& view::operator=(view const& other) noexcept { return *this; } +// _storage: initialized with placement new. +// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) view::view() { new (_storage.data()) impl{}; } +// _storage: initialized with placement new. +// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) view::view(impl i) { new (_storage.data()) impl{i}; } diff --git a/src/bsoncxx/lib/bsoncxx/v1/oid.cpp b/src/bsoncxx/lib/bsoncxx/v1/oid.cpp index 94f3dd29a6..bdfb0c9c21 100644 --- a/src/bsoncxx/lib/bsoncxx/v1/oid.cpp +++ b/src/bsoncxx/lib/bsoncxx/v1/oid.cpp @@ -37,6 +37,8 @@ using code = v1::oid::errc; static_assert(is_regular::value, "bsoncxx::v1::oid must be regular"); static_assert(is_semitrivial::value, "bsoncxx::v1::oid must be semitrivial"); +// _bytes: initialized with memcpy. +// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) oid::oid() { #if defined(_WIN32) // Ensure the Winsock DLL is initialized prior to calling `gethostname` in `bsoncxx::v1::oid::oid()`: @@ -66,6 +68,8 @@ oid::oid() { std::memcpy(_bytes.data(), oid.bytes, sizeof(oid.bytes)); } +// _bytes: initialized with memcpy. +// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) oid::oid(std::uint8_t const* bytes, std::size_t len) { if (!bytes) { throw v1::exception{code::null_bytes_ptr}; @@ -78,6 +82,8 @@ oid::oid(std::uint8_t const* bytes, std::size_t len) { std::memcpy(_bytes.data(), bytes, _bytes.size()); } +// _bytes: initialized with memcpy. +// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) oid::oid(v1::stdx::string_view str) { if (str.empty()) { throw v1::exception{code::empty_string}; diff --git a/src/bsoncxx/lib/bsoncxx/v1/types/value.cpp b/src/bsoncxx/lib/bsoncxx/v1/types/value.cpp index 0ca6de85d1..44d961e325 100644 --- a/src/bsoncxx/lib/bsoncxx/v1/types/value.cpp +++ b/src/bsoncxx/lib/bsoncxx/v1/types/value.cpp @@ -145,8 +145,10 @@ value::~value() { impl::with(this)->~impl(); } +// _storage: initialized with placement new. +// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) value::value(value&& other) noexcept { - new (impl::with(this)) impl{std::move(impl::with(other))}; + new (_storage.data()) impl{std::move(impl::with(other))}; } value& value::operator=(value&& other) noexcept { @@ -154,8 +156,10 @@ value& value::operator=(value&& other) noexcept { return *this; } +// _storage: initialized with placement new. +// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) value::value(value const& other) { - new (impl::with(this)) impl{impl::with(other)}; + new (_storage.data()) impl{impl::with(other)}; } // NOLINTNEXTLINE(cert-oop54-cpp): handled by impl. @@ -282,8 +286,10 @@ value::value(v1::types::b_date const v) : value{} { impl::with(this)->v().v_datetime = v.value.count(); } +// _storage: initialized with placement new. +// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) value::value(v1::types::b_null) { - (new (impl::with(this)) impl{})->t() = BSON_TYPE_NULL; + (new (_storage.data()) impl{})->t() = BSON_TYPE_NULL; } value::value(v1::types::b_regex const v) : value{} { diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp index 7d37fea92d..b4d6b71a0a 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/builder/core.cpp @@ -43,9 +43,7 @@ void bson_free_deleter(std::uint8_t* ptr) { // class managed_bson_t { public: - managed_bson_t() { - bson_init(&bson); - } + managed_bson_t() = default; managed_bson_t(managed_bson_t&&) = delete; managed_bson_t& operator=(managed_bson_t&&) = delete; @@ -62,7 +60,7 @@ class managed_bson_t { } private: - bson_t bson; + bson_t bson = BSON_INITIALIZER; }; } // namespace From 03b583b85bbe3f3b551398e31653c1b04cf6f705 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 4 Dec 2025 15:50:39 -0600 Subject: [PATCH 28/39] Address cppcoreguidelines-avoid-c-arrays warnings --- .clang-tidy | 2 +- src/bsoncxx/lib/bsoncxx/private/itoa.cpp | 2 ++ src/bsoncxx/lib/bsoncxx/v1/decimal128.cpp | 7 ++++--- src/bsoncxx/lib/bsoncxx/v1/oid.cpp | 7 ++++--- .../lib/bsoncxx/v_noabi/bsoncxx/json.cpp | 3 +++ .../v_noabi/mongocxx/gridfs/bucket.cpp | 21 +++++++++++++------ .../mongocxx/v_noabi/mongocxx/instance.cpp | 4 ++-- 7 files changed, 31 insertions(+), 15 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 1e564d0384..300b1057e5 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -8,7 +8,7 @@ Checks: - -cppcoreguidelines-pro-bounds-constant-array-index # Noise. - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' HeaderFileExtensions: - '' - h diff --git a/src/bsoncxx/lib/bsoncxx/private/itoa.cpp b/src/bsoncxx/lib/bsoncxx/private/itoa.cpp index 2fd2471553..9096dffce4 100644 --- a/src/bsoncxx/lib/bsoncxx/private/itoa.cpp +++ b/src/bsoncxx/lib/bsoncxx/private/itoa.cpp @@ -18,6 +18,8 @@ namespace bsoncxx { namespace { +// Precomputed table of C strings. +// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) constexpr char k_index_table[] = "0\0" "1\0" diff --git a/src/bsoncxx/lib/bsoncxx/v1/decimal128.cpp b/src/bsoncxx/lib/bsoncxx/v1/decimal128.cpp index 61ecdbc48a..045e4e6510 100644 --- a/src/bsoncxx/lib/bsoncxx/v1/decimal128.cpp +++ b/src/bsoncxx/lib/bsoncxx/v1/decimal128.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -59,9 +60,9 @@ std::string decimal128::to_string() const { bson_decimal128_t d128; d128.high = _high; d128.low = _low; - char str[BSON_DECIMAL128_STRING]; - bson_decimal128_to_string(&d128, str); - return {str}; + std::array str = {}; + bson_decimal128_to_string(&d128, str.data()); + return {str.data()}; } std::error_category const& decimal128::error_category() { diff --git a/src/bsoncxx/lib/bsoncxx/v1/oid.cpp b/src/bsoncxx/lib/bsoncxx/v1/oid.cpp index bdfb0c9c21..9f4af01112 100644 --- a/src/bsoncxx/lib/bsoncxx/v1/oid.cpp +++ b/src/bsoncxx/lib/bsoncxx/v1/oid.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -101,9 +102,9 @@ oid::oid(v1::stdx::string_view str) { std::string oid::to_string() const { bson_oid_t oid; std::memcpy(oid.bytes, _bytes.data(), sizeof(oid.bytes)); - char str[2u * k_oid_length + 1u]; // Two hex digits per byte + null terminator: 25 characters. - bson_oid_to_string(&oid, str); - return std::string(str); + std::array str = {}; // Two hex digits per byte + null terminator: 25 characters. + bson_oid_to_string(&oid, str.data()); + return std::string(str.data()); } std::time_t oid::get_time_t() const { diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp index f5f8dbf777..b936ae67d2 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/json.cpp @@ -56,6 +56,9 @@ std::string to_json_helper(document::view view, decltype(bson_as_legacy_extended } auto const deleter = [](char* result) { bson_free(result); }; + + // Fixed-size dynamic array: size tracked by `size`. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) std::unique_ptr const cleanup(result, deleter); return {result, size}; diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp index 86a99d5c64..2f6e30a58d 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/gridfs/bucket.cpp @@ -261,7 +261,10 @@ void bucket::_upload_from_stream_with_id( std::istream* source, options::gridfs::upload const& options) { uploader upload_stream = _open_upload_stream_with_id(session, id, filename, options); - std::int32_t chunk_size = upload_stream.chunk_size(); + auto const chunk_size = upload_stream.chunk_size(); + + // Fixed-size dynamic array: size tracked by `chunk_size`. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) std::unique_ptr buffer = bsoncxx::make_unique(static_cast(chunk_size)); do { @@ -408,10 +411,13 @@ void bucket::_download_to_stream( bsoncxx::v_noabi::stdx::optional end) { downloader download_stream = _open_download_stream(session, id, start, end); - std::size_t chunk_size = {}; - if (!int32_to_size_t_safe(download_stream.chunk_size(), chunk_size)) { - throw gridfs_exception{error_code::k_invalid_parameter, "expected chunk size to be in bounds of size_t"}; - } + auto const chunk_size = [&download_stream] { + std::size_t ret = {}; + if (!int32_to_size_t_safe(download_stream.chunk_size(), ret)) { + throw gridfs_exception{error_code::k_invalid_parameter, "expected chunk size to be in bounds of size_t"}; + } + return ret; + }(); if (!start) { start.emplace(0); } @@ -423,7 +429,10 @@ void bucket::_download_to_stream( end = file_length_sz; } auto bytes_expected = *end - *start; - std::unique_ptr buffer = bsoncxx::make_unique(static_cast(chunk_size)); + + // Fixed-size dynamic array: size tracked by `chunk_size`. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) + std::unique_ptr buffer = bsoncxx::make_unique(chunk_size); while (bytes_expected > 0) { std::size_t const bytes_read = diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp index e9fd1e3d6f..319fa7c9d6 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp @@ -43,9 +43,9 @@ std::atomic current_instance{nullptr}; // Sentinel value denoting the current instance has been destroyed. instance* sentinel() { - alignas(instance) static unsigned char value[sizeof(instance)]; + alignas(instance) static std::array value = {}; // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast): identity only. - return reinterpret_cast(value); + return reinterpret_cast(value.data()); } } // namespace From e162a99dd8121f79ac3a374aff30b107f1d4b7f3 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 4 Dec 2025 15:50:39 -0600 Subject: [PATCH 29/39] Address cppcoreguidelines-use-enum-class warnings --- .clang-tidy | 4 +- .../lib/bsoncxx/v_noabi/bsoncxx/vector.cpp | 42 +++++++++---------- src/mongocxx/lib/mongocxx/v1/exception.cpp | 1 + .../v_noabi/mongocxx/options/index.cpp | 4 +- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 300b1057e5..a187674492 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -8,7 +8,7 @@ Checks: - -cppcoreguidelines-pro-bounds-constant-array-index # Noise. - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved,-cppcoreguidelines-use-enum-class' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved' HeaderFileExtensions: - '' - h @@ -123,6 +123,6 @@ CheckOptions: cppcoreguidelines-special-member-functions.IgnoreMacros: 'true' cppcoreguidelines-use-default-member-init.IgnoreMacros: 'true' cppcoreguidelines-use-default-member-init.UseAssignment: 'false' - cppcoreguidelines-use-enum-class.IgnoreUnscopedEnumsInClasses: 'false' + cppcoreguidelines-use-enum-class.IgnoreUnscopedEnumsInClasses: 'true' SystemHeaders: false ... diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/vector.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/vector.cpp index 931d0da9c1..42f289cbfe 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/vector.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/vector.cpp @@ -27,9 +27,11 @@ namespace bsoncxx { namespace v_noabi { namespace vector { -namespace detail { + +namespace { // Equivalent to bson_vector_element_type_t. +// NOLINTNEXTLINE(cppcoreguidelines-use-enum-class): used by bitwise operations. enum element_type : std::uint8_t { signed_integer = 0, unsigned_integer = 1, @@ -37,22 +39,23 @@ enum element_type : std::uint8_t { }; // Equivalent to bson_vector_element_size_t. +// NOLINTNEXTLINE(cppcoreguidelines-use-enum-class): used by bitwise operations. enum element_size : std::uint8_t { bits_1 = 0, bits_8 = 3, bits_32 = 7, }; -static header make_header(element_type element_type, element_size element_size, std::uint8_t padding) { - return {{static_cast((element_type << 4) | element_size), padding}}; +detail::header make_header(element_type type, element_size size, std::uint8_t padding) { + return {{static_cast((type << 4) | size), padding}}; } -static void write_header(std::uint8_t* binary_data, header const& hdr) { - std::memcpy(binary_data, hdr.data(), header_size); +void write_header(std::uint8_t* binary_data, detail::header const& hdr) { + std::memcpy(binary_data, hdr.data(), detail::header_size); } template -static std::uint32_t libbson_length_for_append(std::size_t element_count, Impl func) { +std::uint32_t libbson_length_for_append(std::size_t element_count, Impl func) { std::uint32_t result = func(element_count); if (result < BSON_VECTOR_HEADER_LEN) { throw exception{error_code::k_vector_too_large}; @@ -61,58 +64,53 @@ static std::uint32_t libbson_length_for_append(std::size_t element_count, Impl f } template -static void libbson_validate(types::b_binary const& binary, Impl func) { +void libbson_validate(types::b_binary const& binary, Impl func) { if (binary.sub_type != binary_sub_type::k_vector || !func(NULL, binary.bytes, binary.size)) { throw exception{error_code::k_invalid_vector}; } } -} // namespace detail +} // namespace namespace formats { std::uint32_t f_int8::length_for_append(std::size_t element_count) { - return detail::libbson_length_for_append(element_count, bson_vector_int8_binary_data_length); + return libbson_length_for_append(element_count, bson_vector_int8_binary_data_length); } std::uint32_t f_float32::length_for_append(std::size_t element_count) { - return detail::libbson_length_for_append(element_count, bson_vector_float32_binary_data_length); + return libbson_length_for_append(element_count, bson_vector_float32_binary_data_length); } std::uint32_t f_packed_bit::length_for_append(std::size_t element_count) { - return detail::libbson_length_for_append(element_count, bson_vector_packed_bit_binary_data_length); + return libbson_length_for_append(element_count, bson_vector_packed_bit_binary_data_length); } void f_int8::write_frame(std::uint8_t* binary_data, std::uint32_t, std::size_t) { - detail::write_header( - binary_data, detail::make_header(detail::element_type::signed_integer, detail::element_size::bits_8, 0)); + write_header(binary_data, make_header(element_type::signed_integer, element_size::bits_8, 0)); } void f_float32::write_frame(std::uint8_t* binary_data, std::uint32_t, std::size_t) { - detail::write_header( - binary_data, detail::make_header(detail::element_type::floating_point, detail::element_size::bits_32, 0)); + write_header(binary_data, make_header(element_type::floating_point, element_size::bits_32, 0)); } void f_packed_bit::write_frame(std::uint8_t* binary_data, std::uint32_t binary_data_length, std::size_t element_count) { static constexpr std::uint8_t mask = {0x7u}; binary_data[binary_data_length - 1] = UINT8_C(0); - detail::write_header( - binary_data, - detail::make_header( - detail::element_type::unsigned_integer, detail::element_size::bits_1, std::uint8_t(mask & -element_count))); + write_header(binary_data, make_header(unsigned_integer, bits_1, std::uint8_t(mask & -element_count))); } void formats::f_int8::validate(types::b_binary const& binary) { - return detail::libbson_validate(binary, bson_vector_int8_const_view_init); + return libbson_validate(binary, bson_vector_int8_const_view_init); } void formats::f_float32::validate(types::b_binary const& binary) { - return detail::libbson_validate(binary, bson_vector_float32_const_view_init); + return libbson_validate(binary, bson_vector_float32_const_view_init); } void formats::f_packed_bit::validate(types::b_binary const& binary) { - return detail::libbson_validate(binary, bson_vector_packed_bit_const_view_init); + return libbson_validate(binary, bson_vector_packed_bit_const_view_init); } } // namespace formats diff --git a/src/mongocxx/lib/mongocxx/v1/exception.cpp b/src/mongocxx/lib/mongocxx/v1/exception.cpp index f4fd9316fe..2bf1b669e1 100644 --- a/src/mongocxx/lib/mongocxx/v1/exception.cpp +++ b/src/mongocxx/lib/mongocxx/v1/exception.cpp @@ -220,6 +220,7 @@ v1::exception make_exception(bson_error_t const& error) { auto const has_message = message[0] != '\0'; // Undocumented: see mongoc-error-private.h. + // NOLINTNEXTLINE(cppcoreguidelines-use-enum-class): compile-time constants. enum : int { MONGOC_ERROR_CATEGORY_BSON = 1, // BSON_ERROR_CATEGORY MONGOC_ERROR_CATEGORY = 2, diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/index.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/index.cpp index 0a1f2e7b1b..7cb6c5c95f 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/index.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/index.cpp @@ -213,7 +213,7 @@ bsoncxx::v_noabi::stdx::optional const& index::haystack_bucket_size() co } // CDRIVER-5946: mongoc_index_storage_opt_type_t was removed in mongoc 2.0. -enum mongoc_index_storage_opt_type_t { +enum struct mongoc_index_storage_opt_type_t { MONGOC_INDEX_STORAGE_OPT_MMAPV1, MONGOC_INDEX_STORAGE_OPT_WIREDTIGER, }; @@ -328,7 +328,7 @@ index::wiredtiger_storage_options::config_string() const { } int index::wiredtiger_storage_options::type() const { - return mongoc_index_storage_opt_type_t::MONGOC_INDEX_STORAGE_OPT_WIREDTIGER; + return static_cast(mongoc_index_storage_opt_type_t::MONGOC_INDEX_STORAGE_OPT_WIREDTIGER); } } // namespace options From a13a33e9d20f1744f38498a0ccc9638a72b513df Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 4 Dec 2025 15:50:39 -0600 Subject: [PATCH 30/39] Suppress cppcoreguidelines-rvalue-reference-param-not-moved --- src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/value.cpp | 1 + src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/value.cpp | 1 + src/mongocxx/include/mongocxx/v_noabi/mongocxx/uri.hpp | 1 + src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp | 6 ++---- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/value.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/value.cpp index 008b550415..c4885fc4ed 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/value.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/value.cpp @@ -54,6 +54,7 @@ value::value(v_noabi::array::view view) namespace bsoncxx { namespace v_noabi { +// NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved): ownership transfer with `v.release()`. v_noabi::array::value from_v1(v1::array::value&& v) { auto const deleter_ptr = v.get_deleter().target(); diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/value.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/value.cpp index 547af84dcb..194aba4d6d 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/value.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/value.cpp @@ -63,6 +63,7 @@ value::value(v_noabi::document::view view) namespace bsoncxx { namespace v_noabi { +// NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved): ownership transfer with `v.release()`. v_noabi::document::value from_v1(v1::document::value&& v) { auto const deleter_ptr = v.get_deleter().target(); diff --git a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/uri.hpp b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/uri.hpp index c51303ac92..ee7022a1dc 100644 --- a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/uri.hpp +++ b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/uri.hpp @@ -16,6 +16,7 @@ #include #include +#include #include #include diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp index f3bf9ae6fd..df641b57e9 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp @@ -16,7 +16,7 @@ #include #include -#include +#include #include #include @@ -49,9 +49,7 @@ std::string to_string_null_safe(char const* str) { std::string const uri::k_default_uri = "mongodb://localhost:27017"; -uri::uri(std::unique_ptr&& implementation) { - _impl.reset(implementation.release()); -} +uri::uri(std::unique_ptr&& implementation) : _impl{std::move(implementation)} {} uri::uri(bsoncxx::v_noabi::string::view_or_value uri_string) { bson_error_t error; From 192612ea7ae595282f20d8b48ed01b20f996445b Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 8 Dec 2025 13:43:07 -0600 Subject: [PATCH 31/39] Address cppcoreguidelines-owning-memory warnings --- src/mongocxx/lib/mongocxx/v1/aggregate_options.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/find_one_and_delete_options.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/find_options.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/replace_one_options.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/update_many_options.cpp | 4 ++++ src/mongocxx/lib/mongocxx/v1/update_one_options.cpp | 4 ++++ 6 files changed, 24 insertions(+) diff --git a/src/mongocxx/lib/mongocxx/v1/aggregate_options.cpp b/src/mongocxx/lib/mongocxx/v1/aggregate_options.cpp index c5d2543df3..8557958299 100644 --- a/src/mongocxx/lib/mongocxx/v1/aggregate_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/aggregate_options.cpp @@ -68,6 +68,8 @@ class aggregate_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + aggregate_options::~aggregate_options() { delete impl::with(this); } @@ -94,6 +96,8 @@ aggregate_options& aggregate_options::operator=(aggregate_options const& other) aggregate_options::aggregate_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + aggregate_options& aggregate_options::allow_disk_use(bool allow_disk_use) { impl::with(this)->_allow_disk_use = allow_disk_use; return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/find_one_and_delete_options.cpp b/src/mongocxx/lib/mongocxx/v1/find_one_and_delete_options.cpp index ae76c540cb..6dff7bd0d6 100644 --- a/src/mongocxx/lib/mongocxx/v1/find_one_and_delete_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/find_one_and_delete_options.cpp @@ -62,6 +62,8 @@ class find_one_and_delete_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + find_one_and_delete_options::~find_one_and_delete_options() { delete impl::with(this); } @@ -90,6 +92,8 @@ find_one_and_delete_options& find_one_and_delete_options::operator=(find_one_and find_one_and_delete_options::find_one_and_delete_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + find_one_and_delete_options& find_one_and_delete_options::collation(bsoncxx::v1::document::value collation) { impl::with(this)->_collation = std::move(collation); return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/find_options.cpp b/src/mongocxx/lib/mongocxx/v1/find_options.cpp index 9f44890d13..2a3e54c6f7 100644 --- a/src/mongocxx/lib/mongocxx/v1/find_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/find_options.cpp @@ -76,6 +76,8 @@ class find_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + find_options::~find_options() { delete impl::with(_impl); } @@ -102,6 +104,8 @@ find_options& find_options::operator=(find_options const& other) { find_options::find_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + find_options& find_options::allow_disk_use(bool allow_disk_use) { impl::with(this)->_allow_disk_use = allow_disk_use; return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/replace_one_options.cpp b/src/mongocxx/lib/mongocxx/v1/replace_one_options.cpp index cf6b563182..2ed47b0c4d 100644 --- a/src/mongocxx/lib/mongocxx/v1/replace_one_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/replace_one_options.cpp @@ -60,6 +60,8 @@ class replace_one_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + replace_one_options::~replace_one_options() { delete impl::with(this); } @@ -87,6 +89,8 @@ replace_one_options& replace_one_options::operator=(replace_one_options const& o replace_one_options::replace_one_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + replace_one_options& replace_one_options::bypass_document_validation(bool bypass_document_validation) { impl::with(this)->_bypass_document_validation = std::move(bypass_document_validation); return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/update_many_options.cpp b/src/mongocxx/lib/mongocxx/v1/update_many_options.cpp index c4102a682c..c318dde6dc 100644 --- a/src/mongocxx/lib/mongocxx/v1/update_many_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/update_many_options.cpp @@ -64,6 +64,8 @@ class update_many_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + update_many_options::~update_many_options() { delete impl::with(this); } @@ -91,6 +93,8 @@ update_many_options& update_many_options::operator=(update_many_options const& o update_many_options::update_many_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + update_many_options& update_many_options::bypass_document_validation(bool bypass_document_validation) { impl::with(this)->_bypass_document_validation = bypass_document_validation; return *this; diff --git a/src/mongocxx/lib/mongocxx/v1/update_one_options.cpp b/src/mongocxx/lib/mongocxx/v1/update_one_options.cpp index f61ec4f7ec..6f86f732eb 100644 --- a/src/mongocxx/lib/mongocxx/v1/update_one_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/update_one_options.cpp @@ -64,6 +64,8 @@ class update_one_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + update_one_options::~update_one_options() { delete impl::with(this); } @@ -90,6 +92,8 @@ update_one_options& update_one_options::operator=(update_one_options const& othe update_one_options::update_one_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + update_one_options& update_one_options::bypass_document_validation(bool bypass_document_validation) { impl::with(this)->_bypass_document_validation = bypass_document_validation; return *this; From 515e9d7bf0bd8fa258dbdfb05639d242836ffc41 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 8 Dec 2025 13:43:07 -0600 Subject: [PATCH 32/39] Suppress cert-oop54-cpp warnings --- .../lib/mongocxx/v_noabi/mongocxx/options/transaction.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/transaction.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/transaction.cpp index 65147494bc..cdb508527f 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/transaction.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/transaction.cpp @@ -50,6 +50,7 @@ Transaction& check_moved_from(Transaction& txn) { transaction::transaction(transaction const& other) : _txn{check_moved_from(other._txn)} {} +// NOLINTNEXTLINE(cert-oop54-cpp): handled by v1::transaction. transaction& transaction::operator=(transaction const& other) { _txn = check_moved_from(other._txn); return *this; From 49e264918c046c0a4f3ed2e9c099ef8f4fa9403e Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 8 Dec 2025 13:43:07 -0600 Subject: [PATCH 33/39] Disable cppcoreguidelines-pro-type-vararg --- .clang-tidy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index a187674492..d683b1cd15 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -8,7 +8,8 @@ Checks: - -cppcoreguidelines-pro-bounds-constant-array-index # Noise. - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-rvalue-reference-param-not-moved' + - -cppcoreguidelines-pro-type-vararg # Noise. +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-rvalue-reference-param-not-moved' HeaderFileExtensions: - '' - h From 1c3a7e063f0f18fd9faed1d79555f884bc186589 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 8 Dec 2025 13:43:07 -0600 Subject: [PATCH 34/39] Enforce cppcoreguidelines-rvalue-reference-param-not-moved --- .clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index d683b1cd15..eb93b1572a 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -9,7 +9,7 @@ Checks: - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. - -cppcoreguidelines-pro-type-vararg # Noise. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-rvalue-reference-param-not-moved' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-union-access' HeaderFileExtensions: - '' - h From 7aecce04947d34328acf0ddd5518b916af22b46e Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 8 Dec 2025 13:43:07 -0600 Subject: [PATCH 35/39] Remove cppcoreguidelines-pro-type-union-access exclusion --- .clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index eb93b1572a..db25384836 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -9,7 +9,7 @@ Checks: - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. - -cppcoreguidelines-pro-type-vararg # Noise. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-union-access' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables' HeaderFileExtensions: - '' - h From 04a543d88a492dd6966d2ecc15dfd8dd5565443a Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 8 Dec 2025 13:43:07 -0600 Subject: [PATCH 36/39] Disable cppcoreguidelines-avoid-do-while --- .clang-tidy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index db25384836..169b1576f0 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -4,12 +4,13 @@ Checks: - cert-* - cppcoreguidelines-* - clang-analyzer-*' + - -cppcoreguidelines-avoid-do-while # Noise - -cppcoreguidelines-pro-bounds-array-to-pointer-decay # Noise. - -cppcoreguidelines-pro-bounds-constant-array-index # Noise. - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. - -cppcoreguidelines-pro-type-vararg # Noise. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-avoid-non-const-global-variables' +WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-non-const-global-variables' HeaderFileExtensions: - '' - h From 2f26d61a8e6b55107e5783d5fa47e72b7edbc2eb Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 8 Dec 2025 13:43:07 -0600 Subject: [PATCH 37/39] Suppress cert-err58-cpp --- .clang-tidy | 2 +- src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index 169b1576f0..b38625ad6c 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -10,7 +10,7 @@ Checks: - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. - -cppcoreguidelines-pro-type-vararg # Noise. -WarningsAsErrors: '*,-cert-err58-cpp,-cppcoreguidelines-avoid-non-const-global-variables' +WarningsAsErrors: '*,-cppcoreguidelines-avoid-non-const-global-variables' HeaderFileExtensions: - '' - h diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp index df641b57e9..deab0c0392 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/uri.cpp @@ -47,6 +47,7 @@ std::string to_string_null_safe(char const* str) { } // namespace +// NOLINTNEXTLINE(cert-err58-cpp): v_noabi backward compatibility. std::string const uri::k_default_uri = "mongodb://localhost:27017"; uri::uri(std::unique_ptr&& implementation) : _impl{std::move(implementation)} {} From 0bd2c8fceea9b80648ac5aa9cb2a0b7da362ece8 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 8 Dec 2025 13:43:07 -0600 Subject: [PATCH 38/39] Suppress cppcoreguidelines-avoid-non-const-global-variables warnings --- .clang-tidy | 2 +- src/mongocxx/lib/mongocxx/v1/instance.cpp | 1 + src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index b38625ad6c..5ec90852d9 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -10,7 +10,7 @@ Checks: - -cppcoreguidelines-pro-bounds-pointer-arithmetic # Noise. - -cppcoreguidelines-pro-type-union-access # Requires C++17 or newer for `std::variant`. - -cppcoreguidelines-pro-type-vararg # Noise. -WarningsAsErrors: '*,-cppcoreguidelines-avoid-non-const-global-variables' +WarningsAsErrors: '*' HeaderFileExtensions: - '' - h diff --git a/src/mongocxx/lib/mongocxx/v1/instance.cpp b/src/mongocxx/lib/mongocxx/v1/instance.cpp index 675ec3aaea..968729de90 100644 --- a/src/mongocxx/lib/mongocxx/v1/instance.cpp +++ b/src/mongocxx/lib/mongocxx/v1/instance.cpp @@ -50,6 +50,7 @@ namespace { // 0: no instance object has been created. // 1: there is a single instance object. // 2: the single instance object has been destroyed. +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): support `code::multiple_instances`. std::atomic_int instance_state{0}; void custom_log_handler( diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp index 319fa7c9d6..bc3f369423 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/instance.cpp @@ -38,7 +38,7 @@ namespace v_noabi { namespace { -// To support mongocxx::v_noabi::instance::current(). +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): support `instance::current()`. std::atomic current_instance{nullptr}; // Sentinel value denoting the current instance has been destroyed. From af8a7960e2be177fe70bc863cdaac4951082f7a8 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 8 Dec 2025 15:39:28 -0600 Subject: [PATCH 39/39] Suppress cppcoreguidelines-owning-memory warnings --- src/mongocxx/lib/mongocxx/v1/find_one_and_update_options.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/mongocxx/lib/mongocxx/v1/find_one_and_update_options.cpp b/src/mongocxx/lib/mongocxx/v1/find_one_and_update_options.cpp index 680605ef1c..7a46dfbcea 100644 --- a/src/mongocxx/lib/mongocxx/v1/find_one_and_update_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/find_one_and_update_options.cpp @@ -68,6 +68,8 @@ class find_one_and_update_options::impl { } }; +// NOLINTBEGIN(cppcoreguidelines-owning-memory): owning void* for ABI stability. + find_one_and_update_options::~find_one_and_update_options() { delete impl::with(this); } @@ -96,6 +98,8 @@ find_one_and_update_options& find_one_and_update_options::operator=(find_one_and find_one_and_update_options::find_one_and_update_options() : _impl{new impl{}} {} +// NOLINTEND(cppcoreguidelines-owning-memory) + find_one_and_update_options& find_one_and_update_options::collation(bsoncxx::v1::document::value collation) { impl::with(this)->_collation = std::move(collation); return *this;