Skip to content

Commit 0d649bf

Browse files
authored
Reduce Warnings - Address warnings in examples and redundant recompilations (#1201)
* Avoid redundant recompilation of mongocxx test components * Address -Wmissing-prototypes warnings * Address -Wold-style-cast warnings * Address -Wmissing-noreturn warnings * Address -Wsuggest-override warnings * Address -Wreserved-identifier warnings * Address -Wunreachable-code-return warnings * Address -Wfloat-equal warnings * Address -Wunused-exception-parameter warnings * Address -Wswitch-enum warnings * Address -Wunused* warnings * Address "hides previous local declaration" warnings (C4456) * Improve type consistency of key generation in examples * Address -Wnewline-eof warnings
1 parent f183933 commit 0d649bf

38 files changed

+311
-255
lines changed

examples/bsoncxx/decimal128.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int main() {
2727
decimal128 d128;
2828
try {
2929
d128 = decimal128{"1.234E+3456"};
30-
} catch (const bsoncxx::exception& e) {
30+
} catch (const bsoncxx::exception&) {
3131
// The example won't fail, but in general, arbitrary strings
3232
// might not convert properly.
3333
return EXIT_FAILURE;

examples/bsoncxx/view_and_value.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@ int main() {
5858
// note that all of the interesting methods for reading BSON are defined on the view type.
5959

6060
// iterate over the elements in a bson document
61-
for (document::element ele : view) {
61+
for (document::element doc_ele : view) {
6262
// element is non owning view of a key-value pair within a document.
6363

6464
// we can use the key() method to get a string_view of the key.
65-
stdx::string_view field_key{ele.key()};
65+
stdx::string_view field_key{doc_ele.key()};
6666

6767
std::cout << "Got key, key = " << field_key << std::endl;
6868

6969
// we can use type() to get the type of the value.
70-
switch (ele.type()) {
70+
switch (doc_ele.type()) {
7171
case type::k_string:
7272
std::cout << "Got String!" << std::endl;
7373
break;
@@ -77,20 +77,40 @@ int main() {
7777
case type::k_array: {
7878
std::cout << "Got Array!" << std::endl;
7979
// if we have a subarray, we can access it by getting a view of it.
80-
array::view subarr{ele.get_array().value};
81-
for (array::element ele : subarr) {
80+
array::view subarr{doc_ele.get_array().value};
81+
for (array::element arr_ele : subarr) {
8282
std::cout << "array element: "
83-
<< bsoncxx::string::to_string(ele.get_string().value) << std::endl;
83+
<< bsoncxx::string::to_string(arr_ele.get_string().value)
84+
<< std::endl;
8485
}
8586
break;
8687
}
87-
default:
88+
89+
case type::k_double:
90+
case type::k_document:
91+
case type::k_binary:
92+
case type::k_undefined:
93+
case type::k_bool:
94+
case type::k_date:
95+
case type::k_null:
96+
case type::k_regex:
97+
case type::k_dbpointer:
98+
case type::k_code:
99+
case type::k_symbol:
100+
case type::k_codewscope:
101+
case type::k_int32:
102+
case type::k_timestamp:
103+
case type::k_int64:
104+
case type::k_decimal128:
105+
case type::k_maxkey:
106+
case type::k_minkey:
88107
std::cout << "We messed up!" << std::endl;
108+
break;
89109
}
90110

91111
// usually we don't need to actually use a switch statement, because we can also
92112
// get a variant 'value' that can hold any BSON type.
93-
types::bson_value::view ele_val{ele.get_value()};
113+
types::bson_value::view doc_ele_val{doc_ele.get_value()};
94114
}
95115

96116
// If we want to search for an element we can use operator[]

examples/mongocxx/automatic_client_side_field_level_encryption.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <algorithm>
16+
#include <cstdint>
1617
#include <fstream>
1718
#include <iostream>
1819
#include <random>
@@ -45,6 +46,8 @@ using bsoncxx::types::bson_value::make_value;
4546

4647
using namespace mongocxx;
4748

49+
namespace {
50+
4851
const int kKeyLength = 96;
4952

5053
using ns_pair = std::pair<std::string, std::string>;
@@ -92,15 +95,19 @@ bsoncxx::document::value doc_from_file(std::string path) {
9295
return bsoncxx::from_json(file_contents);
9396
}
9497

98+
} // namespace
99+
95100
int main() {
96101
instance inst{};
97102

98103
// This must be the same master key that was used to create
99104
// the encryption key; here, we use a random key as a placeholder.
100-
char key_storage[kKeyLength];
101-
std::generate_n(key_storage, kKeyLength, std::rand);
105+
std::uint8_t key_storage[kKeyLength];
106+
std::generate_n(key_storage, kKeyLength, []() {
107+
return static_cast<std::uint8_t>(std::rand() % UINT8_MAX);
108+
});
102109
bsoncxx::types::b_binary local_master_key{
103-
bsoncxx::binary_sub_type::k_binary, kKeyLength, (const uint8_t*)&key_storage};
110+
bsoncxx::binary_sub_type::k_binary, kKeyLength, key_storage};
104111

105112
auto kms_providers = document{} << "local" << open_document << "key" << local_master_key
106113
<< close_document << finalize;

examples/mongocxx/change_streams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
namespace {
3030

3131
// watch_forever iterates the change stream until an error occurs.
32-
void watch_forever(mongocxx::collection& collection) {
32+
[[noreturn]] void watch_forever(mongocxx::collection& collection) {
3333
mongocxx::options::change_stream options;
3434
// Wait up to 1 second before polling again.
3535
const std::chrono::milliseconds await_time{1000};

examples/mongocxx/explicit_encryption.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <algorithm>
16+
#include <cstdint>
1617
#include <iostream>
1718
#include <random>
1819

@@ -49,10 +50,12 @@ int main() {
4950

5051
// This must be the same master key that was used to create
5152
// the encryption key; here, we use a random key as a placeholder.
52-
char key_storage[kKeyLength];
53-
std::generate_n(key_storage, kKeyLength, std::rand);
53+
std::uint8_t key_storage[kKeyLength];
54+
std::generate_n(key_storage, kKeyLength, []() {
55+
return static_cast<std::uint8_t>(std::rand() % UINT8_MAX);
56+
});
5457
bsoncxx::types::b_binary local_master_key{
55-
bsoncxx::binary_sub_type::k_binary, kKeyLength, (const uint8_t*)&key_storage};
58+
bsoncxx::binary_sub_type::k_binary, kKeyLength, key_storage};
5659

5760
auto kms_providers = document{} << "local" << open_document << "key" << local_master_key
5861
<< close_document << finalize;

examples/mongocxx/explicit_encryption_auto_decryption.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <algorithm>
16+
#include <cstdint>
1617
#include <iostream>
1718
#include <random>
1819

@@ -49,10 +50,12 @@ int main() {
4950

5051
// This must be the same master key that was used to create
5152
// the encryption key; here, we use a random key as a placeholder.
52-
char key_storage[kKeyLength];
53-
std::generate_n(key_storage, kKeyLength, std::rand);
53+
std::uint8_t key_storage[kKeyLength];
54+
std::generate_n(key_storage, kKeyLength, []() {
55+
return static_cast<std::uint8_t>(std::rand() % UINT8_MAX);
56+
});
5457
bsoncxx::types::b_binary local_master_key{
55-
bsoncxx::binary_sub_type::k_binary, kKeyLength, (const uint8_t*)&key_storage};
58+
bsoncxx::binary_sub_type::k_binary, kKeyLength, key_storage};
5659

5760
auto kms_providers = document{} << "local" << open_document << "key" << local_master_key
5861
<< close_document << finalize;

examples/mongocxx/get_values_from_documents.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ using bsoncxx::builder::basic::kvp;
2525
using bsoncxx::builder::basic::make_array;
2626
using bsoncxx::builder::basic::make_document;
2727

28+
namespace {
29+
2830
// Document model, showing array with nested documents:
2931
//
3032
// {
@@ -61,11 +63,11 @@ void iterate_messagelist(const bsoncxx::document::element& ele) {
6163
// Check validity and type before trying to iterate.
6264
if (ele.type() == type::k_array) {
6365
bsoncxx::array::view subarray{ele.get_array().value};
64-
for (const bsoncxx::array::element& msg : subarray) {
66+
for (const bsoncxx::array::element& message : subarray) {
6567
// Check correct type before trying to access elements.
6668
// Only print out fields if they exist; don't report missing fields.
67-
if (msg.type() == type::k_document) {
68-
bsoncxx::document::view subdoc = msg.get_document().value;
69+
if (message.type() == type::k_document) {
70+
bsoncxx::document::view subdoc = message.get_document().value;
6971
bsoncxx::document::element uid = subdoc["uid"];
7072
bsoncxx::document::element status = subdoc["status"];
7173
bsoncxx::document::element msg = subdoc["msg"];
@@ -118,6 +120,8 @@ void iterate_documents(mongocxx::collection& coll) {
118120
}
119121
}
120122

123+
} // namespace
124+
121125
int main() {
122126
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
123127
// respectively. Therefore, a mongocxx::instance must be created before using the driver and

examples/mongocxx/instance_management.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,22 @@ class mongo_access {
5656
return _pool->acquire();
5757
}
5858

59-
bsoncxx::stdx::optional<connection> try_get_connection() {
60-
return _pool->try_acquire();
61-
}
62-
6359
private:
6460
mongo_access() = default;
6561

6662
std::unique_ptr<mongocxx::instance> _instance = nullptr;
6763
std::unique_ptr<mongocxx::pool> _pool = nullptr;
6864
};
6965

70-
} // namespace
71-
7266
// The 'configure' and 'do_work' functions use the same mongocxx::instance and mongocxx::pool
7367
// objects by way of the mongo_access singleton.
7468

7569
void configure(mongocxx::uri uri) {
7670
class noop_logger : public mongocxx::logger {
7771
public:
78-
virtual void operator()(mongocxx::log_level,
79-
bsoncxx::stdx::string_view,
80-
bsoncxx::stdx::string_view) noexcept {}
72+
void operator()(mongocxx::log_level,
73+
bsoncxx::stdx::string_view,
74+
bsoncxx::stdx::string_view) noexcept override {}
8175
};
8276

8377
// Use `std::make_unique` with C++14 and newer.
@@ -97,6 +91,8 @@ bool do_work() {
9791
return true;
9892
}
9993

94+
} // namespace
95+
10096
int main(int argc, char* argv[]) {
10197
auto uri = mongocxx::uri{(argc >= 2) ? argv[1] : mongocxx::uri::k_default_uri};
10298
configure(std::move(uri));

examples/mongocxx/mongodb.com/aggregation_examples.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626
#include <mongocxx/exception/logic_error.hpp>
2727
#include <mongocxx/instance.hpp>
2828

29-
// NOTE: Any time this file is modified, a DOCS ticket should be opened to sync the changes with the
30-
// corresponding page on mongodb.com/docs. See CXX-1514, CXX-1249, and DRIVERS-356 for more info.
31-
3229
using namespace mongocxx;
3330

31+
namespace {
32+
3433
void aggregation_examples(const mongocxx::database& db) {
3534
{
3635
// Start Aggregation Example 1
@@ -143,6 +142,8 @@ void aggregation_examples(const mongocxx::database& db) {
143142
}
144143
}
145144

145+
} // namespace
146+
146147
int main() {
147148
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
148149
// respectively. Therefore, a mongocxx::instance must be created before using the driver and

examples/mongocxx/mongodb.com/documentation_examples.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
#include <mongocxx/options/server_api.hpp>
3434
#include <mongocxx/uri.hpp>
3535

36-
// NOTE: Any time this file is modified, a DOCS ticket should be opened to sync the changes with the
37-
// corresponding page on mongodb.com/docs. See CXX-1249 and DRIVERS-356 for more info.
36+
namespace {
3837

3938
template <typename T>
4039
void check_field(const T& document,
@@ -141,7 +140,7 @@ std::string getenv_or_fail(const char* s) {
141140
// Returns a document with credentials for KMS providers.
142141
// If include_external is true, all KMS providers are set.
143142
// If include_external is false, only the local provider is set.
144-
bsoncxx::document::value _make_kms_doc(bool include_external = true) {
143+
bsoncxx::document::value make_kms_doc(bool include_external = true) {
145144
using bsoncxx::builder::basic::sub_document;
146145
using bsoncxx::builder::stream::close_array;
147146
using bsoncxx::builder::stream::close_document;
@@ -1442,19 +1441,19 @@ static bool is_snapshot_ready(mongocxx::client& client, mongocxx::collection& co
14421441
opts.snapshot(true);
14431442

14441443
auto session = client.start_session(opts);
1444+
14451445
try {
1446-
auto maybe_value = collection.find_one(session, {});
1447-
if (maybe_value) {
1446+
if (collection.find_one(session, {})) {
14481447
return true;
14491448
}
1450-
return false;
14511449
} catch (const mongocxx::operation_exception& e) {
14521450
if (e.code().value() == 246) { // snapshot unavailable
14531451
return false;
14541452
}
14551453
throw;
14561454
}
1457-
return true;
1455+
1456+
return false;
14581457
}
14591458

14601459
// Seed the pets database and wait for the snapshot to become available.
@@ -1612,7 +1611,7 @@ static void queryable_encryption_api(mongocxx::client& client) {
16121611
options::client_encryption ce_opts;
16131612
ce_opts.key_vault_client(&key_vault_client);
16141613
ce_opts.key_vault_namespace({"keyvault", "datakeys"});
1615-
ce_opts.kms_providers(_make_kms_doc(false));
1614+
ce_opts.kms_providers(make_kms_doc(false));
16161615
client_encryption client_encryption(std::move(ce_opts));
16171616

16181617
auto key1_id = client_encryption.create_data_key("local");
@@ -1634,7 +1633,7 @@ static void queryable_encryption_api(mongocxx::client& client) {
16341633
// Create an Queryable Encryption collection.
16351634
options::auto_encryption auto_encrypt_opts{};
16361635
auto_encrypt_opts.key_vault_namespace({"keyvault", "datakeys"});
1637-
auto_encrypt_opts.kms_providers(_make_kms_doc(false));
1636+
auto_encrypt_opts.kms_providers(make_kms_doc(false));
16381637
auto_encrypt_opts.encrypted_fields_map(encrypted_fields_map.view());
16391638

16401639
// Optional, If mongocryptd is not in PATH, then find the binary at MONGOCRYPTD_PATH.
@@ -1699,6 +1698,8 @@ static void queryable_encryption_api(mongocxx::client& client) {
16991698
// End Queryable Encryption Example
17001699
}
17011700

1701+
} // namespace
1702+
17021703
int main() {
17031704
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
17041705
// respectively. Therefore, a mongocxx::instance must be created before using the driver and

examples/mongocxx/mongodb.com/index_examples.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
#include <mongocxx/client.hpp>
1818
#include <mongocxx/instance.hpp>
1919

20-
// NOTE: Any time this file is modified, a DOCS ticket should be opened to sync the changes with the
21-
// corresponding page on mongodb.com/docs. See CXX-1514, CXX-1249, and DRIVERS-356 for more info.
20+
namespace {
2221

2322
void index_examples(const mongocxx::database& db) {
2423
{
@@ -39,6 +38,8 @@ void index_examples(const mongocxx::database& db) {
3938
}
4039
}
4140

41+
} // namespace
42+
4243
int main() {
4344
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
4445
// respectively. Therefore, a mongocxx::instance must be created before using the driver and

examples/mongocxx/mongodb.com/runcommand_examples.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
#include <mongocxx/client.hpp>
2020
#include <mongocxx/instance.hpp>
2121

22-
// NOTE: Any time this file is modified, a DOCS ticket should be opened to sync the changes with the
23-
// corresponding page on mongodb.com/docs. See CXX-1514, CXX-1249, and DRIVERS-356 for more info.
22+
namespace {
2423

2524
void runcommand_examples(mongocxx::database& db) {
2625
{
@@ -29,12 +28,14 @@ void runcommand_examples(mongocxx::database& db) {
2928
auto buildInfo = db.run_command(make_document(kvp("buildInfo", 1)));
3029
// End runCommand Example 1
3130

32-
if (buildInfo.view()["ok"].get_double() != double{1}) {
31+
if (buildInfo.view()["ok"].get_double() != 1.0) {
3332
throw std::logic_error("buildInfo command failed in runCommand example 1");
3433
}
3534
}
3635
}
3736

37+
} // namespace
38+
3839
int main() {
3940
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
4041
// respectively. Therefore, a mongocxx::instance must be created before using the driver and

0 commit comments

Comments
 (0)