diff options
Diffstat (limited to 'clang-tools-extra/docs/clang-tidy/checks')
5 files changed, 167 insertions, 142 deletions
diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index cb68092d9fa..346612a09bc 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -150,7 +150,7 @@ Clang-Tidy Checks mpi-type-mismatch performance-faster-string-find performance-for-range-copy - performance-implicit-cast-in-loop + performance-implicit-conversion-in-loop performance-inefficient-string-concatenation performance-inefficient-vector-operation performance-type-promotion-in-math-fn @@ -164,7 +164,7 @@ Clang-Tidy Checks readability-else-after-return readability-function-size readability-identifier-naming - readability-implicit-bool-cast + readability-implicit-bool-conversion readability-inconsistent-declaration-parameter-name readability-misleading-indentation readability-misplaced-array-index diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance-implicit-cast-in-loop.rst b/clang-tools-extra/docs/clang-tidy/checks/performance-implicit-cast-in-loop.rst index 7a5cdf4193e..280e7c046f4 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance-implicit-cast-in-loop.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance-implicit-cast-in-loop.rst @@ -1,21 +1,12 @@ +:orphan: + .. title:: clang-tidy - performance-implicit-cast-in-loop +.. meta:: + :http-equiv=refresh: 5;URL=performance-implicit-conversion-in-loop.html performance-implicit-cast-in-loop ================================= -This warning appears in a range-based loop with a loop variable of const ref -type where the type of the variable does not match the one returned by the -iterator. This means that an implicit cast has been added, which can for example -result in expensive deep copies. - -Example: - -.. code-block:: c++ - - map<int, vector<string>> my_map; - for (const pair<int, vector<string>>& p : my_map) {} - // The iterator type is in fact pair<const int, vector<string>>, which means - // that the compiler added a cast, resulting in a copy of the vectors. +This check has been renamed to `performance-implicit-conversion-in-loop +<performance-implicit-conversion-in-loop.html>`_. -The easiest solution is usually to use ``const auto&`` instead of writing the type -manually. diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance-implicit-conversion-in-loop.rst b/clang-tools-extra/docs/clang-tidy/checks/performance-implicit-conversion-in-loop.rst new file mode 100644 index 00000000000..14e4d31f974 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/performance-implicit-conversion-in-loop.rst @@ -0,0 +1,21 @@ +.. title:: clang-tidy - performance-implicit-conversion-in-loop + +performance-implicit-conversion-in-loop +======================================= + +This warning appears in a range-based loop with a loop variable of const ref +type where the type of the variable does not match the one returned by the +iterator. This means that an implicit conversion happens, which can for example +result in expensive deep copies. + +Example: + +.. code-block:: c++ + + map<int, vector<string>> my_map; + for (const pair<int, vector<string>>& p : my_map) {} + // The iterator type is in fact pair<const int, vector<string>>, which means + // that the compiler added a conversion, resulting in a copy of the vectors. + +The easiest solution is usually to use ``const auto&`` instead of writing the +type manually. diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability-implicit-bool-cast.rst b/clang-tools-extra/docs/clang-tidy/checks/readability-implicit-bool-cast.rst index bcd23abf74d..a6a3492681d 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/readability-implicit-bool-cast.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/readability-implicit-bool-cast.rst @@ -1,130 +1,11 @@ +:orphan: + .. title:: clang-tidy - readability-implicit-bool-cast +.. meta:: + :http-equiv=refresh: 5;URL=readability-implicit-bool-conversion.html readability-implicit-bool-cast ============================== -This check can be used to find implicit conversions between built-in types and -booleans. Depending on use case, it may simply help with readability of the code, -or in some cases, point to potential bugs which remain unnoticed due to implicit -conversions. - -The following is a real-world example of bug which was hiding behind implicit -``bool`` cast: - -.. code-block:: c++ - - class Foo { - int m_foo; - - public: - void setFoo(bool foo) { m_foo = foo; } // warning: implicit cast bool -> int - int getFoo() { return m_foo; } - }; - - void use(Foo& foo) { - bool value = foo.getFoo(); // warning: implicit cast int -> bool - } - -This code is the result of unsuccessful refactoring, where type of ``m_foo`` -changed from ``bool`` to ``int``. The programmer forgot to change all -occurrences of ``bool``, and the remaining code is no longer correct, yet it -still compiles without any visible warnings. - -In addition to issuing warnings, fix-it hints are provided to help solve the -reported issues. This can be used for improving readability of code, for -example: - -.. code-block:: c++ - - void conversionsToBool() { - float floating; - bool boolean = floating; - // ^ propose replacement: bool boolean = floating != 0.0f; - - int integer; - if (integer) {} - // ^ propose replacement: if (integer != 0) {} - - int* pointer; - if (!pointer) {} - // ^ propose replacement: if (pointer == nullptr) {} - - while (1) {} - // ^ propose replacement: while (true) {} - } - - void functionTakingInt(int param); - - void conversionsFromBool() { - bool boolean; - functionTakingInt(boolean); - // ^ propose replacement: functionTakingInt(static_cast<int>(boolean)); - - functionTakingInt(true); - // ^ propose replacement: functionTakingInt(1); - } - -In general, the following cast types are checked: - -- integer expression/literal to boolean, - -- floating expression/literal to boolean, - -- pointer/pointer to member/``nullptr``/``NULL`` to boolean, - -- boolean expression/literal to integer, - -- boolean expression/literal to floating. - -The rules for generating fix-it hints are: - -- in case of casts from other built-in type to bool, an explicit comparison - is proposed to make it clear what exaclty is being compared: - - - ``bool boolean = floating;`` is changed to - ``bool boolean = floating == 0.0f;``, - - - for other types, appropriate literals are used (``0``, ``0u``, ``0.0f``, - ``0.0``, ``nullptr``), - -- in case of negated expressions cast to bool, the proposed replacement with - comparison is simplified: - - - ``if (!pointer)`` is changed to ``if (pointer == nullptr)``, - -- in case of casts from bool to other built-in types, an explicit ``static_cast`` - is proposed to make it clear that a cast is taking place: - - - ``int integer = boolean;`` is changed to - ``int integer = static_cast<int>(boolean);``, - -- if the cast is performed on type literals, an equivalent literal is proposed, - according to what type is actually expected, for example: - - - ``functionTakingBool(0);`` is changed to ``functionTakingBool(false);``, - - - ``functionTakingInt(true);`` is changed to ``functionTakingInt(1);``, - - - for other types, appropriate literals are used (``false``, ``true``, ``0``, - ``1``, ``0u``, ``1u``, ``0.0f``, ``1.0f``, ``0.0``, ``1.0f``). - -Some additional accommodations are made for pre-C++11 dialects: - -- ``false`` literal cast to pointer is detected, - -- instead of ``nullptr`` literal, ``0`` is proposed as replacement. - -Occurrences of implicit casts inside macros and template instantiations are -deliberately ignored, as it is not clear how to deal with such cases. - -Options -------- - -.. option:: AllowConditionalIntegerCasts - - When non-zero, the check will allow conditional integer casts. Default is - `0`. - -.. option:: AllowConditionalPointerCasts - - When non-zero, the check will allow conditional pointer casts. Default is `0`. +This check has been renamed to `readability-implicit-bool-conversion +<readability-implicit-bool-conversion.html>`_. diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability-implicit-bool-conversion.rst b/clang-tools-extra/docs/clang-tidy/checks/readability-implicit-bool-conversion.rst new file mode 100644 index 00000000000..64f3f73673c --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/readability-implicit-bool-conversion.rst @@ -0,0 +1,132 @@ +.. title:: clang-tidy - readability-implicit-bool-conversion + +readability-implicit-bool-conversion +==================================== + +This check can be used to find implicit conversions between built-in types and +booleans. Depending on use case, it may simply help with readability of the code, +or in some cases, point to potential bugs which remain unnoticed due to implicit +conversions. + +The following is a real-world example of bug which was hiding behind implicit +``bool`` conversion: + +.. code-block:: c++ + + class Foo { + int m_foo; + + public: + void setFoo(bool foo) { m_foo = foo; } // warning: implicit conversion bool -> int + int getFoo() { return m_foo; } + }; + + void use(Foo& foo) { + bool value = foo.getFoo(); // warning: implicit conversion int -> bool + } + +This code is the result of unsuccessful refactoring, where type of ``m_foo`` +changed from ``bool`` to ``int``. The programmer forgot to change all +occurrences of ``bool``, and the remaining code is no longer correct, yet it +still compiles without any visible warnings. + +In addition to issuing warnings, fix-it hints are provided to help solve the +reported issues. This can be used for improving readability of code, for +example: + +.. code-block:: c++ + + void conversionsToBool() { + float floating; + bool boolean = floating; + // ^ propose replacement: bool boolean = floating != 0.0f; + + int integer; + if (integer) {} + // ^ propose replacement: if (integer != 0) {} + + int* pointer; + if (!pointer) {} + // ^ propose replacement: if (pointer == nullptr) {} + + while (1) {} + // ^ propose replacement: while (true) {} + } + + void functionTakingInt(int param); + + void conversionsFromBool() { + bool boolean; + functionTakingInt(boolean); + // ^ propose replacement: functionTakingInt(static_cast<int>(boolean)); + + functionTakingInt(true); + // ^ propose replacement: functionTakingInt(1); + } + +In general, the following conversion types are checked: + +- integer expression/literal to boolean, + +- floating expression/literal to boolean, + +- pointer/pointer to member/``nullptr``/``NULL`` to boolean, + +- boolean expression/literal to integer, + +- boolean expression/literal to floating. + +The rules for generating fix-it hints are: + +- in case of conversions from other built-in type to bool, an explicit + comparison is proposed to make it clear what exaclty is being compared: + + - ``bool boolean = floating;`` is changed to + ``bool boolean = floating == 0.0f;``, + + - for other types, appropriate literals are used (``0``, ``0u``, ``0.0f``, + ``0.0``, ``nullptr``), + +- in case of negated expressions conversion to bool, the proposed replacement + with comparison is simplified: + + - ``if (!pointer)`` is changed to ``if (pointer == nullptr)``, + +- in case of conversions from bool to other built-in types, an explicit + ``static_cast`` is proposed to make it clear that a conversion is taking + place: + + - ``int integer = boolean;`` is changed to + ``int integer = static_cast<int>(boolean);``, + +- if the conversion is performed on type literals, an equivalent literal is + proposed, according to what type is actually expected, for example: + + - ``functionTakingBool(0);`` is changed to ``functionTakingBool(false);``, + + - ``functionTakingInt(true);`` is changed to ``functionTakingInt(1);``, + + - for other types, appropriate literals are used (``false``, ``true``, ``0``, + ``1``, ``0u``, ``1u``, ``0.0f``, ``1.0f``, ``0.0``, ``1.0f``). + +Some additional accommodations are made for pre-C++11 dialects: + +- ``false`` literal conversion to pointer is detected, + +- instead of ``nullptr`` literal, ``0`` is proposed as replacement. + +Occurrences of implicit conversions inside macros and template instantiations +are deliberately ignored, as it is not clear how to deal with such cases. + +Options +------- + +.. option:: AllowIntegerConditions + + When non-zero, the check will allow conditional integer conversions. Default + is `0`. + +.. option:: AllowPointerConditions + + When non-zero, the check will allow conditional pointer conversions. Default + is `0`. |