From a50b1c0df5599884ff47fccc66216745f2baa4d1 Mon Sep 17 00:00:00 2001 From: Kevin Baichoo Date: Tue, 23 Feb 2021 09:29:49 -0800 Subject: [PATCH] Lint: Lint for std::for_each_n (#15093) Signed-off-by: Kevin Baichoo --- source/common/http/http2/codec_impl.cc | 9 +++++++-- tools/code_format/check_format.py | 4 ++++ tools/code_format/check_format_test_helper.py | 1 + tools/testdata/check_format/for_each_n.cc | 10 ++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tools/testdata/check_format/for_each_n.cc diff --git a/source/common/http/http2/codec_impl.cc b/source/common/http/http2/codec_impl.cc index 1759f9c843f1..5606a2d78255 100644 --- a/source/common/http/http2/codec_impl.cc +++ b/source/common/http/http2/codec_impl.cc @@ -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(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) { diff --git a/tools/code_format/check_format.py b/tools/code_format/check_format.py index af6b1bd43b4b..a33a840ca7c1 100755 --- a/tools/code_format/check_format.py +++ b/tools/code_format/check_format.py @@ -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. ` REF_WITH_PUNCTUATION_REGEX = re.compile(".*\. <[^<]*>`\s*") @@ -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, diff --git a/tools/code_format/check_format_test_helper.py b/tools/code_format/check_format_test_helper.py index 6b1b0092f44e..3548514a241e 100755 --- a/tools/code_format/check_format_test_helper.py +++ b/tools/code_format/check_format_test_helper.py @@ -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 " diff --git a/tools/testdata/check_format/for_each_n.cc b/tools/testdata/check_format/for_each_n.cc new file mode 100644 index 000000000000..6353a6ac911e --- /dev/null +++ b/tools/testdata/check_format/for_each_n.cc @@ -0,0 +1,10 @@ +#include +#include +namespace Envoy { + +void foo() { + std::vector vec; + std::for_each_n(vec.begin(), 10, (int){}); +} + +} // namespace Envoy