Skip to content

Commit 62bd77a

Browse files
committed
Update rules to account for stackalloc initializers
1 parent b17aa80 commit 62bd77a

23 files changed

Lines changed: 1706 additions & 75 deletions

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1505CSharp7UnitTests.cs

Lines changed: 127 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ namespace StyleCop.Analyzers.Test.CSharp7.LayoutRules
55
{
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.CSharp;
89
using Microsoft.CodeAnalysis.Testing;
10+
using StyleCop.Analyzers.LayoutRules;
911
using StyleCop.Analyzers.Test.LayoutRules;
12+
using StyleCop.Analyzers.Test.Verifiers;
1013
using Xunit;
11-
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12-
StyleCop.Analyzers.LayoutRules.SA1505OpeningBracesMustNotBeFollowedByBlankLine,
13-
StyleCop.Analyzers.LayoutRules.SA1505CodeFixProvider>;
14+
using static StyleCop.Analyzers.Test.Verifiers.CustomDiagnosticVerifier<StyleCop.Analyzers.LayoutRules.SA1505OpeningBracesMustNotBeFollowedByBlankLine>;
1415

1516
public class SA1505CSharp7UnitTests : SA1505UnitTests
1617
{
@@ -38,7 +39,7 @@ void LocalFunction()
3839
}
3940
";
4041

41-
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
42+
await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false);
4243
}
4344

4445
/// <summary>
@@ -84,7 +85,128 @@ void LocalFunction()
8485
";
8586

8687
var expectedDiagnostic = Diagnostic().WithLocation(10, 13);
87-
await VerifyCSharpFixAsync(testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
88+
await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
89+
}
90+
91+
[Fact]
92+
public async Task TestStackAllocArrayCreationExpressionAsync()
93+
{
94+
var testCode = @"namespace TestNamespace
95+
{
96+
public class TestClass
97+
{
98+
public unsafe void TestMethod()
99+
{
100+
int* v1 = stackalloc int[]
101+
{
102+
103+
1,
104+
2,
105+
3
106+
};
107+
}
108+
}
109+
}
110+
";
111+
112+
var fixedTestCode = @"namespace TestNamespace
113+
{
114+
public class TestClass
115+
{
116+
public unsafe void TestMethod()
117+
{
118+
int* v1 = stackalloc int[]
119+
{
120+
1,
121+
2,
122+
3
123+
};
124+
}
125+
}
126+
}
127+
";
128+
129+
var expectedDiagnostic = Diagnostic().WithLocation(8, 13);
130+
await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
131+
}
132+
133+
[Fact]
134+
public async Task TestImplicitStackAllocArrayCreationExpressionAsync()
135+
{
136+
var testCode = @"namespace TestNamespace
137+
{
138+
public class TestClass
139+
{
140+
public unsafe void TestMethod()
141+
{
142+
int* v1 = stackalloc[]
143+
{
144+
145+
1,
146+
2,
147+
3
148+
};
149+
}
150+
}
151+
}
152+
";
153+
154+
var fixedTestCode = @"namespace TestNamespace
155+
{
156+
public class TestClass
157+
{
158+
public unsafe void TestMethod()
159+
{
160+
int* v1 = stackalloc[]
161+
{
162+
1,
163+
2,
164+
3
165+
};
166+
}
167+
}
168+
}
169+
";
170+
171+
var expectedDiagnostic = Diagnostic().WithLocation(8, 13);
172+
await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
173+
}
174+
175+
private static Task VerifyCSharpFixAsync(LanguageVersion languageVersion, string source, DiagnosticResult expected, string fixedSource, CancellationToken cancellationToken)
176+
{
177+
return VerifyCSharpFixAsync(languageVersion, source, new[] { expected }, fixedSource, cancellationToken);
178+
}
179+
180+
private static Task VerifyCSharpFixAsync(LanguageVersion languageVersion, string source, DiagnosticResult[] expected, string fixedSource, CancellationToken cancellationToken)
181+
{
182+
var test = new CSharpTest(languageVersion)
183+
{
184+
TestCode = source,
185+
FixedCode = fixedSource,
186+
};
187+
188+
if (source == fixedSource)
189+
{
190+
test.FixedState.InheritanceMode = StateInheritanceMode.AutoInheritAll;
191+
test.FixedState.MarkupHandling = MarkupMode.Allow;
192+
test.BatchFixedState.InheritanceMode = StateInheritanceMode.AutoInheritAll;
193+
test.BatchFixedState.MarkupHandling = MarkupMode.Allow;
194+
}
195+
196+
test.ExpectedDiagnostics.AddRange(expected);
197+
return test.RunAsync(cancellationToken);
198+
}
199+
200+
private class CSharpTest : StyleCopCodeFixVerifier<SA1505OpeningBracesMustNotBeFollowedByBlankLine, SA1505CodeFixProvider>.CSharpTest
201+
{
202+
public CSharpTest(LanguageVersion languageVersion)
203+
{
204+
this.SolutionTransforms.Add((solution, projectId) =>
205+
{
206+
var parseOptions = (CSharpParseOptions)solution.GetProject(projectId).ParseOptions;
207+
return solution.WithProjectParseOptions(projectId, parseOptions.WithLanguageVersion(languageVersion));
208+
});
209+
}
88210
}
89211
}
90212
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1508CSharp7UnitTests.cs

Lines changed: 127 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ namespace StyleCop.Analyzers.Test.CSharp7.LayoutRules
55
{
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.CSharp;
89
using Microsoft.CodeAnalysis.Testing;
10+
using StyleCop.Analyzers.LayoutRules;
911
using StyleCop.Analyzers.Test.LayoutRules;
12+
using StyleCop.Analyzers.Test.Verifiers;
1013
using Xunit;
11-
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12-
StyleCop.Analyzers.LayoutRules.SA1508ClosingBracesMustNotBePrecededByBlankLine,
13-
StyleCop.Analyzers.LayoutRules.SA1508CodeFixProvider>;
14+
using static StyleCop.Analyzers.Test.Verifiers.CustomDiagnosticVerifier<StyleCop.Analyzers.LayoutRules.SA1508ClosingBracesMustNotBePrecededByBlankLine>;
1415

1516
public class SA1508CSharp7UnitTests : SA1508UnitTests
1617
{
@@ -38,7 +39,7 @@ void LocalFunction()
3839
}
3940
";
4041

41-
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
42+
await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false);
4243
}
4344

4445
/// <summary>
@@ -84,7 +85,128 @@ void LocalFunction()
8485
";
8586

8687
var expectedDiagnostic = Diagnostic().WithLocation(13, 13);
87-
await VerifyCSharpFixAsync(testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
88+
await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
89+
}
90+
91+
[Fact]
92+
public async Task TestStackAllocArrayCreationExpressionAsync()
93+
{
94+
var testCode = @"namespace TestNamespace
95+
{
96+
public class TestClass
97+
{
98+
public unsafe void TestMethod()
99+
{
100+
int* v1 = stackalloc int[]
101+
{
102+
1,
103+
2,
104+
3
105+
106+
};
107+
}
108+
}
109+
}
110+
";
111+
112+
var fixedTestCode = @"namespace TestNamespace
113+
{
114+
public class TestClass
115+
{
116+
public unsafe void TestMethod()
117+
{
118+
int* v1 = stackalloc int[]
119+
{
120+
1,
121+
2,
122+
3
123+
};
124+
}
125+
}
126+
}
127+
";
128+
129+
var expectedDiagnostic = Diagnostic().WithLocation(13, 13);
130+
await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
131+
}
132+
133+
[Fact]
134+
public async Task TestImplicitStackAllocArrayCreationExpressionAsync()
135+
{
136+
var testCode = @"namespace TestNamespace
137+
{
138+
public class TestClass
139+
{
140+
public unsafe void TestMethod()
141+
{
142+
int* v1 = stackalloc[]
143+
{
144+
1,
145+
2,
146+
3
147+
148+
};
149+
}
150+
}
151+
}
152+
";
153+
154+
var fixedTestCode = @"namespace TestNamespace
155+
{
156+
public class TestClass
157+
{
158+
public unsafe void TestMethod()
159+
{
160+
int* v1 = stackalloc[]
161+
{
162+
1,
163+
2,
164+
3
165+
};
166+
}
167+
}
168+
}
169+
";
170+
171+
var expectedDiagnostic = Diagnostic().WithLocation(13, 13);
172+
await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
173+
}
174+
175+
private static Task VerifyCSharpFixAsync(LanguageVersion languageVersion, string source, DiagnosticResult expected, string fixedSource, CancellationToken cancellationToken)
176+
{
177+
return VerifyCSharpFixAsync(languageVersion, source, new[] { expected }, fixedSource, cancellationToken);
178+
}
179+
180+
private static Task VerifyCSharpFixAsync(LanguageVersion languageVersion, string source, DiagnosticResult[] expected, string fixedSource, CancellationToken cancellationToken)
181+
{
182+
var test = new CSharpTest(languageVersion)
183+
{
184+
TestCode = source,
185+
FixedCode = fixedSource,
186+
};
187+
188+
if (source == fixedSource)
189+
{
190+
test.FixedState.InheritanceMode = StateInheritanceMode.AutoInheritAll;
191+
test.FixedState.MarkupHandling = MarkupMode.Allow;
192+
test.BatchFixedState.InheritanceMode = StateInheritanceMode.AutoInheritAll;
193+
test.BatchFixedState.MarkupHandling = MarkupMode.Allow;
194+
}
195+
196+
test.ExpectedDiagnostics.AddRange(expected);
197+
return test.RunAsync(cancellationToken);
198+
}
199+
200+
private class CSharpTest : StyleCopCodeFixVerifier<SA1508ClosingBracesMustNotBePrecededByBlankLine, SA1508CodeFixProvider>.CSharpTest
201+
{
202+
public CSharpTest(LanguageVersion languageVersion)
203+
{
204+
this.SolutionTransforms.Add((solution, projectId) =>
205+
{
206+
var parseOptions = (CSharpParseOptions)solution.GetProject(projectId).ParseOptions;
207+
return solution.WithProjectParseOptions(projectId, parseOptions.WithLanguageVersion(languageVersion));
208+
});
209+
}
88210
}
89211
}
90212
}

0 commit comments

Comments
 (0)