Skip to content

Commit d9fe562

Browse files
Updated SA1612 to get parameter name from Identifier.ValueText instead of Identifier.Text, because Identifier.Text contains the needed @ for parameters named as keywords. This causes false positives, since there should be no @ in the documentation xml.
1 parent 4755c66 commit d9fe562

2 files changed

Lines changed: 29 additions & 16 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1612UnitTests.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public static IEnumerable<object[]> Declarations
2222
{
2323
get
2424
{
25-
yield return new[] { " public ClassName Method(string foo, string bar) { return null; }" };
26-
yield return new[] { " public delegate ClassName Method(string foo, string bar);" };
27-
yield return new[] { " public ClassName this[string foo, string bar] { get { return null; } set { } }" };
25+
yield return new[] { " public ClassName Method(string foo, string bar, string @new) { return null; }" };
26+
yield return new[] { " public delegate ClassName Method(string foo, string bar, string @new);" };
27+
yield return new[] { " public ClassName this[string foo, string bar, string @new] { get { return null; } set { } }" };
2828
}
2929
}
3030

@@ -75,6 +75,7 @@ public class ClassName
7575
/// </summary>
7676
///<param name=""foo"">Test</param>
7777
///<param name=""bar"">Test</param>
78+
///<param name=""new"">Test</param>
7879
$$
7980
}";
8081
await this.VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
@@ -95,13 +96,15 @@ public class ClassName
9596
/// </summary>
9697
///<param name=""boo"">Test</param>
9798
///<param name=""far"">Test</param>
99+
///<param name=""foe"">Test</param>
98100
$$
99101
}";
100102

101103
var expected = new[]
102104
{
103105
this.CSharpDiagnostic().WithLocation(10, 21).WithArguments("boo"),
104106
this.CSharpDiagnostic().WithLocation(11, 21).WithArguments("far"),
107+
this.CSharpDiagnostic().WithLocation(12, 21).WithArguments("foe"),
105108
};
106109

107110
await this.VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), expected, CancellationToken.None).ConfigureAwait(false);
@@ -124,6 +127,7 @@ public class ClassName
124127
///<param/>
125128
///<param name="""">Test</param>
126129
///<param name="" "">Test</param>
130+
///<param name="" "">Test</param>
127131
$$
128132
}";
129133
await this.VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
@@ -143,6 +147,7 @@ public class ClassName
143147
/// Foo
144148
/// </summary>
145149
/// <param name=""bar"">Param 2</param>
150+
/// <param name=""new"">Param 3</param>
146151
/// <param name=""foo"">Param 1</param>
147152
$$
148153
}";
@@ -153,7 +158,8 @@ public class ClassName
153158
var expected = new[]
154159
{
155160
diagnostic.WithLocation(10, 22).WithArguments("bar", 2),
156-
diagnostic.WithLocation(11, 22).WithArguments("foo", 1),
161+
diagnostic.WithLocation(11, 22).WithArguments("new", 3),
162+
diagnostic.WithLocation(12, 22).WithArguments("foo", 1),
157163
};
158164

159165
await this.VerifyCSharpDiagnosticAsync(testCode.Replace("$$", p), expected, CancellationToken.None).ConfigureAwait(false);
@@ -174,14 +180,15 @@ public class ClassName
174180
/// </summary>
175181
/// <param name=""foo"">Param 1</param>
176182
/// <param name=""bar"">Param 2</param>
177-
/// <param name=""bar"">Param 3</param>
183+
/// <param name=""new"">Param 3</param>
184+
/// <param name=""bar"">Param 4</param>
178185
$$
179186
}";
180187

181188
var diagnostic = this.CSharpDiagnostic()
182189
.WithMessageFormat("The parameter documentation for '{0}' should be at position {1}.");
183190

184-
var expected = diagnostic.WithLocation(12, 22).WithArguments("bar", 2);
191+
var expected = diagnostic.WithLocation(13, 22).WithArguments("bar", 2);
185192

186193
await this.VerifyCSharpDiagnosticAsync(testCode.Replace("$$", p), expected, CancellationToken.None).ConfigureAwait(false);
187194
}
@@ -227,7 +234,7 @@ public async Task VerifyIncludedMemberWithValidParamsIsNotReportedAsync()
227234
public class ClassName
228235
{
229236
/// <include file='WithParamDocumentation.xml' path='/ClassName/Method/*' />
230-
public ClassName Method(string foo, string bar) { return null; }
237+
public ClassName Method(string foo, string bar, string @new) { return null; }
231238
}";
232239
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
233240
}
@@ -242,13 +249,14 @@ public async Task VerifyIncludedMemberWithInvalidParamsIsReportedAsync()
242249
public class ClassName
243250
{
244251
/// <include file='WithInvalidParamDocumentation.xml' path='/ClassName/Method/*' />
245-
public ClassName Method(string foo, string bar) { return null; }
252+
public ClassName Method(string foo, string bar, string @new) { return null; }
246253
}";
247254

248255
var expected = new[]
249256
{
250257
this.CSharpDiagnostic().WithLocation(8, 22).WithArguments("boo"),
251258
this.CSharpDiagnostic().WithLocation(8, 22).WithArguments("far"),
259+
this.CSharpDiagnostic().WithLocation(8, 22).WithArguments("foe"),
252260
};
253261

254262
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
@@ -264,7 +272,7 @@ public async Task VerifyIncludedMemberWithInvalidParamsThatShouldBeHandledBySA16
264272
public class ClassName
265273
{
266274
/// <include file='WithSA1613ParamDocumentation.xml' path='/ClassName/Method/*' />
267-
public ClassName Method(string foo, string bar) { return null; }
275+
public ClassName Method(string foo, string bar, string @new) { return null; }
268276
}";
269277
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
270278
}
@@ -279,16 +287,17 @@ public async Task VerifyIncludedMemberWithAllDocumentationWrongOrderIsReportedAs
279287
public class ClassName
280288
{
281289
/// <include file='WithParamDocumentation.xml' path='/ClassName/Method/*' />
282-
public ClassName Method(string bar, string foo) { return null; }
290+
public ClassName Method(string bar, string @new, string foo) { return null; }
283291
}";
284292

285293
var diagnostic = this.CSharpDiagnostic()
286294
.WithMessageFormat("The parameter documentation for '{0}' should be at position {1}.");
287295

288296
var expected = new[]
289297
{
290-
diagnostic.WithLocation(8, 22).WithArguments("foo", 2),
298+
diagnostic.WithLocation(8, 22).WithArguments("foo", 3),
291299
diagnostic.WithLocation(8, 22).WithArguments("bar", 1),
300+
diagnostic.WithLocation(8, 22).WithArguments("new", 2),
292301
};
293302

294303
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
@@ -304,7 +313,7 @@ public async Task VerifyIncludedMemberWithTooManyDocumentationIsReportedAsync()
304313
public class ClassName
305314
{
306315
/// <include file='WithTooManyParamDocumentation.xml' path='/ClassName/Method/*' />
307-
public ClassName Method(string foo, string bar) { return null; }
316+
public ClassName Method(string foo, string bar, string @new) { return null; }
308317
}";
309318

310319
var diagnostic = this.CSharpDiagnostic()
@@ -325,7 +334,7 @@ public async Task VerifyIncludedInheritedDocumentationIsNotReportedAsync()
325334
public class ClassName
326335
{
327336
/// <include file='WithInheritedDocumentation.xml' path='/ClassName/Method/*' />
328-
public ClassName Method(string foo, string bar) { return null; }
337+
public ClassName Method(string foo, string bar, string @new) { return null; }
329338
}";
330339
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
331340
}
@@ -354,6 +363,7 @@ protected override Project ApplyCompilationOptions(Project project)
354363
</summary>
355364
<param name=""foo"">Param 1</param>
356365
<param name=""bar"">Param 2</param>
366+
<param name=""new"">Param 3</param>
357367
</Method>
358368
</ClassName>
359369
";
@@ -367,6 +377,7 @@ protected override Project ApplyCompilationOptions(Project project)
367377
</summary>
368378
<param name=""boo"">Param 1</param>
369379
<param name=""far"">Param 2</param>
380+
<param name=""foe"">Param 3</param>
370381
</Method>
371382
</ClassName>
372383
";
@@ -382,6 +393,7 @@ protected override Project ApplyCompilationOptions(Project project)
382393
<param/>
383394
<param name="""">Test</param>
384395
<param name="" "">Test</param>
396+
<param name="" "">Test</param>
385397
</Method>
386398
</ClassName>
387399
";
@@ -395,7 +407,8 @@ protected override Project ApplyCompilationOptions(Project project)
395407
</summary>
396408
<param name=""foo"">Param 1</param>
397409
<param name=""bar"">Param 2</param>
398-
<param name=""bar"">Param 3</param>
410+
<param name=""new"">Param 3</param>
411+
<param name=""bar"">Param 4</param>
399412
</Method>
400413
</ClassName>
401414
";

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1612ElementParameterDocumentationMustMatchElementParameters.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, IEnu
9292
return;
9393
}
9494

95-
var parentParameter = parentParameters.FirstOrDefault(s => s.Identifier.Text == nameAttributeText);
95+
var parentParameter = parentParameters.FirstOrDefault(s => s.Identifier.ValueText == nameAttributeText);
9696
if (parentParameter == null)
9797
{
9898
context.ReportDiagnostic(Diagnostic.Create(MissingParameterDescriptor, location ?? identifierLocation, nameAttributeText));
@@ -150,7 +150,7 @@ protected override void HandleCompleteDocumentation(SyntaxNodeAnalysisContext co
150150
return;
151151
}
152152

153-
var parentParameter = parentParameters.FirstOrDefault(s => s.Identifier.Text == nameAttributeText);
153+
var parentParameter = parentParameters.FirstOrDefault(s => s.Identifier.ValueText == nameAttributeText);
154154
if (parentParameter == null)
155155
{
156156
context.ReportDiagnostic(Diagnostic.Create(MissingParameterDescriptor, identifierLocation, nameAttributeText));

0 commit comments

Comments
 (0)