• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. title:: clang-tidy - readability-isolate-declaration
2
3readability-isolate-declaration
4===============================
5
6Detects local variable declarations declaring more than one variable and
7tries to refactor the code to one statement per declaration.
8
9The automatic code-transformation will use the same indentation as the original
10for every created statement and add a line break after each statement.
11It keeps the order of the variable declarations consistent, too.
12
13.. code-block:: c++
14
15  void f() {
16    int * pointer = nullptr, value = 42, * const const_ptr = &value;
17    // This declaration will be diagnosed and transformed into:
18    // int * pointer = nullptr;
19    // int value = 42;
20    // int * const const_ptr = &value;
21  }
22
23
24The check excludes places where it is necessary or common to declare
25multiple variables in one statement and there is no other way supported in the
26language. Please note that structured bindings are not considered.
27
28.. code-block:: c++
29
30  // It is not possible to transform this declaration and doing the declaration
31  // before the loop will increase the scope of the variable 'Begin' and 'End'
32  // which is undesirable.
33  for (int Begin = 0, End = 100; Begin < End; ++Begin);
34  if (int Begin = 42, Result = some_function(Begin); Begin == Result);
35
36  // It is not possible to transform this declaration because the result is
37  // not functionality preserving as 'j' and 'k' would not be part of the
38  // 'if' statement anymore.
39  if (SomeCondition())
40    int i = 42, j = 43, k = function(i,j);
41
42
43Limitations
44-----------
45
46Global variables and member variables are excluded.
47
48The check currently does not support the automatic transformation of
49member-pointer-types.
50
51.. code-block:: c++
52
53  struct S {
54    int a;
55    const int b;
56    void f() {}
57  };
58
59  void f() {
60    // Only a diagnostic message is emitted
61    int S::*p = &S::a, S::*const q = &S::a;
62  }
63
64Furthermore, the transformation is very cautious when it detects various kinds
65of macros or preprocessor directives in the range of the statement. In this
66case the transformation will not happen to avoid unexpected side-effects due to
67macros.
68
69.. code-block:: c++
70
71  #define NULL 0
72  #define MY_NICE_TYPE int **
73  #define VAR_NAME(name) name##__LINE__
74  #define A_BUNCH_OF_VARIABLES int m1 = 42, m2 = 43, m3 = 44;
75
76  void macros() {
77    int *p1 = NULL, *p2 = NULL;
78    // Will be transformed to
79    // int *p1 = NULL;
80    // int *p2 = NULL;
81
82    MY_NICE_TYPE p3, v1, v2;
83    // Won't be transformed, but a diagnostic is emitted.
84
85    int VAR_NAME(v3),
86        VAR_NAME(v4),
87        VAR_NAME(v5);
88    // Won't be transformed, but a diagnostic is emitted.
89
90    A_BUNCH_OF_VARIABLES
91    // Won't be transformed, but a diagnostic is emitted.
92
93    int Unconditional,
94  #if CONFIGURATION
95        IfConfigured = 42,
96  #else
97        IfConfigured = 0;
98  #endif
99    // Won't be transformed, but a diagnostic is emitted.
100  }
101