Skip to content

Commit

Permalink
More for-each loops.
Browse files Browse the repository at this point in the history
Generated by clang-tidy modernize-loop-convert
  • Loading branch information
chenshuo committed Oct 23, 2018
1 parent 52f2345 commit c74bc32
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
# Only run a few checks for now.
Checks: 'modernize-use-noexcept'
Checks: 'modernize-loop-convert'
WarningsAsErrors: ''
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
Expand Down
4 changes: 2 additions & 2 deletions muduo/base/AsyncLogging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ void AsyncLogging::threadFunc()
buffersToWrite.erase(buffersToWrite.begin()+2, buffersToWrite.end());
}

for (size_t i = 0; i < buffersToWrite.size(); ++i)
for (const auto& buffer : buffersToWrite)
{
// FIXME: use unbuffered stdio FILE ? or use ::writev ?
output.append(buffersToWrite[i]->data(), buffersToWrite[i]->length());
output.append(buffer->data(), buffer->length());
}

if (buffersToWrite.size() > 2)
Expand Down
5 changes: 2 additions & 3 deletions muduo/base/tests/BlockingQueue_bench.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,11 @@ class Bench
printf("tid=%d, %s stopped\n",
muduo::CurrentThread::tid(),
muduo::CurrentThread::name());
for (std::map<int, int>::iterator it = delays.begin();
it != delays.end(); ++it)
for (const std::pair<int, int>& delay : delays)
{
printf("tid = %d, delay = %d, count = %d\n",
muduo::CurrentThread::tid(),
it->first, it->second);
delay.first, delay.second);
}
}

Expand Down
5 changes: 2 additions & 3 deletions muduo/base/tests/Thread_bench.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ int main(int argc, char* argv[])
}
{
muduo::MutexLockGuard lock(g_mutex);
for (std::map<int, int>::iterator it = g_delays.begin();
it != g_delays.end(); ++it)
for (const std::pair<int, int>& delay : g_delays)
{
printf("delay = %d, count = %d\n",
it->first, it->second);
delay.first, delay.second);
}
}

Expand Down
20 changes: 10 additions & 10 deletions muduo/base/tests/TimeZone_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ void testNewYork()

};

for (size_t i = 0; i < sizeof cases / sizeof cases[0]; ++i)
for (const auto& c : cases)
{
test(tz, cases[i]);
test(tz, c);
}
}

Expand Down Expand Up @@ -180,9 +180,9 @@ void testLondon()

};

for (size_t i = 0; i < sizeof cases / sizeof cases[0]; ++i)
for (const auto& c : cases)
{
test(tz, cases[i]);
test(tz, c);
}
}

Expand All @@ -196,9 +196,9 @@ void testHongKong()

};

for (size_t i = 0; i < sizeof cases / sizeof cases[0]; ++i)
for (const auto& c : cases)
{
test(tz, cases[i]);
test(tz, c);
}
}

Expand All @@ -219,9 +219,9 @@ void testSydney()

};

for (size_t i = 0; i < sizeof cases / sizeof cases[0]; ++i)
for (const auto& c : cases)
{
test(tz, cases[i]);
test(tz, c);
}
}

Expand Down Expand Up @@ -259,9 +259,9 @@ void testFixedTimezone()

};

for (size_t i = 0; i < sizeof cases / sizeof cases[0]; ++i)
for (const auto& c : cases)
{
test(tz, cases[i]);
test(tz, c);
}
}

Expand Down
4 changes: 2 additions & 2 deletions muduo/net/EventLoop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ void EventLoop::doPendingFunctors()
functors.swap(pendingFunctors_);
}

for (size_t i = 0; i < functors.size(); ++i)
for (const Functor& functor : functors)
{
functors[i]();
functor();
}
callingPendingFunctors_ = false;
}
Expand Down
28 changes: 12 additions & 16 deletions muduo/net/TimerQueue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@ TimerQueue::~TimerQueue()
timerfdChannel_.remove();
::close(timerfd_);
// do not remove channel, since we're in EventLoop::dtor();
for (TimerList::iterator it = timers_.begin();
it != timers_.end(); ++it)
for (const Entry& timer : timers_)
{
delete it->second;
delete timer.second;
}
}

Expand Down Expand Up @@ -172,10 +171,9 @@ void TimerQueue::handleRead()
callingExpiredTimers_ = true;
cancelingTimers_.clear();
// safe to callback outside critical section
for (std::vector<Entry>::iterator it = expired.begin();
it != expired.end(); ++it)
for (const Entry& it : expired)
{
it->second->run();
it.second->run();
}
callingExpiredTimers_ = false;

Expand All @@ -192,10 +190,9 @@ std::vector<TimerQueue::Entry> TimerQueue::getExpired(Timestamp now)
std::copy(timers_.begin(), end, back_inserter(expired));
timers_.erase(timers_.begin(), end);

for (std::vector<Entry>::iterator it = expired.begin();
it != expired.end(); ++it)
for (const Entry& it : expired)
{
ActiveTimer timer(it->second, it->second->sequence());
ActiveTimer timer(it.second, it.second->sequence());
size_t n = activeTimers_.erase(timer);
assert(n == 1); (void)n;
}
Expand All @@ -208,20 +205,19 @@ void TimerQueue::reset(const std::vector<Entry>& expired, Timestamp now)
{
Timestamp nextExpire;

for (std::vector<Entry>::const_iterator it = expired.begin();
it != expired.end(); ++it)
for (const Entry& it : expired)
{
ActiveTimer timer(it->second, it->second->sequence());
if (it->second->repeat()
ActiveTimer timer(it.second, it.second->sequence());
if (it.second->repeat()
&& cancelingTimers_.find(timer) == cancelingTimers_.end())
{
it->second->restart(now);
insert(it->second);
it.second->restart(now);
insert(it.second);
}
else
{
// FIXME move to a free list
delete it->second; // FIXME: no delete please
delete it.second; // FIXME: no delete please
}
}

Expand Down
8 changes: 3 additions & 5 deletions muduo/net/http/HttpResponse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ void HttpResponse::appendToBuffer(Buffer* output) const
output->append("Connection: Keep-Alive\r\n");
}

for (std::map<string, string>::const_iterator it = headers_.begin();
it != headers_.end();
++it)
for (const std::pair<string, string>& header : headers_)
{
output->append(it->first);
output->append(header.first);
output->append(": ");
output->append(it->second);
output->append(header.second);
output->append("\r\n");
}

Expand Down
6 changes: 2 additions & 4 deletions muduo/net/http/tests/HttpServer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ void onRequest(const HttpRequest& req, HttpResponse* resp)
if (!benchmark)
{
const std::map<string, string>& headers = req.headers();
for (std::map<string, string>::const_iterator it = headers.begin();
it != headers.end();
++it)
for (const std::pair<string, string>& header : headers)
{
std::cout << it->first << ": " << it->second << std::endl;
std::cout << header.first << ": " << header.second << std::endl;
}
}

Expand Down
10 changes: 4 additions & 6 deletions muduo/net/inspect/Inspector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,15 @@ void Inspector::onRequest(const HttpRequest& req, HttpResponse* resp)
++helpListI)
{
const HelpList& list = helpListI->second;
for (HelpList::const_iterator it = list.begin();
it != list.end();
++it)
for (const std::pair<string, string>& it : list)
{
result += "/";
result += helpListI->first;
result += "/";
result += it->first;
size_t len = helpListI->first.size() + it->first.size();
result += it.first;
size_t len = helpListI->first.size() + it.first.size();
result += string(len >= 25 ? 1 : 25 - len, ' ');
result += it->second;
result += it.second;
result += "\n";
}
}
Expand Down
3 changes: 1 addition & 2 deletions muduo/net/inspect/ProcessInspector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,9 @@ string ProcessInspector::threads(HttpRequest::Method, const Inspector::ArgList&)
string result = " TID NAME S User Time System Time\n";
result.reserve(threads.size() * 64);
string stat;
for (size_t i = 0; i < threads.size(); ++i)
for (pid_t tid : threads)
{
char buf[256];
int tid = threads[i];
snprintf(buf, sizeof buf, "/proc/%d/task/%d/stat", ProcessInfo::pid(), tid);
if (FileUtil::readFile(buf, 65536, &stat) == 0)
{
Expand Down
4 changes: 2 additions & 2 deletions muduo/net/protorpc/RpcChannel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ RpcChannel::RpcChannel(const TcpConnectionPtr& conn)
RpcChannel::~RpcChannel()
{
LOG_INFO << "RpcChannel::dtor - " << this;
for (std::map<int64_t, OutstandingCall>::iterator it = outstandings_.begin(); it != outstandings_.end(); ++it)
for (const auto& outstanding : outstandings_)
{
OutstandingCall out = it->second;
OutstandingCall out = outstanding.second;
delete out.response;
delete out.done;
}
Expand Down

0 comments on commit c74bc32

Please sign in to comment.