THRIFT-4776:Modernize c++11 code by clang-tidy (#1732)
* use override
* use make_shared
* use emplace
* use range for
* fix error on MSVC
* replace boost functions with std functions
* fix static analyzer warnings
* check api return value
* initialize member
* check the return value of SSL_peek > 0
* add override
diff --git a/lib/cpp/test/TBufferBaseTest.cpp b/lib/cpp/test/TBufferBaseTest.cpp
index 430302c..7203f82 100644
--- a/lib/cpp/test/TBufferBaseTest.cpp
+++ b/lib/cpp/test/TBufferBaseTest.cpp
@@ -165,8 +165,8 @@
// Repeatability. Kind of.
std::srand(42);
- for (size_t i = 0; i < (sizeof(data)/sizeof(data[0])); ++i) {
- data[i] = (uint8_t)rand();
+ for (unsigned char & i : data) {
+ i = (uint8_t)rand();
}
data_str.assign((char*)data, sizeof(data));
@@ -178,14 +178,14 @@
BOOST_AUTO_TEST_CASE( test_MemoryBuffer_Write_GetBuffer ) {
init_data();
- for (int d1 = 0; d1 < 3; d1++) {
+ for (auto & d1 : dist) {
TMemoryBuffer buffer(16);
int offset = 0;
int index = 0;
while (offset < 1<<15) {
- buffer.write(&data[offset], dist[d1][index]);
- offset += dist[d1][index];
+ buffer.write(&data[offset], d1[index]);
+ offset += d1[index];
index++;
}
@@ -197,8 +197,8 @@
BOOST_AUTO_TEST_CASE( test_MemoryBuffer_Write_Read ) {
init_data();
- for (int d1 = 0; d1 < 3; d1++) {
- for (int d2 = 0; d2 < 3; d2++) {
+ for (auto & d1 : dist) {
+ for (auto & d2 : dist) {
TMemoryBuffer buffer(16);
uint8_t data_out[1<<15];
int offset;
@@ -207,17 +207,17 @@
offset = 0;
index = 0;
while (offset < 1<<15) {
- buffer.write(&data[offset], dist[d1][index]);
- offset += dist[d1][index];
+ buffer.write(&data[offset], d1[index]);
+ offset += d1[index];
index++;
}
offset = 0;
index = 0;
while (offset < 1<<15) {
- unsigned int got = buffer.read(&data_out[offset], dist[d2][index]);
- BOOST_CHECK_EQUAL(got, dist[d2][index]);
- offset += dist[d2][index];
+ unsigned int got = buffer.read(&data_out[offset], d2[index]);
+ BOOST_CHECK_EQUAL(got, d2[index]);
+ offset += d2[index];
index++;
}
@@ -229,8 +229,8 @@
BOOST_AUTO_TEST_CASE( test_MemoryBuffer_Write_ReadString ) {
init_data();
- for (int d1 = 0; d1 < 3; d1++) {
- for (int d2 = 0; d2 < 3; d2++) {
+ for (auto & d1 : dist) {
+ for (auto & d2 : dist) {
TMemoryBuffer buffer(16);
string output;
int offset;
@@ -239,17 +239,17 @@
offset = 0;
index = 0;
while (offset < 1<<15) {
- buffer.write(&data[offset], dist[d1][index]);
- offset += dist[d1][index];
+ buffer.write(&data[offset], d1[index]);
+ offset += d1[index];
index++;
}
offset = 0;
index = 0;
while (offset < 1<<15) {
- unsigned int got = buffer.readAppendToString(output, dist[d2][index]);
- BOOST_CHECK_EQUAL(got, dist[d2][index]);
- offset += dist[d2][index];
+ unsigned int got = buffer.readAppendToString(output, d2[index]);
+ BOOST_CHECK_EQUAL(got, d2[index]);
+ offset += d2[index];
index++;
}
@@ -263,8 +263,8 @@
// Do shorter writes and reads so we don't align to power-of-two boundaries.
- for (int d1 = 0; d1 < 3; d1++) {
- for (int d2 = 0; d2 < 3; d2++) {
+ for (auto & d1 : dist) {
+ for (auto & d2 : dist) {
TMemoryBuffer buffer(16);
uint8_t data_out[1<<15];
int offset;
@@ -274,16 +274,16 @@
offset = 0;
index = 0;
while (offset < (1<<15)-42) {
- buffer.write(&data[offset], dist[d1][index]);
- offset += dist[d1][index];
+ buffer.write(&data[offset], d1[index]);
+ offset += d1[index];
index++;
}
offset = 0;
index = 0;
while (offset < (1<<15)-42) {
- buffer.read(&data_out[offset], dist[d2][index]);
- offset += dist[d2][index];
+ buffer.read(&data_out[offset], d2[index]);
+ offset += d2[index];
index++;
}
@@ -303,8 +303,8 @@
// Pull the buffer out of the loop so its state gets worked harder.
TMemoryBuffer buffer(16);
- for (int d1 = 0; d1 < 3; d1++) {
- for (int d2 = 0; d2 < 3; d2++) {
+ for (auto & d1 : dist) {
+ for (auto & d2 : dist) {
uint8_t data_out[1<<15];
int offset;
int index;
@@ -313,16 +313,16 @@
offset = 0;
index = 0;
while (offset < (1<<15)-42) {
- buffer.write(&data[offset], dist[d1][index]);
- offset += dist[d1][index];
+ buffer.write(&data[offset], d1[index]);
+ offset += d1[index];
index++;
}
offset = 0;
index = 0;
while (offset < (1<<15)-42) {
- buffer.read(&data_out[offset], dist[d2][index]);
- offset += dist[d2][index];
+ buffer.read(&data_out[offset], d2[index]);
+ offset += d2[index];
index++;
}
@@ -341,8 +341,8 @@
// Do shorter writes and reads so we don't align to power-of-two boundaries.
// Pull the buffer out of the loop so its state gets worked harder.
- for (int d1 = 0; d1 < 3; d1++) {
- for (int d2 = 0; d2 < 3; d2++) {
+ for (auto & d1 : dist) {
+ for (auto & d2 : dist) {
TMemoryBuffer buffer(16);
uint8_t data_out[1<<13];
@@ -350,7 +350,7 @@
int write_index = 0;
unsigned int to_write = (1<<14)-42;
while (to_write > 0) {
- int write_amt = (std::min)(dist[d1][write_index], to_write);
+ int write_amt = (std::min)(d1[write_index], to_write);
buffer.write(&data[write_offset], write_amt);
write_offset += write_amt;
write_index++;
@@ -361,7 +361,7 @@
int read_index = 0;
unsigned int to_read = (1<<13)-42;
while (to_read > 0) {
- int read_amt = (std::min)(dist[d2][read_index], to_read);
+ int read_amt = (std::min)(d2[read_index], to_read);
int got = buffer.read(&data_out[read_offset], read_amt);
BOOST_CHECK_EQUAL(got, read_amt);
read_offset += read_amt;
@@ -375,7 +375,7 @@
int second_index = write_index-1;
unsigned int to_second = (1<<14)+42;
while (to_second > 0) {
- int second_amt = (std::min)(dist[d1][second_index], to_second);
+ int second_amt = (std::min)(d1[second_index], to_second);
//printf("%d\n", second_amt);
buffer.write(&data[second_offset], second_amt);
second_offset += second_amt;
@@ -399,17 +399,16 @@
1<<14, 1<<17,
};
- for (size_t i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++) {
- int size = sizes[i];
- for (int d1 = 0; d1 < 3; d1++) {
+ for (int size : sizes) {
+ for (auto & d1 : dist) {
shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(16));
TBufferedTransport trans(buffer, size);
int offset = 0;
int index = 0;
while (offset < 1<<15) {
- trans.write(&data[offset], dist[d1][index]);
- offset += dist[d1][index];
+ trans.write(&data[offset], d1[index]);
+ offset += d1[index];
index++;
}
trans.flush();
@@ -430,9 +429,8 @@
1<<14, 1<<17,
};
- for (size_t i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++) {
- int size = sizes[i];
- for (int d1 = 0; d1 < 3; d1++) {
+ for (int size : sizes) {
+ for (auto & d1 : dist) {
shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(data, sizeof(data)));
TBufferedTransport trans(buffer, size);
uint8_t data_out[1<<15];
@@ -443,8 +441,8 @@
// Note: this doesn't work with "read" because TBufferedTransport
// doesn't try loop over reads, so we get short reads. We don't
// check the return value, so that messes us up.
- trans.readAll(&data_out[offset], dist[d1][index]);
- offset += dist[d1][index];
+ trans.readAll(&data_out[offset], d1[index]);
+ offset += d1[index];
index++;
}
@@ -463,9 +461,8 @@
1<<14, 1<<17,
};
- for (size_t i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++) {
- int size = sizes[i];
- for (int d1 = 0; d1 < 3; d1++) {
+ for (int size : sizes) {
+ for (auto & d1 : dist) {
shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(data, sizeof(data)));
shared_ptr<TShortReadTransport> tshort(new TShortReadTransport(buffer, 0.125));
TBufferedTransport trans(buffer, size);
@@ -477,8 +474,8 @@
// Note: this doesn't work with "read" because TBufferedTransport
// doesn't try loop over reads, so we get short reads. We don't
// check the return value, so that messes us up.
- trans.readAll(&data_out[offset], dist[d1][index]);
- offset += dist[d1][index];
+ trans.readAll(&data_out[offset], d1[index]);
+ offset += d1[index];
index++;
}
@@ -497,17 +494,16 @@
1<<14, 1<<17,
};
- for (size_t i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++) {
- int size = sizes[i];
- for (int d1 = 0; d1 < 3; d1++) {
+ for (int size : sizes) {
+ for (auto & d1 : dist) {
shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(16));
TFramedTransport trans(buffer, size);
int offset = 0;
int index = 0;
while (offset < 1<<15) {
- trans.write(&data[offset], dist[d1][index]);
- offset += dist[d1][index];
+ trans.write(&data[offset], d1[index]);
+ offset += d1[index];
index++;
}
trans.flush();
@@ -526,7 +522,7 @@
BOOST_AUTO_TEST_CASE( test_FramedTransport_Read ) {
init_data();
- for (int d1 = 0; d1 < 3; d1++) {
+ for (auto & d1 : dist) {
uint8_t data_out[1<<15];
shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
TFramedTransport trans(buffer);
@@ -539,8 +535,8 @@
int index = 0;
while (offset < 1<<15) {
// This should work with read because we have one huge frame.
- trans.read(&data_out[offset], dist[d1][index]);
- offset += dist[d1][index];
+ trans.read(&data_out[offset], d1[index]);
+ offset += d1[index];
index++;
}
@@ -560,11 +556,9 @@
int probs[] = { 1, 2, 4, 8, 16, 32, };
- for (size_t i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++) {
- int size = sizes[i];
- for (size_t j = 0; j < sizeof (probs) / sizeof (probs[0]); j++) {
- int prob = probs[j];
- for (int d1 = 0; d1 < 3; d1++) {
+ for (int size : sizes) {
+ for (int prob : probs) {
+ for (auto & d1 : dist) {
shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(16));
TFramedTransport trans(buffer, size);
std::vector<uint8_t> data_out(1<<17, 0);
@@ -574,9 +568,9 @@
int write_index = 0;
int flush_size = 0;
while (write_offset < 1<<15) {
- trans.write(&data[write_offset], dist[d1][write_index]);
- write_offset += dist[d1][write_index];
- flush_size += dist[d1][write_index];
+ trans.write(&data[write_offset], d1[write_index]);
+ write_offset += d1[write_index];
+ flush_size += d1[write_index];
write_index++;
if (flush_size > 0 && rand()%prob == 0) {
flush_sizes.push_back(flush_size);
@@ -593,8 +587,7 @@
int read_offset = 0;
int read_index = 0;
- for (unsigned int k = 0; k < flush_sizes.size(); k++) {
- int fsize = flush_sizes[k];
+ for (int fsize : flush_sizes) {
// We are exploiting an implementation detail of TFramedTransport.
// The read buffer starts empty and it will never do more than one
// readFrame per read, so we should always get exactly one frame.
diff --git a/lib/cpp/test/TFileTransportTest.cpp b/lib/cpp/test/TFileTransportTest.cpp
index ad32185..21c1f3b 100644
--- a/lib/cpp/test/TFileTransportTest.cpp
+++ b/lib/cpp/test/TFileTransportTest.cpp
@@ -279,12 +279,12 @@
BOOST_WARN_GE(calls->size(), static_cast<FsyncLog::CallList::size_type>(1));
const struct timeval* prev_time = nullptr;
- for (auto it = calls->begin(); it != calls->end(); ++it) {
+ for (const auto & call : *calls) {
if (prev_time) {
- int delta = time_diff(prev_time, &it->time);
+ int delta = time_diff(prev_time, &call.time);
BOOST_WARN( delta < max_allowed_delta );
}
- prev_time = &it->time;
+ prev_time = &call.time;
}
}
diff --git a/lib/cpp/test/TMemoryBufferTest.cpp b/lib/cpp/test/TMemoryBufferTest.cpp
index 9206872..42f9711 100644
--- a/lib/cpp/test/TMemoryBufferTest.cpp
+++ b/lib/cpp/test/TMemoryBufferTest.cpp
@@ -81,9 +81,9 @@
}
BOOST_AUTO_TEST_CASE(test_readAppendToString) {
- string* str1 = new string("abcd1234");
- TMemoryBuffer buf((uint8_t*)str1->data(),
- static_cast<uint32_t>(str1->length()),
+ string str1 = "abcd1234";
+ TMemoryBuffer buf((uint8_t*)str1.data(),
+ static_cast<uint32_t>(str1.length()),
TMemoryBuffer::COPY);
string str3 = "wxyz", str4 = "6789";
diff --git a/lib/cpp/test/TNonblockingSSLServerTest.cpp b/lib/cpp/test/TNonblockingSSLServerTest.cpp
index d0d8688..dc40c12 100644
--- a/lib/cpp/test/TNonblockingSSLServerTest.cpp
+++ b/lib/cpp/test/TNonblockingSSLServerTest.cpp
@@ -151,7 +151,7 @@
std::shared_ptr<transport::TNonblockingSSLServerSocket> socket;
Mutex mutex_;
- Runner() {
+ Runner():port(0) {
listenHandler.reset(new ListenEventHandler(&mutex_));
}
diff --git a/lib/cpp/test/TNonblockingServerTest.cpp b/lib/cpp/test/TNonblockingServerTest.cpp
index 434217e..f9aab4c 100644
--- a/lib/cpp/test/TNonblockingServerTest.cpp
+++ b/lib/cpp/test/TNonblockingServerTest.cpp
@@ -83,6 +83,7 @@
Mutex mutex_;
Runner() {
+ port = 0;
listenHandler.reset(new ListenEventHandler(&mutex_));
}
diff --git a/lib/cpp/test/TServerTransportTest.cpp b/lib/cpp/test/TServerTransportTest.cpp
index 15177a8..18a393e 100644
--- a/lib/cpp/test/TServerTransportTest.cpp
+++ b/lib/cpp/test/TServerTransportTest.cpp
@@ -39,7 +39,7 @@
protected:
shared_ptr<TTransport> acceptImpl() override {
- return valid_ ? shared_ptr<TestTTransport>(new TestTTransport)
+ return valid_ ? std::make_shared<TestTTransport>()
: shared_ptr<TestTTransport>();
}
};
diff --git a/lib/cpp/test/TransportTest.cpp b/lib/cpp/test/TransportTest.cpp
index b0c84b6..a890aa8 100644
--- a/lib/cpp/test/TransportTest.cpp
+++ b/lib/cpp/test/TransportTest.cpp
@@ -140,8 +140,8 @@
std::shared_ptr<Transport_> out;
private:
- CoupledTransports(const CoupledTransports&);
- CoupledTransports& operator=(const CoupledTransports&);
+ CoupledTransports(const CoupledTransports&) = delete;
+ CoupledTransports& operator=(const CoupledTransports&) = delete;
};
/**
diff --git a/lib/cpp/test/concurrency/Tests.cpp b/lib/cpp/test/concurrency/Tests.cpp
index f2b0111..8c734c2 100644
--- a/lib/cpp/test/concurrency/Tests.cpp
+++ b/lib/cpp/test/concurrency/Tests.cpp
@@ -31,8 +31,6 @@
int main(int argc, char** argv) {
- std::string arg;
-
std::vector<std::string> args(argc - 1 > 1 ? argc - 1 : 1);
args[0] = "all";