Skip to content

Commit cece261

Browse files
committed
Fix invalid cast in SA1129 code fix
1 parent 1566e36 commit cece261

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1129UnitTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public async Task VerifyInvalidValueTypeCreationAsync()
9090
public void TestMethod()
9191
{
9292
var v1 = new TestStruct();
93+
94+
System.Console.WriteLine(new TestStruct());
9395
}
9496
9597
private struct TestStruct
@@ -104,6 +106,8 @@ private struct TestStruct
104106
public void TestMethod()
105107
{
106108
var v1 = default(TestStruct);
109+
110+
System.Console.WriteLine(default(TestStruct));
107111
}
108112
109113
private struct TestStruct
@@ -115,7 +119,8 @@ private struct TestStruct
115119

116120
DiagnosticResult[] expected =
117121
{
118-
this.CSharpDiagnostic().WithLocation(5, 18)
122+
this.CSharpDiagnostic().WithLocation(5, 18),
123+
this.CSharpDiagnostic().WithLocation(7, 34),
119124
};
120125

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

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1129CodeFixProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
5050
{
5151
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
5252

53-
var newExpression = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
53+
var newExpression = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
5454
var newSyntaxRoot = syntaxRoot.ReplaceNode(newExpression, GetReplacementNode(newExpression));
5555
return document.WithSyntaxRoot(newSyntaxRoot);
5656
}
@@ -82,7 +82,7 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
8282

8383
var syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
8484

85-
var nodes = diagnostics.Select(diagnostic => syntaxRoot.FindNode(diagnostic.Location.SourceSpan));
85+
var nodes = diagnostics.Select(diagnostic => syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true));
8686

8787
return syntaxRoot.ReplaceNodes(nodes, (originalNode, rewrittenNode) => GetReplacementNode(rewrittenNode));
8888
}

0 commit comments

Comments
 (0)