Skip to content

Commit 127cf0d

Browse files
committed
Update SA1312 to handle variable designators (pattern syntax)
1 parent af056c3 commit 127cf0d

2 files changed

Lines changed: 358 additions & 6 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/NamingRules/SA1312CSharp7UnitTests.cs

Lines changed: 344 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,353 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp7.NamingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.NamingRules;
9+
using TestHelper;
10+
using Xunit;
711

812
public class SA1312CSharp7UnitTests : SA1312UnitTests
913
{
14+
[Fact]
15+
public async Task TestThatDiagnosticIsReported_SingleVariableDesignatorAsync()
16+
{
17+
var testCode = @"public class TypeName
18+
{
19+
public void MethodName()
20+
{
21+
int.TryParse(""0"", out var Bar);
22+
int.TryParse(""0"", out var car);
23+
int.TryParse(""0"", out var Par);
24+
}
25+
}";
26+
27+
DiagnosticResult[] expected =
28+
{
29+
this.CSharpDiagnostic().WithArguments("Bar").WithLocation(5, 35),
30+
this.CSharpDiagnostic().WithArguments("Par").WithLocation(7, 35),
31+
};
32+
33+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
34+
35+
var fixedCode = @"public class TypeName
36+
{
37+
public void MethodName()
38+
{
39+
int.TryParse(""0"", out var bar);
40+
int.TryParse(""0"", out var car);
41+
int.TryParse(""0"", out var par);
42+
}
43+
}";
44+
45+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
46+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
47+
}
48+
49+
[Fact]
50+
public async Task TestThatDiagnosticIsReported_MultipleVariableDesignatorsAsync()
51+
{
52+
var testCode = @"public class TypeName
53+
{
54+
public void MethodName()
55+
{
56+
var (Bar, car, Par) = (1, 2, 3);
57+
}
58+
}";
59+
60+
DiagnosticResult[] expected =
61+
{
62+
this.CSharpDiagnostic().WithArguments("Bar").WithLocation(5, 14),
63+
this.CSharpDiagnostic().WithArguments("Par").WithLocation(5, 24),
64+
};
65+
66+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
67+
68+
var fixedCode = @"public class TypeName
69+
{
70+
public void MethodName()
71+
{
72+
var (bar, car, par) = (1, 2, 3);
73+
}
74+
}";
75+
76+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
77+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
78+
}
79+
80+
[Fact]
81+
public async Task TestVariableDesignatorStartingWithAnUnderscoreAsync()
82+
{
83+
// Makes sure SA1312 is reported for variables starting with an underscore
84+
var testCode = @"public class TypeName
85+
{
86+
public void MethodName()
87+
{
88+
int.TryParse(""baz"", out var _bar);
89+
}
90+
}";
91+
92+
var fixedTestCode = @"public class TypeName
93+
{
94+
public void MethodName()
95+
{
96+
int.TryParse(""baz"", out var bar);
97+
}
98+
}";
99+
100+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("_bar").WithLocation(5, 37);
101+
102+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
103+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
104+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
105+
}
106+
107+
[Fact]
108+
public async Task TestVariableDesignatorInWhenClauseAsync()
109+
{
110+
var testCode = @"
111+
using System;
112+
public class TypeName
113+
{
114+
public void MethodName()
115+
{
116+
try
117+
{
118+
}
119+
catch (Exception ex) when (ex is ArgumentException ArgEx)
120+
{
121+
}
122+
}
123+
}";
124+
var fixedCode = @"
125+
using System;
126+
public class TypeName
127+
{
128+
public void MethodName()
129+
{
130+
try
131+
{
132+
}
133+
catch (Exception ex) when (ex is ArgumentException argEx)
134+
{
135+
}
136+
}
137+
}";
138+
139+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("ArgEx").WithLocation(10, 60);
140+
141+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
142+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
143+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
144+
}
145+
146+
[Fact]
147+
public async Task TestPatternInForEachStatementAsync()
148+
{
149+
var testCode = @"public class TypeName
150+
{
151+
public void MethodName()
152+
{
153+
foreach (var (X, y) in new (int, int)[0])
154+
{
155+
}
156+
}
157+
}";
158+
var fixedCode = @"public class TypeName
159+
{
160+
public void MethodName()
161+
{
162+
foreach (var (x, y) in new (int, int)[0])
163+
{
164+
}
165+
}
166+
}";
167+
168+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("X").WithLocation(5, 23);
169+
170+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
171+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
172+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
173+
}
174+
175+
[Fact]
176+
public async Task TestPatternInSwitchCaseAsync()
177+
{
178+
var testCode = @"public class TypeName
179+
{
180+
public void MethodName()
181+
{
182+
switch (new object())
183+
{
184+
case int X:
185+
default:
186+
break;
187+
}
188+
}
189+
}";
190+
var fixedCode = @"public class TypeName
191+
{
192+
public void MethodName()
193+
{
194+
switch (new object())
195+
{
196+
case int x:
197+
default:
198+
break;
199+
}
200+
}
201+
}";
202+
203+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("X").WithLocation(7, 18);
204+
205+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
206+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
207+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
208+
}
209+
210+
[Fact]
211+
public async Task TestPatternPlacedInsideNativeMethodsClassAsync()
212+
{
213+
var testCode = @"public class FooNativeMethods
214+
{
215+
public void MethodName()
216+
{
217+
int.TryParse(""baz"", out var Bar);
218+
}
219+
}";
220+
221+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
222+
}
223+
224+
[Fact]
225+
public async Task TestPatternVariableRenameConflictsWithVariableAsync()
226+
{
227+
var testCode = @"public class TypeName
228+
{
229+
public void MethodName()
230+
{
231+
string variable = ""Text"";
232+
int.TryParse(variable.ToString(), out var Variable);
233+
}
234+
}";
235+
236+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("Variable").WithLocation(6, 51);
237+
238+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
239+
240+
var fixedCode = @"public class TypeName
241+
{
242+
public void MethodName()
243+
{
244+
string variable = ""Text"";
245+
int.TryParse(variable.ToString(), out var variable1);
246+
}
247+
}";
248+
249+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
250+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
251+
}
252+
253+
[Fact]
254+
public async Task TestVariableRenameConflictsWithPatternVariableAsync()
255+
{
256+
var testCode = @"public class TypeName
257+
{
258+
public void MethodName()
259+
{
260+
int.TryParse(""Text"", out var variable);
261+
string Variable = variable.ToString();
262+
}
263+
}";
264+
265+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("Variable").WithLocation(6, 16);
266+
267+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
268+
269+
var fixedCode = @"public class TypeName
270+
{
271+
public void MethodName()
272+
{
273+
int.TryParse(""Text"", out var variable);
274+
string variable1 = variable.ToString();
275+
}
276+
}";
277+
278+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
279+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
280+
}
281+
282+
[Fact]
283+
public async Task TestPatternVariableRenameConflictsWithKeywordAsync()
284+
{
285+
var testCode = @"public class TypeName
286+
{
287+
public void MethodName()
288+
{
289+
int.TryParse(""text"", out var Int);
290+
}
291+
}";
292+
293+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("Int").WithLocation(5, 38);
294+
295+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
296+
297+
var fixedCode = @"public class TypeName
298+
{
299+
public void MethodName()
300+
{
301+
int.TryParse(""text"", out var @int);
302+
}
303+
}";
304+
305+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
306+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
307+
}
308+
309+
[Fact]
310+
public async Task TestPatternVariableRenameConflictsWithParameterAsync()
311+
{
312+
var testCode = @"public class TypeName
313+
{
314+
public void MethodName(int parameter)
315+
{
316+
int.TryParse(parameter.ToString(), out var Parameter);
317+
}
318+
}";
319+
320+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("Parameter").WithLocation(5, 52);
321+
322+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
323+
324+
var fixedCode = @"public class TypeName
325+
{
326+
public void MethodName(int parameter)
327+
{
328+
int.TryParse(parameter.ToString(), out var parameter1);
329+
}
330+
}";
331+
332+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
333+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
334+
}
335+
336+
[Fact]
337+
public async Task TestDiscardsDoNotTriggerCodeFixAsync()
338+
{
339+
var testCode = @"public class TypeName
340+
{
341+
public void MethodName(int parameter)
342+
{
343+
int.TryParse(parameter.ToString(), out var _);
344+
int.TryParse(parameter.ToString(), out var _);
345+
int.TryParse(parameter.ToString(), out var __); // This one isn't a discard
346+
}
347+
}";
348+
349+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("__").WithLocation(7, 52);
350+
351+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
352+
await this.VerifyCSharpFixAsync(testCode, testCode).ConfigureAwait(false);
353+
}
10354
}
11355
}

0 commit comments

Comments
 (0)