Skip to content

Commit 551bba1

Browse files
committed
Update SA1101 for static local functions
1 parent b72b3f7 commit 551bba1

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/ReadabilityRules/SA1101CSharp7UnitTests.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ public class TestClass
121121
{
122122
private int foobar = 1;
123123
124+
public int Foo()
125+
{
126+
int Quux<T>() => {|#0:foobar|};
127+
return Quux<int>();
128+
}
129+
}
130+
";
131+
var fixedCode = @"
132+
public class TestClass
133+
{
134+
private int foobar = 1;
135+
124136
public int Foo()
125137
{
126138
int Quux<T>() => this.foobar;
@@ -129,7 +141,46 @@ public int Foo()
129141
}
130142
";
131143

132-
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
144+
DiagnosticResult[] expected =
145+
{
146+
Diagnostic().WithLocation(0),
147+
};
148+
149+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
150+
}
151+
152+
[Fact]
153+
[WorkItem(3005, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3005")]
154+
public async Task TestLocalFunctionRequiresThisAsync()
155+
{
156+
var testCode = @"public class TestClass
157+
{
158+
private int field;
159+
160+
public int Method()
161+
{
162+
int Local() => {|#0:field|};
163+
return Local();
164+
}
165+
}";
166+
167+
var fixedCode = @"public class TestClass
168+
{
169+
private int field;
170+
171+
public int Method()
172+
{
173+
int Local() => this.field;
174+
return Local();
175+
}
176+
}";
177+
178+
DiagnosticResult[] expected =
179+
{
180+
Diagnostic().WithLocation(0),
181+
};
182+
183+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
133184
}
134185
}
135186
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/ReadabilityRules/SA1101CSharp8UnitTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ namespace StyleCop.Analyzers.Test.CSharp8.ReadabilityRules
1414

1515
public partial class SA1101CSharp8UnitTests : SA1101CSharp7UnitTests
1616
{
17+
[Fact]
18+
[WorkItem(3005, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3005")]
19+
public async Task TestStaticLocalFunctionCallAsync()
20+
{
21+
var testCode = @"public class TestClass
22+
{
23+
public void Method()
24+
{
25+
static void LocalFunction()
26+
{
27+
}
28+
29+
LocalFunction();
30+
}
31+
}";
32+
33+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
34+
}
35+
1736
[Fact]
1837
[WorkItem(3472, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3472")]
1938
public async Task TestPropertyPatternAsync()

0 commit comments

Comments
 (0)