Skip to content

Commit ba74887

Browse files
committed
Fixes issue with bulk fixer of SA1500
1 parent 704e326 commit ba74887

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,15 @@ private static LinePosition GetTokenStartLinePosition(SyntaxToken token)
250250

251251
private static void AddReplacement(Dictionary<SyntaxToken, SyntaxToken> tokenReplacements, SyntaxToken originalToken, SyntaxToken replacementToken)
252252
{
253-
tokenReplacements[originalToken] = replacementToken;
253+
if (tokenReplacements.ContainsKey(originalToken))
254+
{
255+
// This will only happen when a single keyword (like else) has invalid brace tokens before and after it.
256+
tokenReplacements[originalToken] = tokenReplacements[originalToken].WithTrailingTrivia(replacementToken.TrailingTrivia);
257+
}
258+
else
259+
{
260+
tokenReplacements[originalToken] = replacementToken;
261+
}
254262
}
255263

256264
private class FixAll : DocumentBasedFixAllProvider

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1500/SA1500UnitTests.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,89 @@ namespace StyleCop.Analyzers.Test.LayoutRules
2020
/// </remarks>
2121
public partial class SA1500UnitTests : CodeFixVerifier
2222
{
23+
/// <summary>
24+
/// Verifies that a complex multiple fix scenario is handled correctly.
25+
/// </summary>
26+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
27+
[Fact]
28+
[WorkItem(2566, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2566")]
29+
public async Task VerifyFixAllAsync()
30+
{
31+
var testCode = @"using System;
32+
public class TestClass
33+
{
34+
public static void Sample()
35+
{
36+
try {
37+
if (false) {
38+
return;
39+
} else if (false) {
40+
return;
41+
} else {
42+
return;
43+
}
44+
} catch (Exception) {
45+
} catch {
46+
} finally {
47+
}
48+
}
49+
}
50+
";
51+
52+
var fixedTestCode = @"using System;
53+
public class TestClass
54+
{
55+
public static void Sample()
56+
{
57+
try
58+
{
59+
if (false)
60+
{
61+
return;
62+
}
63+
else if (false)
64+
{
65+
return;
66+
}
67+
else
68+
{
69+
return;
70+
}
71+
}
72+
catch (Exception)
73+
{
74+
}
75+
catch
76+
{
77+
}
78+
finally
79+
{
80+
}
81+
}
82+
}
83+
";
84+
85+
DiagnosticResult[] expectedDiagnostics =
86+
{
87+
this.CSharpDiagnostic().WithLocation(6, 13),
88+
this.CSharpDiagnostic().WithLocation(7, 24),
89+
this.CSharpDiagnostic().WithLocation(9, 13),
90+
this.CSharpDiagnostic().WithLocation(9, 31),
91+
this.CSharpDiagnostic().WithLocation(11, 13),
92+
this.CSharpDiagnostic().WithLocation(11, 20),
93+
this.CSharpDiagnostic().WithLocation(14, 9),
94+
this.CSharpDiagnostic().WithLocation(14, 29),
95+
this.CSharpDiagnostic().WithLocation(15, 9),
96+
this.CSharpDiagnostic().WithLocation(15, 17),
97+
this.CSharpDiagnostic().WithLocation(16, 9),
98+
this.CSharpDiagnostic().WithLocation(16, 19),
99+
};
100+
101+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false);
102+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
103+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
104+
}
105+
23106
/// <inheritdoc/>
24107
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
25108
{

0 commit comments

Comments
 (0)