clang-tidy fixes again (#1087)
* [clang-tidy] Do not use else after return
Found with readability-else-after-return
Signed-off-by: Rosen Penev <rosenp@gmail.com>
* [clang-tidy] Convert several loops to be range based
Found with modernize-loop-convert
Signed-off-by: Rosen Penev <rosenp@gmail.com>
* [clang-tidy] Replace deprecated C headers
Found with modernize-deprecated-headers
Signed-off-by: Rosen Penev <rosenp@gmail.com>
* [clang-tidy] Use auto where applicable
Found with modernize-use-auto
Signed-off-by: Rosen Penev <rosenp@gmail.com>
* .clang-tidy: Add these checks
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 @@
if (!file)
return "";
fseek(file, 0, SEEK_END);
- long const size = ftell(file);
- const auto usize = static_cast<size_t>(size);
+ auto const size = ftell(file);
+ auto const usize = static_cast<size_t>(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 @@
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 @@
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 @@
// 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 @@
if (c < '0' || c > '9')
return decodeDouble(token, decoded);
- const Value::UInt digit(static_cast<Value::UInt>(c - '0'));
+ const auto digit(static_cast<Value::UInt>(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 @@
if (isNegative) {
// We use the same magnitude assumption here, just in case.
- const Value::UInt last_digit = static_cast<Value::UInt>(value % 10);
+ const auto last_digit = static_cast<Value::UInt>(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 @@
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 @@
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 @@
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 <json/config.h>
#include <json/json.h>
#include <memory>
-#include <stdint.h>
#include <string>
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 @@
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 @@
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 @@
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 @@
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);