Skip to content

Commit 38872b5

Browse files
authored
Merge pull request #2896 from vweijsters/fix-2894
Fixed crash in SA1134 code fix
2 parents a0971c9 + 1148a55 commit 38872b5

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1134CodeFixProvider.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,23 @@ public override FixAllProvider GetFixAllProvider()
3333
}
3434

3535
/// <inheritdoc/>
36-
public override Task RegisterCodeFixesAsync(CodeFixContext context)
36+
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
3737
{
38+
var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
39+
3840
foreach (var diagnostic in context.Diagnostics)
3941
{
40-
context.RegisterCodeFix(
41-
CodeAction.Create(
42-
ReadabilityResources.SA1134CodeFix,
43-
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken),
44-
nameof(SA1134CodeFixProvider)),
45-
diagnostic);
42+
// Do not offer the code fix if the error is found at an invalid node (like IncompleteMemberSyntax)
43+
if (syntaxRoot.FindNode(diagnostic.Location.SourceSpan) is AttributeListSyntax)
44+
{
45+
context.RegisterCodeFix(
46+
CodeAction.Create(
47+
ReadabilityResources.SA1134CodeFix,
48+
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken),
49+
nameof(SA1134CodeFixProvider)),
50+
diagnostic);
51+
}
4652
}
47-
48-
return SpecializedTasks.CompletedTask;
4953
}
5054

5155
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1134UnitTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,5 +409,35 @@ public class TestClass<[Test(""Test1"")][Test(""Test2"")]T>
409409

410410
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
411411
}
412+
413+
/// <summary>
414+
/// Verifies that passing an invalid member syntax into the codefix will not change the code.
415+
/// </summary>
416+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
417+
[Fact]
418+
[WorkItem(2894, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2894")]
419+
public async Task VerifyInvalidMemberSyntaxInCodeFixAsync()
420+
{
421+
string testCode = @"class Program
422+
{
423+
static void Main(string[] args)
424+
{
425+
{
426+
}[;]
427+
}
428+
}
429+
";
430+
431+
DiagnosticResult[] expected =
432+
{
433+
DiagnosticResult.CompilerError("CS1513").WithLocation(6, 10),
434+
Diagnostic().WithLocation(6, 10),
435+
DiagnosticResult.CompilerError("CS1001").WithLocation(6, 11),
436+
DiagnosticResult.CompilerError("CS1001").WithLocation(6, 11),
437+
DiagnosticResult.CompilerError("CS1022").WithLocation(8, 1),
438+
};
439+
440+
await VerifyCSharpFixAsync(testCode, expected, testCode, CancellationToken.None).ConfigureAwait(false);
441+
}
412442
}
413443
}

0 commit comments

Comments
 (0)