Skip to content

Commit dfdd191

Browse files
committed
Ignore hard tabs appearing within commented code
Fixes #1478
1 parent 2926117 commit dfdd191

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,21 @@ public async Task TestInvalidTabsInCommentsAsync()
146146
"\t\tpublic void Bar()\r\n" +
147147
"\t\t{\r\n" +
148148
"\t\t \t//\tComment\t\t1\r\n" +
149+
" ////\tCommented Code\t\t1\r\n" +
149150
"\t \t\t// Comment 2\r\n" +
150151
"\t\t}\r\n" +
151152
"\t}\r\n";
152153

153-
var fixedTestCode = @" public class Foo
154-
{
155-
public void Bar()
156-
{
157-
// Comment 1
158-
// Comment 2
159-
}
160-
}
161-
";
154+
var fixedTestCode =
155+
" public class Foo\r\n" +
156+
" {\r\n" +
157+
" public void Bar()\r\n" +
158+
" {\r\n" +
159+
" // Comment 1\r\n" +
160+
" ////\tCommented Code\t\t1\r\n" +
161+
" // Comment 2\r\n" +
162+
" }\r\n" +
163+
" }\r\n";
162164

163165
DiagnosticResult[] expected =
164166
{
@@ -168,9 +170,9 @@ public void Bar()
168170
this.CSharpDiagnostic().WithLocation(4, 1),
169171
this.CSharpDiagnostic().WithLocation(5, 1),
170172
this.CSharpDiagnostic().WithLocation(5, 5),
171-
this.CSharpDiagnostic().WithLocation(6, 1),
172173
this.CSharpDiagnostic().WithLocation(7, 1),
173174
this.CSharpDiagnostic().WithLocation(8, 1),
175+
this.CSharpDiagnostic().WithLocation(9, 1),
174176
};
175177

176178
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1027TabsMustNotBeUsed.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,16 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
8181

8282
private static void HandleWhitespaceTrivia(SyntaxTreeAnalysisContext context, SyntaxTrivia trivia)
8383
{
84-
if (trivia.ToFullString().IndexOf('\t') < 0)
84+
string fullString = trivia.ToFullString();
85+
if (fullString.StartsWith("////"))
8586
{
87+
// This is a line of commented code.
88+
return;
89+
}
90+
91+
if (fullString.IndexOf('\t') < 0)
92+
{
93+
// No hard tabs were found.
8694
return;
8795
}
8896

0 commit comments

Comments
 (0)