|
| 1 | +## SA1137 |
| 2 | + |
| 3 | +<table> |
| 4 | +<tr> |
| 5 | + <td>TypeName</td> |
| 6 | + <td>SA1137ElementsShouldHaveTheSameIndentation</td> |
| 7 | +</tr> |
| 8 | +<tr> |
| 9 | + <td>CheckId</td> |
| 10 | + <td>SA1137</td> |
| 11 | +</tr> |
| 12 | +<tr> |
| 13 | + <td>Category</td> |
| 14 | + <td>Readability Rules</td> |
| 15 | +</tr> |
| 16 | +</table> |
| 17 | + |
| 18 | +:memo: This rule is new for StyleCop Analyzers, and was not present in StyleCop Classic. |
| 19 | + |
| 20 | +## Cause |
| 21 | + |
| 22 | +Two sibling elements which each start on their own line have different levels of indentation. |
| 23 | + |
| 24 | +## Rule description |
| 25 | + |
| 26 | +A violation of this rule occurs when two or more sibling elements each start on their own line but are not indented the |
| 27 | +same amount. |
| 28 | + |
| 29 | +For example, the following code would produce a violation of this rule: |
| 30 | + |
| 31 | +```csharp |
| 32 | +public void MethodName() |
| 33 | +{ |
| 34 | + A(); |
| 35 | + B(); // SA1137: Expected the indentation to match the previous line |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +The following code would not produce any violations: |
| 40 | + |
| 41 | +```csharp |
| 42 | +public void MethodName() |
| 43 | +{ |
| 44 | + A(); |
| 45 | + B(); |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +Note that relative indentation and indentation in independent groups of siblings is not checked by this rule. The |
| 50 | +following code shows a more complex example which would not produce any violations: |
| 51 | + |
| 52 | +```csharp |
| 53 | +public void Method1() |
| 54 | +{ |
| 55 | + A(); |
| 56 | + B(); |
| 57 | +} |
| 58 | + |
| 59 | +public void Method2() |
| 60 | +{ |
| 61 | + A(); |
| 62 | + B(); |
| 63 | +} |
| 64 | + |
| 65 | +public void Method3() |
| 66 | +{ |
| 67 | +A(); |
| 68 | +B(); |
| 69 | +} |
| 70 | + |
| 71 | +public void Method4() |
| 72 | + { |
| 73 | + A(); |
| 74 | + B(); |
| 75 | + } |
| 76 | +``` |
| 77 | + |
| 78 | +### Attributes |
| 79 | + |
| 80 | +Attribute lists are evaluated by this rule as individual elements at the same level as the element they are applied to. |
| 81 | +However, when determining the expected indentation for sibling elements, attribute lists are given lower priority. In |
| 82 | +other words, the first element in the same group which starts on its own line, if one exists, determines the expected |
| 83 | +indentation for the group. If no such element exists, the indentation of the first attribute list is used instead. For |
| 84 | +example, the following code shows locations where SA1137 is reported relative to attribute lists. |
| 85 | + |
| 86 | +```csharp |
| 87 | + [Obsolete] // SA1137 (expected no indentation) |
| 88 | +public void Method() // OK (this line establishes indentation for the group) |
| 89 | +{ |
| 90 | +} |
| 91 | + |
| 92 | + public Task MethodAsync() // SA1137 (expected no indentation) |
| 93 | + { // OK (not part of the analysis group) |
| 94 | + } |
| 95 | +``` |
| 96 | + |
| 97 | +The following example shows a case where the indentation of an attribute list *is* used for the group. |
| 98 | + |
| 99 | +```csharp |
| 100 | +public void Method(int x, // Ignored (parameter does not start on its own line) |
| 101 | + [In] int y, // OK (this line establishes indentation for the group) |
| 102 | + [In] int z) // SA1137 |
| 103 | +{ |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +### Labels |
| 108 | + |
| 109 | +Labels which appear within a block statement are evaluated in a special manner. Each label within a block is expected to |
| 110 | +have the same indentation, even if that indentation differs from the indentation used by statements within the block. |
| 111 | +The specific indentation of labels relative to other statements is not examined for this rule. |
| 112 | + |
| 113 | +The following example shows a block with statements and labels. |
| 114 | + |
| 115 | +```csharp |
| 116 | +public int MethodName() |
| 117 | +{ |
| 118 | + int x; |
| 119 | + |
| 120 | + beginning: // OK (label indentation may differ from other statements) |
| 121 | + x = 3; // SA1137 (should be indented four spaces to match 'int x;' above) |
| 122 | +
|
| 123 | +end: // SA1137 (should be indented two spaces to match 'beginning:' above) |
| 124 | + return x; |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +Inside of a `switch` statement, the `case` and `default` labels form an additional category. The specific rules in this |
| 129 | +case are: |
| 130 | + |
| 131 | +1. `case` and `default` labels in a `switch` statement need to use the same indentation. |
| 132 | +2. Other labels in a `switch` statement need to use the same indentation (but this may differ from the indentation of |
| 133 | + `case` labels) |
| 134 | +3. Statements in a `switch` statement need to use the same indentation (but this may differ from both of the above |
| 135 | + labels). |
| 136 | + |
| 137 | +## How to fix violations |
| 138 | + |
| 139 | +To fix a violation of this rule, adjust the indentation of sibling elements to match. |
| 140 | + |
| 141 | +## How to suppress violations |
| 142 | + |
| 143 | +```csharp |
| 144 | +class TypeName { } |
| 145 | + |
| 146 | +#pragma warning disable SA1137 // Elements should have the same indentation |
| 147 | + class Indented { } |
| 148 | +#pragma warning restore SA1137 // Elements should have the same indentation |
| 149 | +``` |
0 commit comments