STYLE: Use range-based loops from C++11
C++11 Range based for loops can be used in
Used as a more readable equivalent to the traditional for loop operating over a
range of values, such as all elements in a container, in the forward direction..
Range based loopes are more explicit for only computing the
end location once for containers.
SRCDIR=/Users/johnsonhj/src/jsoncpp/ #My local SRC
BLDDIR=/Users/johnsonhj/src/jsoncpp/cmake-build-debug/ #My local BLD
cd /Users/johnsonhj/src/jsoncpp/cmake-build-debug/
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,modernize-loop-convert -header-filter=.* -fix
diff --git a/src/jsontestrunner/main.cpp b/src/jsontestrunner/main.cpp
index 28691a2..5449299 100644
--- a/src/jsontestrunner/main.cpp
+++ b/src/jsontestrunner/main.cpp
@@ -110,9 +110,7 @@
Json::Value::Members members(value.getMemberNames());
std::sort(members.begin(), members.end());
JSONCPP_STRING suffix = *(path.end() - 1) == '.' ? "" : ".";
- for (Json::Value::Members::iterator it = members.begin();
- it != members.end(); ++it) {
- const JSONCPP_STRING name = *it;
+ for (auto name : members) {
printValueTree(fout, value[name], path + suffix + name);
}
} break;
diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp
index aa5156f..077da1b 100644
--- a/src/lib_json/json_reader.cpp
+++ b/src/lib_json/json_reader.cpp
@@ -818,9 +818,7 @@
JSONCPP_STRING Reader::getFormattedErrorMessages() const {
JSONCPP_STRING formattedMessage;
- for (Errors::const_iterator itError = errors_.begin();
- itError != errors_.end(); ++itError) {
- const ErrorInfo& error = *itError;
+ for (const auto & error : errors_) {
formattedMessage +=
"* " + getLocationLineAndColumn(error.token_.start_) + "\n";
formattedMessage += " " + error.message_ + "\n";
@@ -833,9 +831,7 @@
std::vector<Reader::StructuredError> Reader::getStructuredErrors() const {
std::vector<Reader::StructuredError> allErrors;
- for (Errors::const_iterator itError = errors_.begin();
- itError != errors_.end(); ++itError) {
- const ErrorInfo& error = *itError;
+ for (const auto & error : errors_) {
Reader::StructuredError structured;
structured.offset_start = error.token_.start_ - begin_;
structured.offset_limit = error.token_.end_ - begin_;
@@ -1833,9 +1829,7 @@
JSONCPP_STRING OurReader::getFormattedErrorMessages() const {
JSONCPP_STRING formattedMessage;
- for (Errors::const_iterator itError = errors_.begin();
- itError != errors_.end(); ++itError) {
- const ErrorInfo& error = *itError;
+ for (const auto & error : errors_) {
formattedMessage +=
"* " + getLocationLineAndColumn(error.token_.start_) + "\n";
formattedMessage += " " + error.message_ + "\n";
@@ -1848,9 +1842,7 @@
std::vector<OurReader::StructuredError> OurReader::getStructuredErrors() const {
std::vector<OurReader::StructuredError> allErrors;
- for (Errors::const_iterator itError = errors_.begin();
- itError != errors_.end(); ++itError) {
- const ErrorInfo& error = *itError;
+ for (const auto & error : errors_) {
OurReader::StructuredError structured;
structured.offset_start = error.token_.start_ - begin_;
structured.offset_limit = error.token_.end_ - begin_;
diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp
index c7fcfa4..5e83239 100644
--- a/src/lib_json/json_value.cpp
+++ b/src/lib_json/json_value.cpp
@@ -1655,8 +1655,7 @@
const Value& Path::resolve(const Value& root) const {
const Value* node = &root;
- for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
- const PathArgument& arg = *it;
+ for (const auto & arg : args_) {
if (arg.kind_ == PathArgument::kindIndex) {
if (!node->isArray() || !node->isValidIndex(arg.index_)) {
// Error: unable to resolve path (array value expected at position...
@@ -1681,8 +1680,7 @@
Value Path::resolve(const Value& root, const Value& defaultValue) const {
const Value* node = &root;
- for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
- const PathArgument& arg = *it;
+ for (const auto & arg : args_) {
if (arg.kind_ == PathArgument::kindIndex) {
if (!node->isArray() || !node->isValidIndex(arg.index_))
return defaultValue;
@@ -1700,8 +1698,7 @@
Value& Path::make(Value& root) const {
Value* node = &root;
- for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
- const PathArgument& arg = *it;
+ for (const auto & arg : args_) {
if (arg.kind_ == PathArgument::kindIndex) {
if (!node->isArray()) {
// Error: node is not an array at position ...
diff --git a/src/test_lib_json/jsontest.cpp b/src/test_lib_json/jsontest.cpp
index 5e04aa4..f661505 100644
--- a/src/test_lib_json/jsontest.cpp
+++ b/src/test_lib_json/jsontest.cpp
@@ -150,9 +150,7 @@
}
// Print in reverse to display the callstack in the right order
- Failures::const_iterator itEnd = failures_.end();
- for (Failures::const_iterator it = failures_.begin(); it != itEnd; ++it) {
- const Failure& failure = *it;
+ for (const auto & failure : failures_ ) {
JSONCPP_STRING indent(failure.nestingLevel_ * 2, ' ');
if (failure.file_) {
printf("%s%s(%u): ", indent.c_str(), failure.file_, failure.line_);
@@ -275,8 +273,7 @@
}
return true;
} else {
- for (unsigned int index = 0; index < failures.size(); ++index) {
- TestResult& result = failures[index];
+ for (auto & result : failures) {
result.printFailure(count > 1);
}
diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp
index 4dfca75..8b21547 100644
--- a/src/test_lib_json/main.cpp
+++ b/src/test_lib_json/main.cpp
@@ -2365,8 +2365,7 @@
{ __LINE__, false, "{\"a\":.Infinity}" }, { __LINE__, false, "{\"a\":_Infinity}" },
{ __LINE__, false, "{\"a\":_nfinity}" }, { __LINE__, true, "{\"a\":-Infinity}" }
};
- for (size_t tdi = 0; tdi < sizeof(test_data) / sizeof(*test_data); ++tdi) {
- const TestData& td = test_data[tdi];
+ for (const auto& td : test_data) {
bool ok = reader->parse(&*td.in.begin(), &*td.in.begin() + td.in.size(),
&root, &errs);
JSONTEST_ASSERT(td.ok == ok) << "line:" << td.line << "\n"