Permalink
Fetching contributors…
Cannot retrieve contributors at this time
79 lines (57 sloc) 2.6 KB

SA1027

TypeName SA1027UseTabsCorrectly
CheckId SA1027
Category Spacing Rules

Cause

The code contains a tab or space character which is not consistent with the current project settings.

Rule description

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

In all configurations, this violation is always reported for tab characters that appear after the first non-whitespace character on a line. When tab characters are used in these positions, it is not possible to ensure that varying tab widths do not change the layout of code. See Issue #2035 for more information.

Default behavior

The default behavior matches the behavior of StyleCop Classic, which was based on the following rationale.

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.

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.

Exclusions

To avoid unintentionally changing the behavior of code, this violation is never reported in the following scenarios.

  • String literals, including interpolated string literals in C# 6

  • Character literals

  • Commented code (line comments starting with ////). This ensures that literals within commented code are not changed.

  • Disabled text

    #if DEBUG
    This is ignored in debug configurations
    #else
    This is ignored in release configurations
    #endif

How to fix violations

To fix a violation of this rule, use spaces or tabs to indent lines according to the project settings, and avoid the use of tabs aside from indentation.

How to suppress violations

[SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1027:TabsMustNotBeUsed", Justification = "Reviewed.")]
#pragma warning disable SA1027 // TabsMustNotBeUsed
#pragma warning restore SA1027 // TabsMustNotBeUsed