diff options
author | Jonas Toth <jonas.toth@gmail.com> | 2018-10-31 16:50:44 +0000 |
---|---|---|
committer | Jonas Toth <jonas.toth@gmail.com> | 2018-10-31 16:50:44 +0000 |
commit | 7bc34bec346f7263537f29be93c0213cd5724f76 (patch) | |
tree | af23d474c7746826b59523ca21c00fb986cc647e /clang-tools-extra/docs | |
parent | 0c62d1bc9a72e69022669f154ab442a655abf903 (diff) |
[clang-tidy] new check 'readability-isolate-declaration'
Summary:
This patch introduces a new clang-tidy check that matches on all `declStmt` that declare more then one variable
and transform them into one statement per declaration if possible.
It currently only focusses on variable declarations but should be extended to cover more kinds of declarations in the future.
It is related to https://reviews.llvm.org/D27621 and does use it's extensive test-suite. Thank you to firolino for his work!
Reviewers: rsmith, aaron.ballman, alexfh, hokein, kbobyrev
Reviewed By: aaron.ballman
Subscribers: ZaMaZaN4iK, mgehre, nemanjai, kbarton, lebedev.ri, Eugene.Zelenko, mgorny, xazax.hun, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D51949
Diffstat (limited to 'clang-tools-extra/docs')
3 files changed, 107 insertions, 0 deletions
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 4b1a6f2b7c8..76bdfb222f0 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -136,6 +136,12 @@ Improvements to clang-tidy Detects usage of the deprecated member types of ``std::ios_base`` and replaces those that have a non-deprecated equivalent. +- New :doc:`readability-isolate-decl + <clang-tidy/checks/readability-isolate-decl>` check. + + Detects local variable declarations declaring more than one variable and + tries to refactor the code to one statement per declaration. + - New :doc:`readability-magic-numbers <clang-tidy/checks/readability-magic-numbers>` check. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index a5b5a4f7780..082ec276af4 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -233,6 +233,7 @@ Clang-Tidy Checks readability-identifier-naming readability-implicit-bool-conversion readability-inconsistent-declaration-parameter-name + readability-isolate-declaration readability-magic-numbers readability-misleading-indentation readability-misplaced-array-index diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability-isolate-declaration.rst b/clang-tools-extra/docs/clang-tidy/checks/readability-isolate-declaration.rst new file mode 100644 index 00000000000..7385f304366 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/readability-isolate-declaration.rst @@ -0,0 +1,100 @@ +.. title:: clang-tidy - readability-isolate-declaration + +readability-isolate-declaration +=============================== + +Detects local variable declarations declaring more than one variable and +tries to refactor the code to one statement per declaration. + +The automatic code-transformation will use the same indentation as the original +for every created statement and add a line break after each statement. +It keeps the order of the variable declarations consistent, too. + +.. code-block:: c++ + + void f() { + int * pointer = nullptr, value = 42, * const const_ptr = &value; + // This declaration will be diagnosed and transformed into: + // int * pointer = nullptr; + // int value = 42; + // int * const const_ptr = &value; + } + + +The check excludes places where it is necessary or common to declare +multiple variables in one statement and there is no other way supported in the +language. Please note that structured bindings are not considered. + +.. code-block:: c++ + + // It is not possible to transform this declaration and doing the declaration + // before the loop will increase the scope of the variable 'Begin' and 'End' + // which is undesirable. + for (int Begin = 0, End = 100; Begin < End; ++Begin); + if (int Begin = 42, Result = some_function(Begin); Begin == Result); + + // It is not possible to transform this declaration because the result is + // not functionality preserving as 'j' and 'k' would not be part of the + // 'if' statement anymore. + if (SomeCondition()) + int i = 42, j = 43, k = function(i,j); + + +Limitations +----------- + +Global variables and member variables are excluded. + +The check currently does not support the automatic transformation of +member-pointer-types. + +.. code-block:: c++ + + struct S { + int a; + const int b; + void f() {} + }; + + void f() { + // Only a diagnostic message is emitted + int S::*p = &S::a, S::*const q = &S::a; + } + +Furthermore, the transformation is very cautious when it detects various kinds +of macros or preprocessor directives in the range of the statement. In this +case the transformation will not happen to avoid unexpected side-effects due to +macros. + +.. code-block:: c++ + + #define NULL 0 + #define MY_NICE_TYPE int ** + #define VAR_NAME(name) name##__LINE__ + #define A_BUNCH_OF_VARIABLES int m1 = 42, m2 = 43, m3 = 44; + + void macros() { + int *p1 = NULL, *p2 = NULL; + // Will be transformed to + // int *p1 = NULL; + // int *p2 = NULL; + + MY_NICE_TYPE p3, v1, v2; + // Won't be transformed, but a diagnostic is emitted. + + int VAR_NAME(v3), + VAR_NAME(v4), + VAR_NAME(v5); + // Won't be transformed, but a diagnostic is emitted. + + A_BUNCH_OF_VARIABLES + // Won't be transformed, but a diagnostic is emitted. + + int Unconditional, + #if CONFIGURATION + IfConfigured = 42, + #else + IfConfigured = 0; + #endif + // Won't be transformed, but a diagnostic is emitted. + } |