Skip to content

Commit 4cdc15f

Browse files
Updated SA1125 to get the identifier name from Identifier.ValueText instead of Identifier.Text. The previous implementation caused false negatives when Nullable was prefixed with @.
1 parent 4755c66 commit 4cdc15f

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1125UnitTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,31 @@ class ClassName<T>
336336
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
337337
}
338338

339+
// This is a regression test for issue 2284.
340+
[Theory]
341+
[InlineData("@Nullable<int>", "int?")]
342+
[InlineData("System.@Nullable<int>", "int?")]
343+
[InlineData("global::System.@Nullable<int>", "int?")]
344+
public async Task TestNullableFieldWithAtSignPrefixInTypeAsync(string longForm, string shortForm)
345+
{
346+
string template = @"
347+
namespace System
348+
{{
349+
class ClassName<T>
350+
where T : struct
351+
{{
352+
{0} nullableField;
353+
}}
354+
}}
355+
";
356+
string testCode = string.Format(template, longForm);
357+
string fixedCode = string.Format(template, shortForm);
358+
359+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(7, 9);
360+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
361+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
362+
}
363+
339364
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
340365
{
341366
yield return new SA1125UseShorthandForNullableTypes();

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1125UseShorthandForNullableTypes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private static void HandleGenericName(SyntaxNodeAnalysisContext context)
5353
{
5454
GenericNameSyntax genericNameSyntax = (GenericNameSyntax)context.Node;
5555

56-
if (genericNameSyntax.Identifier.IsMissing || genericNameSyntax.Identifier.Text != "Nullable")
56+
if (genericNameSyntax.Identifier.IsMissing || genericNameSyntax.Identifier.ValueText != "Nullable")
5757
{
5858
return;
5959
}

0 commit comments

Comments
 (0)