From e0caad916cfec06a780dda5ceec9eb96c4150fb5 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Tue, 26 Nov 2024 21:30:19 +0100 Subject: [PATCH 1/2] Fix compiler warnings run-clang-tidy -fix -checks=bugprone-assert-side-effect,hicpp-* --- examples/fibonacci.cpp | 3 +- .../beman/inplace_vector/inplace_vector.hpp | 54 +++++++++---------- .../inplace_vector/inplace_vector.test.cpp | 4 +- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/examples/fibonacci.cpp b/examples/fibonacci.cpp index 2757f11..2f1b330 100644 --- a/examples/fibonacci.cpp +++ b/examples/fibonacci.cpp @@ -42,7 +42,8 @@ template inplace_vector fibonacci_to(int num) { */ int main() { auto fib_seq = fibonacci_to<50>(10); - for (auto i = 0u; i < fib_seq.size(); ++i) + for (auto i = 0U; i < fib_seq.size(); ++i) { std::cout << i << ": " << fib_seq[i] << "\n"; + } std::cout << std::endl; } diff --git a/include/beman/inplace_vector/inplace_vector.hpp b/include/beman/inplace_vector/inplace_vector.hpp index 70fbf8b..6d6e6a7 100644 --- a/include/beman/inplace_vector/inplace_vector.hpp +++ b/include/beman/inplace_vector/inplace_vector.hpp @@ -335,7 +335,7 @@ class inplace_vector : public inplace_vector_base { for (; first != last; ++first) { emplace_back(*first); } - }; // freestanding-deleted + } // freestanding-deleted #if defined(__cpp_lib_containers_ranges) and defined(__cpp_concepts) template @@ -359,7 +359,7 @@ class inplace_vector : public inplace_vector_base { for (; first != last; ++first) { emplace_back(*first); } - }; // freestanding-deleted + } // freestanding-deleted #endif constexpr void assign(size_type n, const T &u) { @@ -434,7 +434,7 @@ class inplace_vector : public inplace_vector_base { constexpr reverse_iterator rbegin() noexcept { return reverse_iterator{this->end()}; - }; + } constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator{this->end()}; } @@ -452,13 +452,13 @@ class inplace_vector : public inplace_vector_base { constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator{this->begin()}; - }; + } // [containers.sequences.inplace.vector.members] size/capacity - constexpr bool empty() const noexcept { return size() == 0; }; - constexpr size_type size() const noexcept { return this->size_; }; - static constexpr size_type max_size() noexcept { return Capacity; }; - static constexpr size_type capacity() noexcept { return Capacity; }; + constexpr bool empty() const noexcept { return size() == 0; } + constexpr size_type size() const noexcept { return this->size_; } + static constexpr size_type max_size() noexcept { return Capacity; } + static constexpr size_type capacity() noexcept { return Capacity; } constexpr void resize(size_type sz) { const auto diff = static_cast(sz - this->size()); if (diff < 0) { @@ -471,7 +471,7 @@ class inplace_vector : public inplace_vector_base { base::uninitialized_value_construct(end, end + diff); } this->change_size(diff); - }; // freestanding-deleted + } // freestanding-deleted constexpr void resize(size_type sz, const T &c) { const auto diff = static_cast(sz - this->size()); if (diff < 0) { @@ -484,12 +484,12 @@ class inplace_vector : public inplace_vector_base { std::uninitialized_fill(end, end + diff, c); } this->change_size(diff); - }; // freestanding-deleted + } // freestanding-deleted static constexpr void reserve(size_type sz) { if (Capacity < sz) { throw std::bad_alloc(); } - }; // freestanding-deleted + } // freestanding-deleted static constexpr void shrink_to_fit() noexcept {} // [containers.sequences.inplace.vector.modifiers], modifiers @@ -498,20 +498,20 @@ class inplace_vector : public inplace_vector_base { throw std::bad_alloc(); } return this->unchecked_emplace_back(std::forward(args)...); - }; // freestanding-deleted + } // freestanding-deleted constexpr reference push_back(const T &x) { if (this->size() == Capacity) { throw std::bad_alloc(); } return this->unchecked_emplace_back(x); - }; // freestanding-deleted + } // freestanding-deleted constexpr reference push_back(T &&x) { if (this->size() == Capacity) { throw std::bad_alloc(); } return this->unchecked_emplace_back(std::move(x)); - }; // freestanding-deleted + } // freestanding-deleted #if defined(__cpp_lib_containers_ranges) and defined(__cpp_concepts) template @@ -522,7 +522,7 @@ class inplace_vector : public inplace_vector_base { for (; first != last; ++first) { emplace_back(*first); } - }; // freestanding-deleted + } // freestanding-deleted #endif constexpr void pop_back() { @@ -531,7 +531,7 @@ class inplace_vector : public inplace_vector_base { std::destroy(end, end + 1); this->change_size(-1); } - }; + } template constexpr pointer try_emplace_back(Args &&...args) { if (this->size() == Capacity) { @@ -539,14 +539,14 @@ class inplace_vector : public inplace_vector_base { } return std::addressof( this->unchecked_emplace_back(std::forward(args)...)); - }; + } constexpr pointer try_push_back(const T &x) noexcept(std::is_nothrow_copy_constructible_v) { if (this->size() == Capacity) { return nullptr; } return std::addressof(this->unchecked_emplace_back(x)); - }; + } constexpr pointer try_push_back(T &&x) noexcept(std::is_nothrow_move_constructible_v) { @@ -554,7 +554,7 @@ class inplace_vector : public inplace_vector_base { return nullptr; } return std::addressof(this->unchecked_emplace_back(std::move(x))); - }; + } /* template @@ -566,7 +566,7 @@ class inplace_vector : public inplace_vector_base { emplace_back(*first); } return frist; - }; + } */ template @@ -580,15 +580,15 @@ class inplace_vector : public inplace_vector_base { #endif this->change_size(1); return *final; - }; + } constexpr reference unchecked_push_back(const T &x) noexcept(std::is_nothrow_constructible_v) { return this->unchecked_emplace_back(x); - }; + } constexpr reference unchecked_push_back(T &&x) { return this->unchecked_emplace_back(std::move(x)); - }; + } template constexpr iterator emplace(const_iterator position, Args &&...args) { @@ -609,13 +609,13 @@ class inplace_vector : public inplace_vector_base { *pos = std::move(temp); } return pos; - }; // freestanding-deleted + } // freestanding-deleted constexpr iterator insert(const_iterator position, const T &x) { return emplace(position, x); - }; // freestanding-deleted + } // freestanding-deleted constexpr iterator insert(const_iterator position, T &&x) { return emplace(position, std::move(x)); - }; // freestanding-deleted + } // freestanding-deleted constexpr iterator insert(const_iterator position, size_type n, const T &x) { const iterator pos = position; const iterator end = this->end(); @@ -781,7 +781,7 @@ class inplace_vector : public inplace_vector_base { constexpr friend std::compare_three_way_result operator<=>(const inplace_vector &x, const inplace_vector &y) { return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); - }; + } #endif constexpr friend void swap(inplace_vector &x, inplace_vector &y) noexcept( diff --git a/tests/beman/inplace_vector/inplace_vector.test.cpp b/tests/beman/inplace_vector/inplace_vector.test.cpp index dd09737..28e9b85 100644 --- a/tests/beman/inplace_vector/inplace_vector.test.cpp +++ b/tests/beman/inplace_vector/inplace_vector.test.cpp @@ -31,7 +31,7 @@ template constexpr void test() { static_assert(std::is_same::value, ""); - assert(front = T(1)); + assert(front == T(1)); auto &&const_front = const_range.front(); static_assert( @@ -69,6 +69,7 @@ void test_exceptions() { try { vec too_small{}; auto res = too_small.at(5); + (void)res; } catch (const std::out_of_range &) { } catch (...) { assert(false); @@ -76,6 +77,7 @@ void test_exceptions() { try { const vec too_small{}; auto res = too_small.at(5); + (void)res; } catch (const std::out_of_range &) { } catch (...) { assert(false); From 61462492cb483cc0bed28afe16b290ff5d0b7e0a Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Tue, 26 Nov 2024 22:41:19 +0100 Subject: [PATCH 2/2] Fix more warnings and errors --- include/beman/inplace_vector/inplace_vector.hpp | 2 +- tests/beman/inplace_vector/inplace_vector.test.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/include/beman/inplace_vector/inplace_vector.hpp b/include/beman/inplace_vector/inplace_vector.hpp index 6d6e6a7..6feafe5 100644 --- a/include/beman/inplace_vector/inplace_vector.hpp +++ b/include/beman/inplace_vector/inplace_vector.hpp @@ -74,7 +74,7 @@ struct inplace_vector_destruct_base { inplace_vector_destruct_base( const inplace_vector_destruct_base &&other) noexcept(std::is_nothrow_move_constructible_v) - : elems(), size_(other.size()) {} + : elems(), size_(other.size_) {} inplace_vector_destruct_base & operator=(const inplace_vector_destruct_base &other) noexcept( diff --git a/tests/beman/inplace_vector/inplace_vector.test.cpp b/tests/beman/inplace_vector/inplace_vector.test.cpp index 28e9b85..d4b7372 100644 --- a/tests/beman/inplace_vector/inplace_vector.test.cpp +++ b/tests/beman/inplace_vector/inplace_vector.test.cpp @@ -32,6 +32,7 @@ template constexpr void test() { typename vec::const_reference>::value, ""); assert(front == T(1)); + (void)front; auto &&const_front = const_range.front(); static_assert(