Skip to content

Commit

Permalink
Lint: Lint for std::for_each_n (#15093)
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Baichoo <kbaichoo@google.com>
  • Loading branch information
KBaichoo authored Feb 23, 2021
1 parent 5e7eb11 commit a50b1c0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions source/common/http/http2/codec_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1409,8 +1409,13 @@ void ConnectionImpl::dumpState(std::ostream& os, int indent_level) const {
DUMP_DETAILS(&protocol_constraints_);

os << spaces << "Number of active streams: " << active_streams_.size() << " Active Streams:\n";
std::for_each_n(active_streams_.begin(), std::min<size_t>(active_streams_.size(), 100),
[&](auto& stream) { DUMP_DETAILS(stream); });
size_t count = 0;
for (auto& stream : active_streams_) {
DUMP_DETAILS(stream);
if (++count >= 100) {
break;
}
}

// Dump the active slice
if (current_slice_ == nullptr) {
Expand Down
4 changes: 4 additions & 0 deletions tools/code_format/check_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@
RELOADABLE_FLAG_REGEX = re.compile(".*(..)(envoy.reloadable_features.[^ ]*)\s.*")
INVALID_REFLINK = re.compile(".* ref:.*")
OLD_MOCK_METHOD_REGEX = re.compile("MOCK_METHOD\d")
# C++17 feature, lacks sufficient support across various libraries / compilers.
FOR_EACH_N_REGEX = re.compile("for_each_n\(")
# Check for punctuation in a terminal ref clause, e.g.
# :ref:`panic mode. <arch_overview_load_balancing_panic_threshold>`
REF_WITH_PUNCTUATION_REGEX = re.compile(".*\. <[^<]*>`\s*")
Expand Down Expand Up @@ -779,6 +781,8 @@ def checkSourceLine(self, line, file_path, reportError):
reportError("Test names should be CamelCase, starting with a capital letter")
if OLD_MOCK_METHOD_REGEX.search(line):
reportError("The MOCK_METHODn() macros should not be used, use MOCK_METHOD() instead")
if FOR_EACH_N_REGEX.search(line):
reportError("std::for_each_n should not be used, use an alternative for loop instead")

if not self.allowlistedForSerializeAsString(file_path) and "SerializeAsString" in line:
# The MessageLite::SerializeAsString doesn't generate deterministic serialization,
Expand Down
1 change: 1 addition & 0 deletions tools/code_format/check_format_test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ def runChecks():
errors += checkUnfixableError("test_naming.cc",
"Test names should be CamelCase, starting with a capital letter")
errors += checkUnfixableError("mock_method_n.cc", "use MOCK_METHOD() instead")
errors += checkUnfixableError("for_each_n.cc", "use an alternative for loop instead")
errors += checkUnfixableError(
"test/register_factory.cc",
"Don't use Registry::RegisterFactory or REGISTER_FACTORY in tests, use "
Expand Down
10 changes: 10 additions & 0 deletions tools/testdata/check_format/for_each_n.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <algorithm>
#include <vector>
namespace Envoy {

void foo() {
std::vector<int> vec;
std::for_each_n(vec.begin(), 10, (int){});
}

} // namespace Envoy

0 comments on commit a50b1c0

Please sign in to comment.