STYLE: Use modern C++11 style by hjmjohnson · Pull Request #865 · open-source-parsers/jsoncpp (original) (raw)
Run clang-tidy code modernization refactoring tools. The set of scripts used for this repository have been in heavy use on many large repositories that are moving to C++11 support.
cd /Users/johnsonhj/src/jsoncpp/cmake-build-debug/
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-,modernize-pass-by-value -header-filter=. -fix
The emptiness of a container should be checked using the empty() method instead of the size() method. It is not guaranteed that size() is a constant-time function, and it is generally more efficient and also shows clearer intent to use empty(). Furthermore some containers may implement the empty() method but not implement the size() method. Using empty() whenever possible makes it easier to switch to another container in the future.
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=-,readability-container-size-empty -header-filter=. -fix
hjmjohnson changed the title
PERF: Allow compiler to choose best way to construct a copy STYLE: Use modern C++11 style
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
… expression
This check is responsible for using the auto type specifier for variable declarations to improve code readability and maintainability.
The auto type specifier will only be introduced in situations where the variable type matches the type of the initializer expression. In other words auto should deduce the same type that was originally spelled in the source
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-use-auto -header-filter = . -fix
With move semantics added to the language and the standard library updated with move constructors added for many types it is now interesting to take an argument directly by value, instead of by const-reference, and then copy. This check allows the compiler to take care of choosing the best way to construct the copy.
The transformation is usually beneficial when the calling code passes an rvalue and assumes the move construction is a cheap operation. This short example illustrates how the construction of the value happens:
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-pass-by-value -header-filter=. -fix
Converts a default constructor’s member initializers into the new default member initializers in C++11. Other member initializers that match the default member initializer are removed. This can reduce repeated code or allow use of ‘= default’.
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-use-default-member-init -header-filter=. -fix
This check replaces default bodies of special member functions with = default;. The explicitly defaulted function declarations enable more opportunities in optimization, because the compiler might treat explicitly defaulted functions as trivial.
Additionally, the C++11 use of = default more clearly expreses the intent for the special member functions.
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-use-equals-default -header-filter=. -fix
This check replaces undefined special member functions with = delete;. The explicitly deleted function declarations enable more opportunities in optimization, because the compiler might treat explicitly delted functions as noops.
Additionally, the C++11 use of = delete more clearly expreses the intent for the special member functions.
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-use-equals-delete -header-filter=. -fix
Make the index values consistent with size_t.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed all identified problems.
@BillyDonahue Thank you for your comments. I have addressed all of the items you identified.
I did not fix one item that was a bit out of scope for this set of changes.
If you would please approve this PR, I'd like to get this incorporated.
Sorry I didn't get back to this sooner. Thanks for the merge.