Skip to content

Commit edf528e

Browse files
authored
clang-tidy fixes again (open-source-parsers#1087)
* [clang-tidy] Do not use else after return Found with readability-else-after-return Signed-off-by: Rosen Penev <[email protected]> * [clang-tidy] Convert several loops to be range based Found with modernize-loop-convert Signed-off-by: Rosen Penev <[email protected]> * [clang-tidy] Replace deprecated C headers Found with modernize-deprecated-headers Signed-off-by: Rosen Penev <[email protected]> * [clang-tidy] Use auto where applicable Found with modernize-use-auto Signed-off-by: Rosen Penev <[email protected]> * .clang-tidy: Add these checks
1 parent 6317f9a commit edf528e

File tree

7 files changed

+34
-40
lines changed

7 files changed

+34
-40
lines changed

.clang-tidy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Checks: 'google-readability-casting,modernize-use-default-member-init,modernize-use-using,modernize-use-auto,readability-redundant-member-init'
2+
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'
33
WarningsAsErrors: ''
44
HeaderFilterRegex: ''
55
AnalyzeTemporaryDtors: false

src/jsontestrunner/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ static Json::String readInputTestFile(const char* path) {
5757
if (!file)
5858
return "";
5959
fseek(file, 0, SEEK_END);
60-
long const size = ftell(file);
61-
const auto usize = static_cast<size_t>(size);
60+
auto const size = ftell(file);
61+
auto const usize = static_cast<size_t>(size);
6262
fseek(file, 0, SEEK_SET);
63-
char* buffer = new char[size + 1];
63+
auto buffer = new char[size + 1];
6464
buffer[size] = 0;
6565
Json::String text;
6666
if (fread(buffer, 1, usize, file) == usize)

src/lib_json/json_reader.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ bool Reader::decodeString(Token& token, String& decoded) {
637637
Char c = *current++;
638638
if (c == '"')
639639
break;
640-
else if (c == '\\') {
640+
if (c == '\\') {
641641
if (current == end)
642642
return addError("Empty escape sequence in string", token, current);
643643
Char escape = *current++;
@@ -1358,11 +1358,10 @@ bool OurReader::readCStyleComment(bool* containsNewLineResult) {
13581358

13591359
while ((current_ + 1) < end_) {
13601360
Char c = getNextChar();
1361-
if (c == '*' && *current_ == '/') {
1361+
if (c == '*' && *current_ == '/')
13621362
break;
1363-
} else if (c == '\n') {
1363+
if (c == '\n')
13641364
*containsNewLineResult = true;
1365-
}
13661365
}
13671366

13681367
return getNextChar() == '/';
@@ -1586,9 +1585,9 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
15861585
// then take the inverse. This assumes that minLargestInt is only a single
15871586
// power of 10 different in magnitude, which we check above. For the last
15881587
// digit, we take the modulus before negating for the same reason.
1589-
static constexpr Value::LargestUInt negative_threshold =
1588+
static constexpr auto negative_threshold =
15901589
Value::LargestUInt(-(Value::minLargestInt / 10));
1591-
static constexpr Value::UInt negative_last_digit =
1590+
static constexpr auto negative_last_digit =
15921591
Value::UInt(-(Value::minLargestInt % 10));
15931592

15941593
const Value::LargestUInt threshold =
@@ -1602,7 +1601,7 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
16021601
if (c < '0' || c > '9')
16031602
return decodeDouble(token, decoded);
16041603

1605-
const Value::UInt digit(static_cast<Value::UInt>(c - '0'));
1604+
const auto digit(static_cast<Value::UInt>(c - '0'));
16061605
if (value >= threshold) {
16071606
// We've hit or exceeded the max value divided by 10 (rounded down). If
16081607
// a) we've only just touched the limit, meaing value == threshold,
@@ -1619,7 +1618,7 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
16191618

16201619
if (isNegative) {
16211620
// We use the same magnitude assumption here, just in case.
1622-
const Value::UInt last_digit = static_cast<Value::UInt>(value % 10);
1621+
const auto last_digit = static_cast<Value::UInt>(value % 10);
16231622
decoded = -Value::LargestInt(value / 10) * 10 - last_digit;
16241623
} else if (value <= Value::LargestUInt(Value::maxLargestInt)) {
16251624
decoded = Value::LargestInt(value);
@@ -1669,9 +1668,9 @@ bool OurReader::decodeString(Token& token, String& decoded) {
16691668
Location end = token.end_ - 1; // do not include '"'
16701669
while (current != end) {
16711670
Char c = *current++;
1672-
if (c == '"') {
1671+
if (c == '"')
16731672
break;
1674-
} else if (c == '\\') {
1673+
if (c == '\\') {
16751674
if (current == end)
16761675
return addError("Empty escape sequence in string", token, current);
16771676
Char escape = *current++;

src/lib_json/json_value.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,7 @@ ArrayIndex Value::size() const {
875875
bool Value::empty() const {
876876
if (isNull() || isArray() || isObject())
877877
return size() == 0U;
878-
else
879-
return false;
878+
return false;
880879
}
881880

882881
Value::operator bool() const { return !isNull(); }
@@ -1137,13 +1136,12 @@ bool Value::insert(ArrayIndex index, Value&& newValue) {
11371136
ArrayIndex length = size();
11381137
if (index > length) {
11391138
return false;
1140-
} else {
1141-
for (ArrayIndex i = length; i > index; i--) {
1142-
(*this)[i] = std::move((*this)[i - 1]);
1143-
}
1144-
(*this)[index] = std::move(newValue);
1145-
return true;
11461139
}
1140+
for (ArrayIndex i = length; i > index; i--) {
1141+
(*this)[i] = std::move((*this)[i - 1]);
1142+
}
1143+
(*this)[index] = std::move(newValue);
1144+
return true;
11471145
}
11481146

11491147
Value Value::get(char const* begin, char const* end,

src/test_lib_json/fuzz.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <json/config.h>
1010
#include <json/json.h>
1111
#include <memory>
12-
#include <stdint.h>
1312
#include <string>
1413

1514
namespace Json {

src/test_lib_json/jsontest.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -267,19 +267,18 @@ bool Runner::runAllTest(bool printSummary) const {
267267
printf("All %zu tests passed\n", count);
268268
}
269269
return true;
270-
} else {
271-
for (auto& result : failures) {
272-
result.printFailure(count > 1);
273-
}
270+
}
271+
for (auto& result : failures) {
272+
result.printFailure(count > 1);
273+
}
274274

275-
if (printSummary) {
276-
size_t const failedCount = failures.size();
277-
size_t const passedCount = count - failedCount;
278-
printf("%zu/%zu tests passed (%zu failure(s))\n", passedCount, count,
279-
failedCount);
280-
}
281-
return false;
275+
if (printSummary) {
276+
size_t const failedCount = failures.size();
277+
size_t const passedCount = count - failedCount;
278+
printf("%zu/%zu tests passed (%zu failure(s))\n", passedCount, count,
279+
failedCount);
282280
}
281+
return false;
283282
}
284283

285284
bool Runner::testIndex(const Json::String& testName, size_t& indexOut) const {
@@ -308,7 +307,8 @@ int Runner::runCommandLine(int argc, const char* argv[]) const {
308307
if (opt == "--list-tests") {
309308
listTests();
310309
return 0;
311-
} else if (opt == "--test-auto") {
310+
}
311+
if (opt == "--test-auto") {
312312
preventDialogOnCrash();
313313
} else if (opt == "--test") {
314314
++index;

src/test_lib_json/main.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,9 +1476,7 @@ void ValueTest::checkMemberCount(Json::Value& value,
14761476
JSONTEST_ASSERT_PRED(checkConstMemberCount(value, expectedCount));
14771477
}
14781478

1479-
ValueTest::IsCheck::IsCheck()
1480-
1481-
= default;
1479+
ValueTest::IsCheck::IsCheck() = default;
14821480

14831481
void ValueTest::checkIs(const Json::Value& value, const IsCheck& check) {
14841482
JSONTEST_ASSERT_EQUAL(check.isObject_, value.isObject());
@@ -3752,8 +3750,8 @@ JSONTEST_FIXTURE_LOCAL(FuzzTest, fuzzDoesntCrash) {
37523750
int main(int argc, const char* argv[]) {
37533751
JsonTest::Runner runner;
37543752

3755-
for (auto it = local_.begin(); it != local_.end(); it++) {
3756-
runner.add(*it);
3753+
for (auto& local : local_) {
3754+
runner.add(local);
37573755
}
37583756

37593757
return runner.runCommandLine(argc, argv);

0 commit comments

Comments
 (0)