From 2983f5a89a9fd8399402af10b27ff214509431d3 Mon Sep 17 00:00:00 2001 From: Chen <50514813+dota17@users.noreply.github.com> Date: Wed, 4 Dec 2019 09:08:45 +0800 Subject: [PATCH] Run Clang-tidy with modernize-use-auto (#1077) * Run clang-tidy modify with modernize-use-auto * Use using instead of typedef --- .clang-tidy | 2 +- example/readFromString/readFromString.cpp | 2 +- include/json/config.h | 20 ++++----- include/json/forwards.h | 2 +- include/json/reader.h | 8 ++-- include/json/value.h | 54 +++++++++++------------ include/json/writer.h | 4 +- src/jsontestrunner/main.cpp | 2 +- src/lib_json/json_reader.cpp | 2 +- src/lib_json/json_tool.h | 2 +- src/lib_json/json_value.cpp | 13 +++--- src/lib_json/json_writer.cpp | 2 +- src/test_lib_json/fuzz.cpp | 2 +- src/test_lib_json/jsontest.h | 8 ++-- 14 files changed, 62 insertions(+), 61 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 6b5e801..b78b324 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,5 +1,5 @@ --- -Checks: 'google-readability-casting,modernize-use-default-member-init,modernize-use-using,readability-redundant-member-init' +Checks: 'google-readability-casting,modernize-use-default-member-init,modernize-use-using,modernize-use-auto,readability-redundant-member-init' WarningsAsErrors: '' HeaderFilterRegex: '' AnalyzeTemporaryDtors: false diff --git a/example/readFromString/readFromString.cpp b/example/readFromString/readFromString.cpp index ce32236..c27bbd5 100644 --- a/example/readFromString/readFromString.cpp +++ b/example/readFromString/readFromString.cpp @@ -11,7 +11,7 @@ */ int main() { const std::string rawJson = R"({"Age": 20, "Name": "colin"})"; - const int rawJsonLength = static_cast(rawJson.length()); + const auto rawJsonLength = static_cast(rawJson.length()); constexpr bool shouldUseOldWay = false; JSONCPP_STRING err; Json::Value root; diff --git a/include/json/config.h b/include/json/config.h index 0a3bc92..3d148a6 100644 --- a/include/json/config.h +++ b/include/json/config.h @@ -119,23 +119,23 @@ extern JSON_API int msvc_pre1900_c99_snprintf(char* outBuf, size_t size, #endif // if !defined(JSON_IS_AMALGAMATION) namespace Json { -typedef int Int; -typedef unsigned int UInt; +using Int = int; +using UInt = unsigned int; #if defined(JSON_NO_INT64) -typedef int LargestInt; -typedef unsigned int LargestUInt; +using LargestInt = int; +using LargestUInt = unsigned int; #undef JSON_HAS_INT64 #else // if defined(JSON_NO_INT64) // For Microsoft Visual use specific types as long long is not supported #if defined(_MSC_VER) // Microsoft Visual Studio -typedef __int64 Int64; -typedef unsigned __int64 UInt64; +using Int64 = __int64; +using UInt64 = unsigned __int64; #else // if defined(_MSC_VER) // Other platforms, use long long -typedef int64_t Int64; -typedef uint64_t UInt64; +using Int64 = int64_t; +using UInt64 = uint64_t; #endif // if defined(_MSC_VER) -typedef Int64 LargestInt; -typedef UInt64 LargestUInt; +using LargestInt = Int64; +using LargestUInt = UInt64; #define JSON_HAS_INT64 #endif // if defined(JSON_NO_INT64) diff --git a/include/json/forwards.h b/include/json/forwards.h index b0d981b..affe33a 100644 --- a/include/json/forwards.h +++ b/include/json/forwards.h @@ -29,7 +29,7 @@ class CharReaderBuilder; class Features; // value.h -typedef unsigned int ArrayIndex; +using ArrayIndex = unsigned int; class StaticString; class Path; class PathArgument; diff --git a/include/json/reader.h b/include/json/reader.h index 1540ac3..9175466 100644 --- a/include/json/reader.h +++ b/include/json/reader.h @@ -36,8 +36,8 @@ namespace Json { class JSONCPP_DEPRECATED( "Use CharReader and CharReaderBuilder instead.") JSON_API Reader { public: - typedef char Char; - typedef const Char* Location; + using Char = char; + using Location = const Char*; /** \brief An error tagged with where in the JSON text it was encountered. * @@ -187,7 +187,7 @@ private: Location extra_; }; - typedef std::deque Errors; + using Errors = std::deque; bool readToken(Token& token); void skipSpaces(); @@ -226,7 +226,7 @@ private: static bool containsNewLine(Location begin, Location end); static String normalizeEOL(Location begin, Location end); - typedef std::stack Nodes; + using Nodes = std::stack; Nodes nodes_; Errors errors_; String document_; diff --git a/include/json/value.h b/include/json/value.h index c4b29b9..e169c3c 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -176,21 +176,21 @@ class JSON_API Value { friend class ValueIteratorBase; public: - typedef std::vector Members; - typedef ValueIterator iterator; - typedef ValueConstIterator const_iterator; - typedef Json::UInt UInt; - typedef Json::Int Int; + using Members = std::vector; + using iterator = ValueIterator; + using const_iterator = ValueConstIterator; + using UInt = Json::UInt; + using Int = Json::Int; #if defined(JSON_HAS_INT64) - typedef Json::UInt64 UInt64; - typedef Json::Int64 Int64; + using UInt64 = Json::UInt64; + using Int64 = Json::Int64; #endif // defined(JSON_HAS_INT64) - typedef Json::LargestInt LargestInt; - typedef Json::LargestUInt LargestUInt; - typedef Json::ArrayIndex ArrayIndex; + using LargestInt = Json::LargestInt; + using LargestUInt = Json::LargestUInt; + using ArrayIndex = Json::ArrayIndex; // Required for boost integration, e. g. BOOST_TEST - typedef std::string value_type; + using value_type = std::string; #if JSON_USE_NULLREF // Binary compatibility kludges, do not use. @@ -710,8 +710,8 @@ public: Value& make(Value& root) const; private: - typedef std::vector InArgs; - typedef std::vector Args; + using InArgs = std::vector; + using Args = std::vector; void makePath(const String& path, const InArgs& in); void addPathInArg(const String& path, const InArgs& in, @@ -726,10 +726,10 @@ private: */ class JSON_API ValueIteratorBase { public: - typedef std::bidirectional_iterator_tag iterator_category; - typedef unsigned int size_t; - typedef int difference_type; - typedef ValueIteratorBase SelfType; + using iterator_category = std::bidirectional_iterator_tag; + using size_t = unsigned int; + using difference_type = int; + using SelfType = ValueIteratorBase; bool operator==(const SelfType& other) const { return isEqual(other); } @@ -802,12 +802,12 @@ class JSON_API ValueConstIterator : public ValueIteratorBase { friend class Value; public: - typedef const Value value_type; + using value_type = const Value; // typedef unsigned int size_t; // typedef int difference_type; - typedef const Value& reference; - typedef const Value* pointer; - typedef ValueConstIterator SelfType; + using reference = const Value&; + using pointer = const Value*; + using SelfType = ValueConstIterator; ValueConstIterator(); ValueConstIterator(ValueIterator const& other); @@ -853,12 +853,12 @@ class JSON_API ValueIterator : public ValueIteratorBase { friend class Value; public: - typedef Value value_type; - typedef unsigned int size_t; - typedef int difference_type; - typedef Value& reference; - typedef Value* pointer; - typedef ValueIterator SelfType; + using value_type = Value; + using size_t = unsigned int; + using difference_type = int; + using reference = Value&; + using pointer = Value*; + using SelfType = ValueIterator; ValueIterator(); explicit ValueIterator(const ValueConstIterator& other); diff --git a/include/json/writer.h b/include/json/writer.h index a72c06a..fb0852a 100644 --- a/include/json/writer.h +++ b/include/json/writer.h @@ -252,7 +252,7 @@ private: static bool hasCommentForValue(const Value& value); static String normalizeEOL(const String& text); - typedef std::vector ChildValues; + using ChildValues = std::vector; ChildValues childValues_; String document_; @@ -326,7 +326,7 @@ private: static bool hasCommentForValue(const Value& value); static String normalizeEOL(const String& text); - typedef std::vector ChildValues; + using ChildValues = std::vector; ChildValues childValues_; OStream* document_; diff --git a/src/jsontestrunner/main.cpp b/src/jsontestrunner/main.cpp index dbfbe98..f6b1a4d 100644 --- a/src/jsontestrunner/main.cpp +++ b/src/jsontestrunner/main.cpp @@ -58,7 +58,7 @@ static Json::String readInputTestFile(const char* path) { return ""; fseek(file, 0, SEEK_END); long const size = ftell(file); - size_t const usize = static_cast(size); + const auto usize = static_cast(size); fseek(file, 0, SEEK_SET); char* buffer = new char[size + 1]; buffer[size] = 0; diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index 63b6937..87483da 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -54,7 +54,7 @@ namespace Json { #if __cplusplus >= 201103L || (defined(_CPPLIB_VER) && _CPPLIB_VER >= 520) using CharReaderPtr = std::unique_ptr; #else -typedef std::auto_ptr CharReaderPtr; +using CharReaderPtr = std::auto_ptr; #endif // Implementation of class Features diff --git a/src/lib_json/json_tool.h b/src/lib_json/json_tool.h index 5c13f1f..2d7b7d9 100644 --- a/src/lib_json/json_tool.h +++ b/src/lib_json/json_tool.h @@ -71,7 +71,7 @@ enum { }; // Defines a char buffer for use with uintToString(). -typedef char UIntToStringBuffer[uintToStringBufferSize]; +using UIntToStringBuffer = char[uintToStringBufferSize]; /** Converts an unsigned integer to string. * @param value Unsigned integer to convert to string diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 2353807..1e3bbde 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -117,7 +117,7 @@ static inline char* duplicateStringValue(const char* value, size_t length) { if (length >= static_cast(Value::maxInt)) length = Value::maxInt - 1; - char* newString = static_cast(malloc(length + 1)); + auto newString = static_cast(malloc(length + 1)); if (newString == nullptr) { throwRuntimeError("in Json::Value::duplicateStringValue(): " "Failed to allocate string value buffer"); @@ -137,8 +137,8 @@ static inline char* duplicateAndPrefixStringValue(const char* value, sizeof(unsigned) - 1U, "in Json::Value::duplicateAndPrefixStringValue(): " "length too big for prefixing"); - unsigned actualLength = length + static_cast(sizeof(unsigned)) + 1U; - char* newString = static_cast(malloc(actualLength)); + size_t actualLength = sizeof(length) + length + 1; + auto newString = static_cast(malloc(actualLength)); if (newString == nullptr) { throwRuntimeError("in Json::Value::duplicateAndPrefixStringValue(): " "Failed to allocate string value buffer"); @@ -518,9 +518,10 @@ bool Value::operator<(const Value& other) const { } case arrayValue: case objectValue: { - int delta = int(value_.map_->size() - other.value_.map_->size()); - if (delta) - return delta < 0; + auto thisSize = value_.map_->size(); + auto otherSize = other.value_.map_->size(); + if (thisSize != otherSize) + return thisSize < otherSize; return (*value_.map_) < (*other.value_.map_); } default: diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index 519ce23..8e06cca 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -86,7 +86,7 @@ namespace Json { #if __cplusplus >= 201103L || (defined(_CPPLIB_VER) && _CPPLIB_VER >= 520) using StreamWriterPtr = std::unique_ptr; #else -typedef std::auto_ptr StreamWriterPtr; +using StreamWriterPtr = std::auto_ptr; #endif String valueToString(LargestInt value) { diff --git a/src/test_lib_json/fuzz.cpp b/src/test_lib_json/fuzz.cpp index fe515b1..68f839d 100644 --- a/src/test_lib_json/fuzz.cpp +++ b/src/test_lib_json/fuzz.cpp @@ -45,7 +45,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { std::unique_ptr reader(builder.newCharReader()); Json::Value root; - const char* data_str = reinterpret_cast(data); + const auto data_str = reinterpret_cast(data); try { reader->parse(data_str, data_str + size, &root, nullptr); } catch (Json::Exception const&) { diff --git a/src/test_lib_json/jsontest.h b/src/test_lib_json/jsontest.h index e076f7c..8c3aa5e 100644 --- a/src/test_lib_json/jsontest.h +++ b/src/test_lib_json/jsontest.h @@ -42,7 +42,7 @@ public: /// Must be a POD to allow inline initialisation without stepping /// into the debugger. struct PredicateContext { - typedef unsigned int Id; + using Id = unsigned int; Id id_; const char* file_; unsigned int line_; @@ -102,7 +102,7 @@ private: static Json::String indentText(const Json::String& text, const Json::String& indent); - typedef std::deque Failures; + using Failures = std::deque; Failures failures_; Json::String name_; PredicateContext rootPredicateNode_; @@ -129,7 +129,7 @@ private: }; /// Function pointer type for TestCase factory -typedef TestCase* (*TestCaseFactory)(); +using TestCaseFactory = TestCase* (*)(); class Runner { public: @@ -168,7 +168,7 @@ private: static void preventDialogOnCrash(); private: - typedef std::deque Factories; + using Factories = std::deque; Factories tests_; };