Skip to content

Commit a42e619

Browse files
committed
Update messaging for SA1027 to match new behavior
1 parent 026253a commit a42e619

8 files changed

Lines changed: 59 additions & 34 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1027CodeFixProvider.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ namespace StyleCop.Analyzers.SpacingRules
1818
using Settings.ObjectModel;
1919

2020
/// <summary>
21-
/// Implements a code fix for <see cref="SA1027TabsMustNotBeUsed"/>.
21+
/// Implements a code fix for <see cref="SA1027UseTabsCorrectly"/>.
2222
/// </summary>
2323
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(SA1027CodeFixProvider))]
2424
[Shared]
2525
internal class SA1027CodeFixProvider : CodeFixProvider
2626
{
2727
/// <inheritdoc/>
2828
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
29-
ImmutableArray.Create(SA1027TabsMustNotBeUsed.DiagnosticId);
29+
ImmutableArray.Create(SA1027UseTabsCorrectly.DiagnosticId);
3030

3131
/// <inheritdoc/>
3232
public override FixAllProvider GetFixAllProvider()
@@ -65,9 +65,9 @@ private static TextChange FixDiagnostic(IndentationSettings indentationSettings,
6565

6666
bool useTabs = false;
6767
string behavior;
68-
if (diagnostic.Properties.TryGetValue(SA1027TabsMustNotBeUsed.BehaviorKey, out behavior))
68+
if (diagnostic.Properties.TryGetValue(SA1027UseTabsCorrectly.BehaviorKey, out behavior))
6969
{
70-
useTabs = behavior == SA1027TabsMustNotBeUsed.ConvertToTabsBehavior;
70+
useTabs = behavior == SA1027UseTabsCorrectly.ConvertToTabsBehavior;
7171
}
7272

7373
string text = sourceText.ToString(TextSpan.FromBounds(startLine.Start, span.End));

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1027UnitTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace StyleCop.Analyzers.Test.SpacingRules
1313
using Xunit;
1414

1515
/// <summary>
16-
/// Unit tests for <see cref="SA1027TabsMustNotBeUsed"/>
16+
/// Unit tests for <see cref="SA1027UseTabsCorrectly"/>
1717
/// </summary>
1818
public class SA1027UnitTests : CodeFixVerifier
1919
{
@@ -237,7 +237,7 @@ Comment 2
237237
/// <inheritdoc/>
238238
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
239239
{
240-
yield return new SA1027TabsMustNotBeUsed();
240+
yield return new SA1027UseTabsCorrectly();
241241
}
242242

243243
/// <inheritdoc/>

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1027UseTabsUnitTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace StyleCop.Analyzers.Test.SpacingRules
1414
using Xunit;
1515

1616
/// <summary>
17-
/// Unit tests for <see cref="SA1027TabsMustNotBeUsed"/> when <see cref="IndentationSettings.UseTabs"/> is
17+
/// Unit tests for <see cref="SA1027UseTabsCorrectly"/> when <see cref="IndentationSettings.UseTabs"/> is
1818
/// <see langword="true"/>.
1919
/// </summary>
2020
public class SA1027UseTabsUnitTests : CodeFixVerifier
@@ -251,7 +251,7 @@ protected override string GetSettings() =>
251251
/// <inheritdoc/>
252252
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
253253
{
254-
yield return new SA1027TabsMustNotBeUsed();
254+
yield return new SA1027UseTabsCorrectly();
255255
}
256256

257257
/// <inheritdoc/>

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1027TabsMustNotBeUsed.cs renamed to StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1027UseTabsCorrectly.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,13 @@ namespace StyleCop.Analyzers.SpacingRules
1212
using StyleCop.Analyzers.Settings.ObjectModel;
1313

1414
/// <summary>
15-
/// The C# code contains a tab character.
15+
/// The code contains a tab or space character which is not consistent with the current project settings.
1616
/// </summary>
17-
/// <remarks>
18-
/// <para>A violation of this rule occurs whenever the code contains a tab character.</para>
19-
///
20-
/// <para>Tabs should not be used within C# code, because the length of the tab character can vary depending upon
21-
/// the editor being used to view the code. This can cause the spacing and indexing of the code to vary from the
22-
/// developer's original intention, and can in some cases make the code difficult to read.</para>
23-
///
24-
/// <para>For these reasons, tabs should not be used, and each level of indentation should consist of four spaces.
25-
/// This will ensure that the code looks the same no matter which editor is being used to view the code.</para>
26-
/// </remarks>
2717
[DiagnosticAnalyzer(LanguageNames.CSharp)]
28-
internal class SA1027TabsMustNotBeUsed : DiagnosticAnalyzer
18+
internal class SA1027UseTabsCorrectly : DiagnosticAnalyzer
2919
{
3020
/// <summary>
31-
/// The ID for diagnostics produced by the <see cref="SA1027TabsMustNotBeUsed"/> analyzer.
21+
/// The ID for diagnostics produced by the <see cref="SA1027UseTabsCorrectly"/> analyzer.
3222
/// </summary>
3323
public const string DiagnosticId = "SA1027";
3424

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SpacingResources.Designer.cs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SpacingResources.resx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,13 @@
220220
<value>Replace tabs with spaces</value>
221221
</data>
222222
<data name="SA1027Description" xml:space="preserve">
223-
<value>The C# code contains a tab character</value>
223+
<value>The code contains a tab or space character which is not consistent with the current project settings.</value>
224224
</data>
225225
<data name="SA1027MessageFormat" xml:space="preserve">
226-
<value>Tabs must not be used.</value>
226+
<value>Tabs and spaces should be used correctly</value>
227227
</data>
228228
<data name="SA1027Title" xml:space="preserve">
229-
<value>Tabs must not be used</value>
229+
<value>Use tabs correctly</value>
230230
</data>
231231
<data name="SA1028CodeFix" xml:space="preserve">
232232
<value>Remove trailing whitespace</value>

StyleCop.Analyzers/StyleCop.Analyzers/StyleCop.Analyzers.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@
316316
<Compile Include="SpacingRules\SA1024ColonsMustBeSpacedCorrectly.cs" />
317317
<Compile Include="SpacingRules\SA1025CodeMustNotContainMultipleWhitespaceInARow.cs" />
318318
<Compile Include="SpacingRules\SA1026CodeMustNotContainSpaceAfterNewKeywordInImplicitlyTypedArrayAllocation.cs" />
319-
<Compile Include="SpacingRules\SA1027TabsMustNotBeUsed.cs" />
319+
<Compile Include="SpacingRules\SA1027UseTabsCorrectly.cs" />
320320
<Compile Include="SpacingRules\SA1028CodeMustNotContainTrailingWhitespace.cs" />
321321
<Compile Include="SpacingRules\SpacingResources.Designer.cs">
322322
<AutoGen>True</AutoGen>

documentation/SA1027.md

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<table>
44
<tr>
55
<td>TypeName</td>
6-
<td>SA1027TabsMustNotBeUsed</td>
6+
<td>SA1027UseTabsCorrectly</td>
77
</tr>
88
<tr>
99
<td>CheckId</td>
@@ -17,19 +17,54 @@
1717

1818
## Cause
1919

20-
The C# code contains a tab character.
20+
The code contains a tab or space character which is not consistent with the current project settings.
2121

2222
## Rule description
2323

24-
A violation of this rule occurs whenever the code contains a tab character.
24+
A violation of this rule occurs whenever the code contains a tab or space character which is not consistent with the
25+
current project settings. By default, StyleCop Analyzers does not expect code to contain any tab characters. See
26+
[Configuration.md](Configuration.md) for information about changing this behavior via **stylecop.json**.
2527

26-
Tabs should not be used within C# code, because the length of the tab character can vary depending upon the editor being used to view the code. This can cause the spacing and indexing of the code to vary from the developer's original intention, and can in some cases make the code difficult to read.
28+
In all configurations, this violation is always reported for tab characters that appear after the first non-whitespace
29+
character on a line. When tab characters are used in these positions, it is not possible to ensure that varying tab
30+
widths do not change the layout of code. See [Issue #2035](https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2035)
31+
for more information.
2732

28-
For these reasons, tabs should not be used, and each level of indentation should consist of four spaces. This will ensure that the code looks the same no matter which editor is being used to view the code.
33+
### Default behavior
34+
35+
The default behavior matches the behavior of StyleCop Classic, which was based on the following rationale.
36+
37+
> Tabs should not be used within C# code, because the length of the tab character can vary depending upon the editor being
38+
> used to view the code. This can cause the spacing and indexing of the code to vary from the developer's original
39+
> intention, and can in some cases make the code difficult to read.
40+
>
41+
> For these reasons, tabs should not be used, and each level of indentation should consist of four spaces. This will
42+
> ensure that the code looks the same no matter which editor is being used to view the code.
43+
44+
### Exclusions
45+
46+
To avoid unintentionally changing the behavior of code, this violation is never reported in the following scenarios.
47+
48+
* String literals, including interpolated string literals in C# 6
49+
50+
* Character literals
51+
52+
* Commented code (line comments starting with `////`). This ensures that literals within commented code are not changed.
53+
54+
* Disabled text
55+
56+
```csharp
57+
#if DEBUG
58+
This is ignored in debug configurations
59+
#else
60+
This is ignored in release configurations
61+
#endif
62+
```
2963

3064
## How to fix violations
3165

32-
To fix a violation of this rule, remove the tab character from the code.
66+
To fix a violation of this rule, use spaces or tabs to indent lines according to the project settings, and avoid the use
67+
of tabs aside from indentation.
3368

3469
## How to suppress violations
3570

0 commit comments

Comments
 (0)