From edf528edfa66fd02f54f81ae341592f50e61c39f Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sun, 2 Feb 2020 20:03:45 -0800 Subject: [PATCH] clang-tidy fixes again (#1087) * [clang-tidy] Do not use else after return Found with readability-else-after-return Signed-off-by: Rosen Penev * [clang-tidy] Convert several loops to be range based Found with modernize-loop-convert Signed-off-by: Rosen Penev * [clang-tidy] Replace deprecated C headers Found with modernize-deprecated-headers Signed-off-by: Rosen Penev * [clang-tidy] Use auto where applicable Found with modernize-use-auto Signed-off-by: Rosen Penev * .clang-tidy: Add these checks --- .clang-tidy | 2 +- src/jsontestrunner/main.cpp | 6 +++--- src/lib_json/json_reader.cpp | 19 +++++++++---------- src/lib_json/json_value.cpp | 14 ++++++-------- src/test_lib_json/fuzz.cpp | 1 - src/test_lib_json/jsontest.cpp | 26 +++++++++++++------------- src/test_lib_json/main.cpp | 8 +++----- 7 files changed, 35 insertions(+), 41 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index b78b324..be3d06a 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,5 +1,5 @@ --- -Checks: 'google-readability-casting,modernize-use-default-member-init,modernize-use-using,modernize-use-auto,readability-redundant-member-init' +Checks: 'google-readability-casting,modernize-deprecated-headers,modernize-loop-convert,modernize-use-auto,modernize-use-default-member-init,modernize-use-using,readability-else-after-return,readability-redundant-member-init,readability-redundant-string-cstr' WarningsAsErrors: '' HeaderFilterRegex: '' AnalyzeTemporaryDtors: false diff --git a/src/jsontestrunner/main.cpp b/src/jsontestrunner/main.cpp index f6b1a4d..cdf6bff 100644 --- a/src/jsontestrunner/main.cpp +++ b/src/jsontestrunner/main.cpp @@ -57,10 +57,10 @@ static Json::String readInputTestFile(const char* path) { if (!file) return ""; fseek(file, 0, SEEK_END); - long const size = ftell(file); - const auto usize = static_cast(size); + auto const size = ftell(file); + auto const usize = static_cast(size); fseek(file, 0, SEEK_SET); - char* buffer = new char[size + 1]; + auto buffer = new char[size + 1]; buffer[size] = 0; Json::String text; if (fread(buffer, 1, usize, file) == usize) diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index 9818b5b..2c6fead 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -637,7 +637,7 @@ bool Reader::decodeString(Token& token, String& decoded) { Char c = *current++; if (c == '"') break; - else if (c == '\\') { + if (c == '\\') { if (current == end) return addError("Empty escape sequence in string", token, current); Char escape = *current++; @@ -1358,11 +1358,10 @@ bool OurReader::readCStyleComment(bool* containsNewLineResult) { while ((current_ + 1) < end_) { Char c = getNextChar(); - if (c == '*' && *current_ == '/') { + if (c == '*' && *current_ == '/') break; - } else if (c == '\n') { + if (c == '\n') *containsNewLineResult = true; - } } return getNextChar() == '/'; @@ -1586,9 +1585,9 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) { // then take the inverse. This assumes that minLargestInt is only a single // power of 10 different in magnitude, which we check above. For the last // digit, we take the modulus before negating for the same reason. - static constexpr Value::LargestUInt negative_threshold = + static constexpr auto negative_threshold = Value::LargestUInt(-(Value::minLargestInt / 10)); - static constexpr Value::UInt negative_last_digit = + static constexpr auto negative_last_digit = Value::UInt(-(Value::minLargestInt % 10)); const Value::LargestUInt threshold = @@ -1602,7 +1601,7 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) { if (c < '0' || c > '9') return decodeDouble(token, decoded); - const Value::UInt digit(static_cast(c - '0')); + const auto digit(static_cast(c - '0')); if (value >= threshold) { // We've hit or exceeded the max value divided by 10 (rounded down). If // a) we've only just touched the limit, meaing value == threshold, @@ -1619,7 +1618,7 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) { if (isNegative) { // We use the same magnitude assumption here, just in case. - const Value::UInt last_digit = static_cast(value % 10); + const auto last_digit = static_cast(value % 10); decoded = -Value::LargestInt(value / 10) * 10 - last_digit; } else if (value <= Value::LargestUInt(Value::maxLargestInt)) { decoded = Value::LargestInt(value); @@ -1669,9 +1668,9 @@ bool OurReader::decodeString(Token& token, String& decoded) { Location end = token.end_ - 1; // do not include '"' while (current != end) { Char c = *current++; - if (c == '"') { + if (c == '"') break; - } else if (c == '\\') { + if (c == '\\') { if (current == end) return addError("Empty escape sequence in string", token, current); Char escape = *current++; diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 1e3bbde..74535fb 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -875,8 +875,7 @@ ArrayIndex Value::size() const { bool Value::empty() const { if (isNull() || isArray() || isObject()) return size() == 0U; - else - return false; + return false; } Value::operator bool() const { return !isNull(); } @@ -1137,13 +1136,12 @@ bool Value::insert(ArrayIndex index, Value&& newValue) { ArrayIndex length = size(); if (index > length) { return false; - } else { - for (ArrayIndex i = length; i > index; i--) { - (*this)[i] = std::move((*this)[i - 1]); - } - (*this)[index] = std::move(newValue); - return true; } + for (ArrayIndex i = length; i > index; i--) { + (*this)[i] = std::move((*this)[i - 1]); + } + (*this)[index] = std::move(newValue); + return true; } Value Value::get(char const* begin, char const* end, diff --git a/src/test_lib_json/fuzz.cpp b/src/test_lib_json/fuzz.cpp index 68f839d..5b75c22 100644 --- a/src/test_lib_json/fuzz.cpp +++ b/src/test_lib_json/fuzz.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include namespace Json { diff --git a/src/test_lib_json/jsontest.cpp b/src/test_lib_json/jsontest.cpp index 0cc500a..0b7d12b 100644 --- a/src/test_lib_json/jsontest.cpp +++ b/src/test_lib_json/jsontest.cpp @@ -267,19 +267,18 @@ bool Runner::runAllTest(bool printSummary) const { printf("All %zu tests passed\n", count); } return true; - } else { - for (auto& result : failures) { - result.printFailure(count > 1); - } - - if (printSummary) { - size_t const failedCount = failures.size(); - size_t const passedCount = count - failedCount; - printf("%zu/%zu tests passed (%zu failure(s))\n", passedCount, count, - failedCount); - } - return false; } + for (auto& result : failures) { + result.printFailure(count > 1); + } + + if (printSummary) { + size_t const failedCount = failures.size(); + size_t const passedCount = count - failedCount; + printf("%zu/%zu tests passed (%zu failure(s))\n", passedCount, count, + failedCount); + } + return false; } bool Runner::testIndex(const Json::String& testName, size_t& indexOut) const { @@ -308,7 +307,8 @@ int Runner::runCommandLine(int argc, const char* argv[]) const { if (opt == "--list-tests") { listTests(); return 0; - } else if (opt == "--test-auto") { + } + if (opt == "--test-auto") { preventDialogOnCrash(); } else if (opt == "--test") { ++index; diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index c2df69d..7b20e41 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -1476,9 +1476,7 @@ void ValueTest::checkMemberCount(Json::Value& value, JSONTEST_ASSERT_PRED(checkConstMemberCount(value, expectedCount)); } -ValueTest::IsCheck::IsCheck() - - = default; +ValueTest::IsCheck::IsCheck() = default; void ValueTest::checkIs(const Json::Value& value, const IsCheck& check) { JSONTEST_ASSERT_EQUAL(check.isObject_, value.isObject()); @@ -3752,8 +3750,8 @@ JSONTEST_FIXTURE_LOCAL(FuzzTest, fuzzDoesntCrash) { int main(int argc, const char* argv[]) { JsonTest::Runner runner; - for (auto it = local_.begin(); it != local_.end(); it++) { - runner.add(*it); + for (auto& local : local_) { + runner.add(local); } return runner.runCommandLine(argc, argv);