Skip to content

Commit d91b4fe

Browse files
authored
Merge pull request #2431 from sharwell/sa1006-coverage
Improve test coverage of SA1006
2 parents 3906604 + 02ec78e commit d91b4fe

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1006UnitTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ more invalid text
106106
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
107107
}
108108

109+
[Fact]
110+
public async Task TestMissingDirectiveNameOnLastLineAsync()
111+
{
112+
string testCode = @"
113+
class ClassName
114+
{
115+
}
116+
# ";
117+
118+
DiagnosticResult expected = this.CSharpCompilerError("CS1024").WithMessage("Preprocessor directive expected").WithLocation(5, 1);
119+
120+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
121+
}
122+
109123
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
110124
{
111125
yield return new SA1006PreprocessorKeywordsMustNotBePrecededBySpace();

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1006PreprocessorKeywordsMustNotBePrecededBySpace.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.SpacingRules
88
using Microsoft.CodeAnalysis;
99
using Microsoft.CodeAnalysis.CSharp;
1010
using Microsoft.CodeAnalysis.Diagnostics;
11+
using StyleCop.Analyzers.Helpers;
1112

1213
/// <summary>
1314
/// A C# preprocessor-type keyword is preceded by space.
@@ -76,18 +77,13 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
7677

7778
private static void HandleHashToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
7879
{
79-
if (token.IsMissing)
80-
{
81-
return;
82-
}
83-
8480
if (!token.HasTrailingTrivia || token.TrailingTrivia.Any(SyntaxKind.EndOfLineTrivia))
8581
{
8682
return;
8783
}
8884

8985
SyntaxToken targetToken = token.GetNextToken(includeDirectives: true);
90-
if (targetToken.IsMissing)
86+
if (targetToken.IsMissingOrDefault())
9187
{
9288
return;
9389
}

0 commit comments

Comments
 (0)