Skip to content

Commit 4c37862

Browse files
committed
CXX-788 Fix some -Wall and -Wextra warnings under clang and gcc
1 parent 6bc1103 commit 4c37862

22 files changed

+127
-84
lines changed

examples/bsoncxx/builder_basic.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include <cstdlib>
16+
1517
#include <bsoncxx/builder/basic/array.hpp>
1618
#include <bsoncxx/builder/basic/document.hpp>
1719
#include <bsoncxx/builder/basic/kvp.hpp>
@@ -70,4 +72,7 @@ int main(int, char**) {
7072

7173
// We can get a view of the resulting bson by calling view()
7274
auto v = doc.view();
75+
76+
// Use 'v' so we don't get compiler warnings.
77+
return v.empty() ? EXIT_FAILURE : EXIT_SUCCESS;
7378
}

examples/bsoncxx/getting_values.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include <cstdlib>
16+
1517
#include <bsoncxx/builder/stream/array.hpp>
1618
#include <bsoncxx/builder/stream/document.hpp>
1719
#include <bsoncxx/builder/stream/helpers.hpp>
@@ -86,4 +88,8 @@ int main(int, char**) {
8688
} catch (const std::runtime_error&) {
8789
// Trying to index into a non-array throws.
8890
};
91+
92+
// Make all variables used.
93+
return (awards && first_award_year && second_award_year && last_name) ?
94+
EXIT_SUCCESS : EXIT_FAILURE;
8995
}

src/bsoncxx/document/element.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,21 @@ element::element(const std::uint8_t* raw, std::uint32_t length, std::uint32_t of
5353
: _raw(raw), _length(length), _offset(offset) {
5454
}
5555

56-
const std::uint8_t* const element::raw() const {
56+
const std::uint8_t* element::raw() const {
5757
return _raw;
5858
}
5959
void element::raw(const std::uint8_t* raw) {
6060
_raw = raw;
6161
}
6262

63-
const std::uint32_t element::length() const {
63+
std::uint32_t element::length() const {
6464
return _length;
6565
}
6666
void element::length(std::uint32_t length) {
6767
_length = length;
6868
}
6969

70-
const std::uint32_t element::offset() const {
70+
std::uint32_t element::offset() const {
7171
return _offset;
7272
}
7373
void element::offset(std::uint32_t offset) {

src/bsoncxx/document/element.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ class BSONCXX_API element {
6969

7070
explicit operator bool() const;
7171

72-
const std::uint8_t* const raw() const;
72+
const std::uint8_t* raw() const;
7373
void raw(const std::uint8_t*);
7474

75-
const std::uint32_t length() const;
75+
std::uint32_t length() const;
7676
void length(std::uint32_t);
7777

78-
const std::uint32_t offset() const;
78+
std::uint32_t offset() const;
7979
void offset(std::uint32_t);
8080

8181
bsoncxx::type type() const;

src/bsoncxx/private/error_category.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class bsoncxx_error_category_impl final : public std::error_category {
6464
return "can't convert builder to a valid view: unmatched key";
6565
case error_code::k_unset_element:
6666
return "unset document::element";
67+
default:
68+
return "unknown bsoncxx error code";
6769
}
6870
}
6971
};
@@ -120,7 +122,7 @@ class libbson_unknown_error final : public std::error_category {
120122
return "libbson unknown error";
121123
}
122124

123-
std::string message(int code) const noexcept override {
125+
std::string message(int) const noexcept override {
124126
return "unknown libbson error in unknown domain";
125127
}
126128
};

src/bsoncxx/test/bson_validate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ TEST_CASE("validate accepts bson we produce", "[bsoncxx::validate]") {
3636
}
3737

3838
TEST_CASE("validate doesn't accept random bytes", "[bsoncxx::validate]") {
39-
std::array<uint8_t, 12> arr{0xDE, 0xAD, 0xBE, 0xEF, 0xF0, 0x0B, 0x45};
39+
std::array<uint8_t, 12> arr{{0xDE, 0xAD, 0xBE, 0xEF, 0xF0, 0x0B, 0x45}};
4040
REQUIRE(is_disengaged(validate(arr.data(), arr.size())));
4141
}
4242

src/mongocxx/collection.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <chrono>
1818
#include <cstdint>
19+
#include <limits>
1920
#include <tuple>
2021
#include <utility>
2122

@@ -219,7 +220,10 @@ cursor collection::find(view_or_value filter, const options::find& options) {
219220
projection.bson(), rp_ptr);
220221

221222
if (options.max_await_time()) {
222-
libmongoc::cursor_set_max_await_time_ms(mongoc_cursor, (*options.max_await_time()).count());
223+
const auto count = options.max_await_time()->count();
224+
if ((count < 0) || (count >= std::numeric_limits<std::uint32_t>::max()))
225+
throw logic_error{make_error_code(error_code::k_invalid_parameter)};
226+
libmongoc::cursor_set_max_await_time_ms(mongoc_cursor, static_cast<std::uint32_t>(count));
223227
}
224228

225229
return cursor{mongoc_cursor};

src/mongocxx/config/postlude.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,7 @@
4949
#pragma pop_macro("MONGOCXX_NO_DEPRECATED")
5050
#undef MONGOCXX_PRIVATE
5151
#pragma pop_macro("MONGOCXX_PRIVATE")
52+
53+
// prelude.hpp
54+
#undef MONGOCXX_UNREACHABLE
55+
#pragma pop_macro("MONGOCXX_UNREACHABLE")

src/mongocxx/config/prelude.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@
5454
#include <mongocxx/config/config.hpp>
5555
#include <mongocxx/config/version.hpp>
5656
#include <mongocxx/export.hpp>
57+
58+
// TODO: Find a way to DRY this with BSONCXX_UNREACHABLE
59+
#pragma push_macro("MONGOCXX_UNREACHABLE")
60+
#undef MONGOCXX_UNREACHABLE
61+
#define MONGOCXX_UNREACHABLE std::abort()

src/mongocxx/exception/error_code.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ namespace mongocxx {
2222
MONGOCXX_INLINE_NAMESPACE_BEGIN
2323

2424
enum class error_code : std::int32_t {
25-
k_invalid_client_object = 1,
26-
k_invalid_collection_object,
27-
k_invalid_database_object,
25+
k_invalid_client_object = 1,
26+
k_invalid_collection_object,
27+
k_invalid_database_object,
28+
k_invalid_parameter,
2829
};
2930

3031
MONGOCXX_INLINE_NAMESPACE_END

src/mongocxx/exception/private/error_category.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class mongoc_insert_error_category final : public std::error_category {
167167
return "libmongoc insert error";
168168
}
169169

170-
std::string message(int code) const noexcept override {
170+
std::string message(int) const noexcept override {
171171
return "unknown mongoc insert error";
172172
}
173173
};
@@ -267,7 +267,7 @@ class mongoc_sasl_error_category final : public std::error_category {
267267
return "libmongoc sasl error";
268268
}
269269

270-
std::string message(int code) const noexcept override {
270+
std::string message(int) const noexcept override {
271271
return "unknown mongoc sasl error";
272272
}
273273
};
@@ -354,7 +354,7 @@ class mongoc_write_concern_error_category final : public std::error_category {
354354
return "libmongoc write concern error";
355355
}
356356

357-
std::string message(int code) const noexcept override {
357+
std::string message(int) const noexcept override {
358358
return "unknown mongoc write concern error";
359359
}
360360
};
@@ -368,7 +368,7 @@ class mongoc_unknown_error_category final : public std::error_category {
368368
return "libmongoc unknown error";
369369
}
370370

371-
std::string message(int code) const noexcept override {
371+
std::string message(int) const noexcept override {
372372
return "unknown mongoc error";
373373
}
374374
};
@@ -391,6 +391,8 @@ class mongocxx_error_category_impl final : public std::error_category {
391391
"object";
392392
case error_code::k_invalid_database_object:
393393
return "invalid use of default constructed or moved-from mongocxx::database object";
394+
case error_code::k_invalid_parameter:
395+
return "an invalid or out-of-bounds parameter was provided";
394396
default:
395397
return "unknown mongocxx error";
396398
}

src/mongocxx/mock/mock.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class mock<R (*)(Args...)> {
8686
std::array<R, sizeof...(rs)+1> vec = {r, rs...};
8787
std::size_t i = 0;
8888

89-
_callbacks.emplace([vec, i](Args... args) mutable -> R {
89+
_callbacks.emplace([vec, i](Args...) mutable -> R {
9090
if (i == vec.size()) {
9191
i = 0;
9292
}

src/mongocxx/options/index.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ const stdx::optional<std::string>& index::wiredtiger_storage_options::config_str
168168
return _config_string;
169169
}
170170

171-
const int index::wiredtiger_storage_options::type() const {
171+
int index::wiredtiger_storage_options::type() const {
172172
return mongoc_index_storage_opt_type_t::MONGOC_INDEX_STORAGE_OPT_WIREDTIGER;
173173
}
174174

src/mongocxx/options/index.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class MONGOCXX_API index {
4545

4646
private:
4747
friend collection;
48-
MONGOCXX_PRIVATE virtual const int type() const = 0;
48+
MONGOCXX_PRIVATE virtual int type() const = 0;
4949
};
5050

5151
///
@@ -72,7 +72,7 @@ class MONGOCXX_API index {
7272

7373
private:
7474
friend collection;
75-
MONGOCXX_PRIVATE const int type() const override;
75+
MONGOCXX_PRIVATE int type() const override;
7676
stdx::optional<std::string> _config_string;
7777
};
7878

src/mongocxx/test/bulk_write.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ TEST_CASE("destruction of a bulk_write will destroy mongoc operation", "[bulk_wr
5151
auto destruct = libmongoc::bulk_operation_destroy.create_instance();
5252
bool destruct_called = false;
5353

54-
destruct->visit([&destruct_called](mongoc_bulk_operation_t* op) { destruct_called = true; });
54+
destruct->visit([&destruct_called](mongoc_bulk_operation_t*) { destruct_called = true; });
5555

5656
bulk_write(true);
5757
REQUIRE(destruct_called);
@@ -64,7 +64,7 @@ class SingleDocumentFun {
6464
_called = false;
6565
}
6666

67-
void operator()(mongoc_bulk_operation_t* bulk, const bson_t* document) {
67+
void operator()(mongoc_bulk_operation_t*, const bson_t* document) {
6868
_called = true;
6969
REQUIRE(bson_get_data(document) == _document.data());
7070
}

src/mongocxx/test/client.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ TEST_CASE("A client supports move operations", "[client]") {
7979
client a{uri{}};
8080

8181
bool called = false;
82-
client_new->interpose([&](const mongoc_uri_t* url) {
82+
client_new->interpose([&](const mongoc_uri_t*) {
8383
called = true;
8484
return nullptr;
8585
});
@@ -101,7 +101,7 @@ TEST_CASE("A client has a settable Read Concern", "[collection]") {
101101
rc.acknowledge_level(read_concern::level::k_majority);
102102

103103
client_set_read_concern->interpose(
104-
[&client_set_rc_called](::mongoc_client_t* coll, const ::mongoc_read_concern_t* rc_t) {
104+
[&client_set_rc_called](::mongoc_client_t*, const ::mongoc_read_concern_t* rc_t) {
105105
REQUIRE(rc_t);
106106
const auto result = libmongoc::read_concern_get_level(rc_t);
107107
REQUIRE(result);
@@ -123,15 +123,15 @@ TEST_CASE("A client's read preferences may be set and obtained", "[client]") {
123123
auto deleter = [](mongoc_read_prefs_t* var) { mongoc_read_prefs_destroy(var); };
124124
std::unique_ptr<mongoc_read_prefs_t, decltype(deleter)> saved_preference(nullptr, deleter);
125125

126-
client_set_preference->interpose([&](mongoc_client_t* client,
126+
client_set_preference->interpose([&](mongoc_client_t*,
127127
const mongoc_read_prefs_t* read_prefs) {
128128
called_set = true;
129129
saved_preference.reset(mongoc_read_prefs_copy(read_prefs));
130130
REQUIRE(mongoc_read_prefs_get_mode(read_prefs) ==
131131
static_cast<mongoc_read_mode_t>(read_preference::read_mode::k_secondary_preferred));
132132
});
133133

134-
client_get_preference->interpose([&](const mongoc_client_t* client) {
134+
client_get_preference->interpose([&](const mongoc_client_t*) {
135135
return saved_preference.get();
136136
}).forever();
137137

@@ -153,13 +153,13 @@ TEST_CASE("A client's write concern may be set and obtained", "[client]") {
153153

154154
bool set_called = false;
155155
client_set_concern->interpose(
156-
[&](mongoc_client_t* client, const mongoc_write_concern_t* concern) {
156+
[&](mongoc_client_t*, const mongoc_write_concern_t* concern) {
157157
set_called = true;
158158
underlying_wc = mongoc_write_concern_copy(concern);
159159
});
160160

161161
bool get_called = false;
162-
client_get_concern->interpose([&](const mongoc_client_t* client) {
162+
client_get_concern->interpose([&](const mongoc_client_t*) {
163163
get_called = true;
164164
return underlying_wc;
165165
});
@@ -169,7 +169,7 @@ TEST_CASE("A client's write concern may be set and obtained", "[client]") {
169169

170170
MOCK_CONCERN
171171
bool copy_called = false;
172-
concern_copy->interpose([&](const mongoc_write_concern_t* concern) {
172+
concern_copy->interpose([&](const mongoc_write_concern_t*) {
173173
copy_called = true;
174174
return mongoc_write_concern_copy(underlying_wc);
175175
});

src/mongocxx/test/collection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ TEST_CASE("CRUD functionality", "[driver::collection]") {
118118

119119
auto cursor = coll.find({});
120120

121-
std::size_t i = 0;
121+
std::int32_t i = 0;
122122
for (auto&& x : cursor) {
123123
i++;
124124
REQUIRE(x["x"].get_int32() == i);

0 commit comments

Comments
 (0)