Skip to content

Commit c0c556a

Browse files
authored
Merge pull request #2870 from sharwell/update-lightup
Update the lightup layer to account for changes in C# 7.1-7.3
2 parents ce6c3cd + 1f4ac95 commit c0c556a

60 files changed

Lines changed: 2126 additions & 182 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1207CodeFixProvider.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace StyleCop.Analyzers.OrderingRules
55
{
66
using System.Collections.Immutable;
77
using System.Composition;
8+
using System.Linq;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using Microsoft.CodeAnalysis;
@@ -59,21 +60,23 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
5960
return document;
6061
}
6162

62-
var newDeclarationNode = originalDeclarationNode.ReplaceTokens(childTokens, ComputeReplacementToken);
63+
bool hasInternalKeyword = childTokens.Any(token => token.IsKind(SyntaxKind.InternalKeyword));
64+
var newDeclarationNode = originalDeclarationNode.ReplaceTokens(childTokens, (originalToken, rewrittenToken) => ComputeReplacementToken(originalToken, rewrittenToken, hasInternalKeyword));
6365

6466
var newSyntaxRoot = syntaxRoot.ReplaceNode(originalDeclarationNode, newDeclarationNode);
6567
return document.WithSyntaxRoot(newSyntaxRoot);
6668
}
6769

68-
private static SyntaxToken ComputeReplacementToken(SyntaxToken originalToken, SyntaxToken rewrittenToken)
70+
private static SyntaxToken ComputeReplacementToken(SyntaxToken originalToken, SyntaxToken rewrittenToken, bool hasInternalKeyword)
6971
{
70-
if (originalToken.IsKind(SyntaxKind.InternalKeyword))
72+
if (originalToken.IsKind(SyntaxKind.InternalKeyword)
73+
|| originalToken.IsKind(SyntaxKind.PrivateKeyword))
7174
{
7275
return SyntaxFactory.Token(SyntaxKind.ProtectedKeyword).WithTriviaFrom(rewrittenToken);
7376
}
7477
else if (originalToken.IsKind(SyntaxKind.ProtectedKeyword))
7578
{
76-
return SyntaxFactory.Token(SyntaxKind.InternalKeyword).WithTriviaFrom(rewrittenToken);
79+
return SyntaxFactory.Token(hasInternalKeyword ? SyntaxKind.InternalKeyword : SyntaxKind.PrivateKeyword).WithTriviaFrom(rewrittenToken);
7780
}
7881
else
7982
{

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

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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;
910
using StyleCop.Analyzers.Test.LayoutRules;
1011
using Xunit;
@@ -38,7 +39,7 @@ void LocalFunction()
3839
}
3940
";
4041

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

4445
/// <summary>
@@ -86,5 +87,89 @@ void LocalFunction()
8687
var expectedDiagnostic = Diagnostic().WithLocation(10, 13);
8788
await VerifyCSharpFixAsync(testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
8889
}
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+
}
89174
}
90175
}

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

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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;
910
using StyleCop.Analyzers.Test.LayoutRules;
1011
using Xunit;
@@ -38,7 +39,7 @@ void LocalFunction()
3839
}
3940
";
4041

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

4445
/// <summary>
@@ -86,5 +87,89 @@ void LocalFunction()
8687
var expectedDiagnostic = Diagnostic().WithLocation(13, 13);
8788
await VerifyCSharpFixAsync(testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
8889
}
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+
}
89174
}
90175
}

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

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ 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;
910
using StyleCop.Analyzers.Test.LayoutRules;
10-
using TestHelper;
1111
using Xunit;
1212
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1313
StyleCop.Analyzers.LayoutRules.SA1509OpeningBracesMustNotBePrecededByBlankLine,
@@ -74,5 +74,89 @@ void Bar()
7474
DiagnosticResult expected = Diagnostic().WithLocation(9, 9);
7575
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
7676
}
77+
78+
[Fact]
79+
public async Task TestStackAllocArrayCreationExpressionAsync()
80+
{
81+
var testCode = @"namespace TestNamespace
82+
{
83+
public class TestClass
84+
{
85+
public unsafe void TestMethod()
86+
{
87+
int* v1 = stackalloc int[]
88+
89+
{
90+
1,
91+
2,
92+
3
93+
};
94+
}
95+
}
96+
}
97+
";
98+
99+
var fixedTestCode = @"namespace TestNamespace
100+
{
101+
public class TestClass
102+
{
103+
public unsafe void TestMethod()
104+
{
105+
int* v1 = stackalloc int[]
106+
{
107+
1,
108+
2,
109+
3
110+
};
111+
}
112+
}
113+
}
114+
";
115+
116+
var expectedDiagnostic = Diagnostic().WithLocation(9, 13);
117+
await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
118+
}
119+
120+
[Fact]
121+
public async Task TestImplicitStackAllocArrayCreationExpressionAsync()
122+
{
123+
var testCode = @"namespace TestNamespace
124+
{
125+
public class TestClass
126+
{
127+
public unsafe void TestMethod()
128+
{
129+
int* v1 = stackalloc[]
130+
131+
{
132+
1,
133+
2,
134+
3
135+
};
136+
}
137+
}
138+
}
139+
";
140+
141+
var fixedTestCode = @"namespace TestNamespace
142+
{
143+
public class TestClass
144+
{
145+
public unsafe void TestMethod()
146+
{
147+
int* v1 = stackalloc[]
148+
{
149+
1,
150+
2,
151+
3
152+
};
153+
}
154+
}
155+
}
156+
";
157+
158+
var expectedDiagnostic = Diagnostic().WithLocation(9, 13);
159+
await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
160+
}
77161
}
78162
}

0 commit comments

Comments
 (0)