mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
Fix some corechecker warnings (#470)
* Improve const correctness in string_span * Improve const correctness in bounds_tests.cpp and byte_tests.cpp * Improve const correctness in span_tests.cpp * Improve const correctness in utils_tests.cpp * Use gsl::owner for dynamically allocated memory in string_span_tests.cpp * Improve const correctness in string_span_tests.cpp * Improve const correctness for strided_span_tests.cpp
This commit is contained in:
parent
3300602653
commit
ade86caa92
@ -577,7 +577,7 @@ template <class CharT, std::ptrdiff_t Extent, class T,
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>>
|
||||
bool operator==(const gsl::basic_string_span<CharT, Extent>& one, const T& other) GSL_NOEXCEPT
|
||||
{
|
||||
gsl::basic_string_span<std::add_const_t<CharT>> tmp(other);
|
||||
const gsl::basic_string_span<std::add_const_t<CharT>> tmp(other);
|
||||
#ifdef GSL_MSVC_NO_CPP14_STD_EQUAL
|
||||
return (one.size() == tmp.size()) && std::equal(one.begin(), one.end(), tmp.begin());
|
||||
#else
|
||||
@ -624,7 +624,7 @@ template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename
|
||||
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
|
||||
bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other) GSL_NOEXCEPT
|
||||
{
|
||||
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
|
||||
const gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
|
||||
return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end());
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,7 @@ SUITE(bounds_test)
|
||||
TEST(bounds_basic)
|
||||
{
|
||||
static_bounds<3, 4, 5> b;
|
||||
auto a = b.slice();
|
||||
const auto a = b.slice();
|
||||
(void)a;
|
||||
static_bounds<4, dynamic_range, 2> x{ 4 };
|
||||
x.slice().slice();
|
||||
@ -55,7 +55,7 @@ SUITE(bounds_test)
|
||||
{
|
||||
static_bounds<4, dynamic_range, 2> bounds{ 3 };
|
||||
|
||||
auto itr = bounds.begin();
|
||||
const auto itr = bounds.begin();
|
||||
(void)itr;
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
multi_span<int, 4, dynamic_range, 2> av(nullptr, bounds);
|
||||
|
@ -35,22 +35,22 @@ SUITE(byte_tests)
|
||||
TEST(construction)
|
||||
{
|
||||
{
|
||||
byte b = static_cast<byte>(4);
|
||||
const byte b = static_cast<byte>(4);
|
||||
CHECK(static_cast<unsigned char>(b) == 4);
|
||||
}
|
||||
|
||||
{
|
||||
byte b = byte(12);
|
||||
const byte b = byte(12);
|
||||
CHECK(static_cast<unsigned char>(b) == 12);
|
||||
}
|
||||
|
||||
{
|
||||
byte b = to_byte<12>();
|
||||
const byte b = to_byte<12>();
|
||||
CHECK(static_cast<unsigned char>(b) == 12);
|
||||
}
|
||||
{
|
||||
unsigned char uc = 12;
|
||||
byte b = to_byte(uc);
|
||||
const unsigned char uc = 12;
|
||||
const byte b = to_byte(uc);
|
||||
CHECK(static_cast<unsigned char>(b) == 12);
|
||||
}
|
||||
|
||||
@ -63,7 +63,7 @@ SUITE(byte_tests)
|
||||
|
||||
TEST(bitwise_operations)
|
||||
{
|
||||
byte b = to_byte<0xFF>();
|
||||
const byte b = to_byte<0xFF>();
|
||||
|
||||
byte a = to_byte<0x00>();
|
||||
CHECK((b | a) == to_byte<0xFF>());
|
||||
@ -99,7 +99,7 @@ SUITE(byte_tests)
|
||||
|
||||
TEST(to_integer)
|
||||
{
|
||||
byte b = to_byte<0x12>();
|
||||
const byte b = to_byte<0x12>();
|
||||
|
||||
CHECK(0x12 == gsl::to_integer<char>(b));
|
||||
CHECK(0x12 == gsl::to_integer<short>(b));
|
||||
@ -125,7 +125,7 @@ SUITE(byte_tests)
|
||||
TEST(aliasing)
|
||||
{
|
||||
int i{ 0 };
|
||||
int res = modify_both(reinterpret_cast<byte&>(i), i);
|
||||
const int res = modify_both(reinterpret_cast<byte&>(i), i);
|
||||
CHECK(res == i);
|
||||
}
|
||||
}
|
||||
|
@ -930,7 +930,7 @@ SUITE(span_tests)
|
||||
CHECK(av.subspan(4).length() == 1);
|
||||
CHECK(av.subspan(5).length() == 0);
|
||||
CHECK_THROW(av.subspan(6).length(), fail_fast);
|
||||
auto av2 = av.subspan(1);
|
||||
const auto av2 = av.subspan(1);
|
||||
for (int i = 0; i < 4; ++i) CHECK(av2[i] == i + 2);
|
||||
}
|
||||
|
||||
@ -941,7 +941,7 @@ SUITE(span_tests)
|
||||
CHECK(av.subspan(4).length() == 1);
|
||||
CHECK(av.subspan(5).length() == 0);
|
||||
CHECK_THROW(av.subspan(6).length(), fail_fast);
|
||||
auto av2 = av.subspan(1);
|
||||
const auto av2 = av.subspan(1);
|
||||
for (int i = 0; i < 4; ++i) CHECK(av2[i] == i + 2);
|
||||
}
|
||||
}
|
||||
@ -1117,7 +1117,7 @@ SUITE(span_tests)
|
||||
CHECK(it == beyond);
|
||||
CHECK(it - beyond == 0);
|
||||
|
||||
for (auto& n : s)
|
||||
for (const auto& n : s)
|
||||
{
|
||||
CHECK(n == 5);
|
||||
}
|
||||
@ -1214,7 +1214,7 @@ SUITE(span_tests)
|
||||
CHECK(it == beyond);
|
||||
CHECK(it - beyond == 0);
|
||||
|
||||
for (auto& n : s)
|
||||
for (const auto& n : s)
|
||||
{
|
||||
CHECK(n == 5);
|
||||
}
|
||||
@ -1386,16 +1386,16 @@ SUITE(span_tests)
|
||||
int a[] = {1, 2, 3, 4};
|
||||
|
||||
{
|
||||
span<const int> s = a;
|
||||
const span<const int> s = a;
|
||||
CHECK(s.length() == 4);
|
||||
span<const byte> bs = as_bytes(s);
|
||||
const span<const byte> bs = as_bytes(s);
|
||||
CHECK(static_cast<const void*>(bs.data()) == static_cast<const void*>(s.data()));
|
||||
CHECK(bs.length() == s.length_bytes());
|
||||
}
|
||||
|
||||
{
|
||||
span<int> s;
|
||||
auto bs = as_bytes(s);
|
||||
const auto bs = as_bytes(s);
|
||||
CHECK(bs.length() == s.length());
|
||||
CHECK(bs.length() == 0);
|
||||
CHECK(bs.size_bytes() == 0);
|
||||
@ -1405,7 +1405,7 @@ SUITE(span_tests)
|
||||
|
||||
{
|
||||
span<int> s = a;
|
||||
auto bs = as_bytes(s);
|
||||
const auto bs = as_bytes(s);
|
||||
CHECK(static_cast<const void*>(bs.data()) == static_cast<const void*>(s.data()));
|
||||
CHECK(bs.length() == s.length_bytes());
|
||||
}
|
||||
@ -1428,7 +1428,7 @@ SUITE(span_tests)
|
||||
|
||||
{
|
||||
span<int> s;
|
||||
auto bs = as_writeable_bytes(s);
|
||||
const auto bs = as_writeable_bytes(s);
|
||||
CHECK(bs.length() == s.length());
|
||||
CHECK(bs.length() == 0);
|
||||
CHECK(bs.size_bytes() == 0);
|
||||
@ -1438,7 +1438,7 @@ SUITE(span_tests)
|
||||
|
||||
{
|
||||
span<int> s = a;
|
||||
auto bs = as_writeable_bytes(s);
|
||||
const auto bs = as_writeable_bytes(s);
|
||||
CHECK(static_cast<void*>(bs.data()) == static_cast<void*>(s.data()));
|
||||
CHECK(bs.length() == s.length_bytes());
|
||||
}
|
||||
@ -1484,11 +1484,11 @@ SUITE(span_tests)
|
||||
|
||||
// you can convert statically
|
||||
{
|
||||
span<int, 2> s2 = {arr, 2};
|
||||
const span<int, 2> s2 = {arr, 2};
|
||||
static_cast<void>(s2);
|
||||
}
|
||||
{
|
||||
span<int, 1> s1 = s4.first<1>();
|
||||
const span<int, 1> s1 = s4.first<1>();
|
||||
static_cast<void>(s1);
|
||||
}
|
||||
|
||||
@ -1532,7 +1532,7 @@ SUITE(span_tests)
|
||||
{
|
||||
char lat[] = { '1', '2', '3', '4', '5', '6', 'E', 'F', 'G' };
|
||||
span<char> s = lat;
|
||||
auto f_it = s.begin() + 7;
|
||||
const auto f_it = s.begin() + 7;
|
||||
|
||||
std::match_results<span<char>::iterator> match;
|
||||
|
||||
|
@ -39,9 +39,9 @@ SUITE(strided_span_tests)
|
||||
{
|
||||
int a[30][4][5];
|
||||
|
||||
auto av = as_multi_span(a);
|
||||
auto sub = av.section({15, 0, 0}, gsl::index<3>{2, 2, 2});
|
||||
auto subsub = sub.section({1, 0, 0}, gsl::index<3>{1, 1, 1});
|
||||
const auto av = as_multi_span(a);
|
||||
const auto sub = av.section({15, 0, 0}, gsl::index<3>{2, 2, 2});
|
||||
const auto subsub = sub.section({1, 0, 0}, gsl::index<3>{1, 1, 1});
|
||||
(void)subsub;
|
||||
}
|
||||
|
||||
@ -51,13 +51,13 @@ SUITE(strided_span_tests)
|
||||
std::iota(begin(data), end(data), 0);
|
||||
const multi_span<int, 5, 10> av = as_multi_span(multi_span<int>{data}, dim<5>(), dim<10>());
|
||||
|
||||
strided_span<int, 2> av_section_1 = av.section({ 1, 2 }, { 3, 4 });
|
||||
const strided_span<int, 2> av_section_1 = av.section({ 1, 2 }, { 3, 4 });
|
||||
CHECK((av_section_1[{0, 0}] == 12));
|
||||
CHECK((av_section_1[{0, 1}] == 13));
|
||||
CHECK((av_section_1[{1, 0}] == 22));
|
||||
CHECK((av_section_1[{2, 3}] == 35));
|
||||
|
||||
strided_span<int, 2> av_section_2 = av_section_1.section({ 1, 2 }, { 2,2 });
|
||||
const strided_span<int, 2> av_section_2 = av_section_1.section({ 1, 2 }, { 2,2 });
|
||||
CHECK((av_section_2[{0, 0}] == 24));
|
||||
CHECK((av_section_2[{0, 1}] == 25));
|
||||
CHECK((av_section_2[{1, 0}] == 34));
|
||||
@ -530,7 +530,7 @@ SUITE(strided_span_tests)
|
||||
CHECK_THROW(empty_sav.begin()[0], fail_fast);
|
||||
CHECK_THROW(empty_sav.cbegin()[0], fail_fast);
|
||||
|
||||
for (auto& v : empty_sav)
|
||||
for (const auto& v : empty_sav)
|
||||
{
|
||||
(void)v;
|
||||
CHECK(false);
|
||||
@ -545,7 +545,7 @@ SUITE(strided_span_tests)
|
||||
CHECK_THROW(empty_sav.begin()[0], fail_fast);
|
||||
CHECK_THROW(empty_sav.cbegin()[0], fail_fast);
|
||||
|
||||
for (auto& v : empty_sav)
|
||||
for (const auto& v : empty_sav)
|
||||
{
|
||||
(void)v;
|
||||
CHECK(false);
|
||||
@ -614,7 +614,7 @@ SUITE(strided_span_tests)
|
||||
|
||||
void iterate_second_slice(multi_span<int, dynamic_range, dynamic_range, dynamic_range> av)
|
||||
{
|
||||
int expected[6] = {2,3,10,11,18,19};
|
||||
const int expected[6] = {2,3,10,11,18,19};
|
||||
auto section = av.section({0,1,0}, {3,1,2});
|
||||
|
||||
for (auto i = 0; i < section.extent<0>(); ++i)
|
||||
@ -635,7 +635,7 @@ SUITE(strided_span_tests)
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (auto num : section)
|
||||
for (const auto num : section)
|
||||
{
|
||||
CHECK(num == expected[i]);
|
||||
i++;
|
||||
@ -644,7 +644,7 @@ SUITE(strided_span_tests)
|
||||
|
||||
TEST(strided_span_section_iteration_3d)
|
||||
{
|
||||
int arr[3][4][2];
|
||||
int arr[3][4][2]{};
|
||||
for (auto i = 0; i < 3; ++i)
|
||||
{
|
||||
for (auto j = 0; j < 4; ++j)
|
||||
@ -660,8 +660,8 @@ SUITE(strided_span_tests)
|
||||
|
||||
TEST(dynamic_strided_span_section_iteration_3d)
|
||||
{
|
||||
auto height = 12, width = 2;
|
||||
auto size = height * width;
|
||||
const auto height = 12, width = 2;
|
||||
const auto size = height * width;
|
||||
|
||||
auto arr = new int[static_cast<std::size_t>(size)];
|
||||
for (auto i = 0; i < size; ++i)
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
#include <cstdlib>
|
||||
#include <gsl/string_span>
|
||||
#include <gsl/gsl> //owner
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
@ -229,7 +230,7 @@ SUITE(string_span_tests)
|
||||
const char ar2[10] = "Hello";
|
||||
const std::string str = "Hello";
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
gsl::span<const char> sp = ensure_z("Hello");
|
||||
const gsl::span<const char> sp = ensure_z("Hello");
|
||||
|
||||
cstring_span<> span = "Hello";
|
||||
|
||||
@ -441,7 +442,7 @@ SUITE(string_span_tests)
|
||||
|
||||
// ensure z on c strings
|
||||
{
|
||||
char* ptr = new char[3];
|
||||
gsl::owner<char*> ptr = new char[3];
|
||||
|
||||
ptr[0] = 'a';
|
||||
ptr[1] = 'b';
|
||||
@ -553,52 +554,52 @@ SUITE(string_span_tests)
|
||||
// from const string
|
||||
{
|
||||
const std::string str = "Hello";
|
||||
cstring_span<> span = str;
|
||||
const cstring_span<> span = str;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from non-const string
|
||||
{
|
||||
std::string str = "Hello";
|
||||
cstring_span<> span = str;
|
||||
const cstring_span<> span = str;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from const vector
|
||||
{
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
cstring_span<> span = vec;
|
||||
const cstring_span<> span = vec;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from non-const vector
|
||||
{
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
cstring_span<> span = vec;
|
||||
const cstring_span<> span = vec;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from const span
|
||||
{
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const span<const char> inner = vec;
|
||||
cstring_span<> span = inner;
|
||||
const cstring_span<> span = inner;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from non-const span
|
||||
{
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
span<char> inner = vec;
|
||||
cstring_span<> span = inner;
|
||||
const span<char> inner = vec;
|
||||
const cstring_span<> span = inner;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from const string_span
|
||||
{
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
cstring_span<> tmp = vec;
|
||||
cstring_span<> span = tmp;
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const cstring_span<> tmp = vec;
|
||||
const cstring_span<> span = tmp;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
@ -725,8 +726,8 @@ SUITE(string_span_tests)
|
||||
// from non-const string_span
|
||||
{
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
string_span<> tmp = vec;
|
||||
string_span<> span = tmp;
|
||||
const string_span<> tmp = vec;
|
||||
const string_span<> span = tmp;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
@ -744,7 +745,7 @@ SUITE(string_span_tests)
|
||||
{
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const string_span<> tmp = vec;
|
||||
string_span<> span = tmp;
|
||||
const string_span<> span = tmp;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
}
|
||||
@ -766,29 +767,29 @@ SUITE(string_span_tests)
|
||||
// move string_span
|
||||
{
|
||||
cstring_span<> span = "Hello";
|
||||
auto span1 = std::move(span);
|
||||
const auto span1 = std::move(span);
|
||||
CHECK(span1.length() == 5);
|
||||
}
|
||||
{
|
||||
cstring_span<> span = "Hello";
|
||||
auto span1 = move_wrapper(std::move(span));
|
||||
const auto span1 = move_wrapper(std::move(span));
|
||||
CHECK(span1.length() == 5);
|
||||
}
|
||||
{
|
||||
cstring_span<> span = "Hello";
|
||||
auto span1 = move_wrapper(std::move(span));
|
||||
const auto span1 = move_wrapper(std::move(span));
|
||||
CHECK(span1.length() == 5);
|
||||
}
|
||||
|
||||
// move span
|
||||
{
|
||||
span<const char> span = ensure_z("Hello");
|
||||
cstring_span<> span1 = std::move(span);
|
||||
const cstring_span<> span1 = std::move(span);
|
||||
CHECK(span1.length() == 5);
|
||||
}
|
||||
{
|
||||
span<const char> span = ensure_z("Hello");
|
||||
cstring_span<> span2 = move_wrapper(std::move(span));
|
||||
const cstring_span<> span2 = move_wrapper(std::move(span));
|
||||
CHECK(span2.length() == 5);
|
||||
}
|
||||
|
||||
@ -939,7 +940,7 @@ SUITE(string_span_tests)
|
||||
wchar_t buf[1];
|
||||
buf[0] = L'a';
|
||||
|
||||
auto workaround_macro = [&]() { wzstring_span<> zspan({ buf, 1 }); };
|
||||
const auto workaround_macro = [&]() { wzstring_span<> zspan({ buf, 1 }); };
|
||||
CHECK_THROW(workaround_macro(), fail_fast);
|
||||
}
|
||||
|
||||
@ -947,7 +948,7 @@ SUITE(string_span_tests)
|
||||
{
|
||||
wchar_t buf[10];
|
||||
|
||||
auto name = CreateTempNameW({ buf, 10 });
|
||||
const auto name = CreateTempNameW({ buf, 10 });
|
||||
if (!name.empty())
|
||||
{
|
||||
cwzstring<> str = name.assume_z();
|
||||
|
@ -92,7 +92,7 @@ SUITE(utils_tests)
|
||||
TEST(narrow)
|
||||
{
|
||||
int n = 120;
|
||||
char c = narrow<char>(n);
|
||||
const char c = narrow<char>(n);
|
||||
CHECK(c == 120);
|
||||
|
||||
n = 300;
|
||||
|
Loading…
Reference in New Issue
Block a user