Skip to content

Commit 857819a

Browse files
committed
Fix incorrect qualification of types nested within generic types
Fixes #2879
1 parent b0aed87 commit 857819a

2 files changed

Lines changed: 52 additions & 30 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1135UnitTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,5 +319,18 @@ namespace MyNamespace {
319319

320320
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
321321
}
322+
323+
[Fact]
324+
[WorkItem(2879, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2879")]
325+
public async Task TestAliasTypeNestedInGenericAsync()
326+
{
327+
var testCode = @"
328+
namespace TestNamespace
329+
{
330+
using Example = System.Collections.Immutable.ImmutableDictionary<int, int>.Builder;
331+
}
332+
";
333+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
334+
}
322335
}
323336
}

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/SymbolNameHelpers.cs

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,44 @@ public static string ToQualifiedString(this ISymbol symbol)
3939
}
4040

4141
AppendQualifiedSymbolName(builder, namedTypeSymbol);
42+
}
43+
else
44+
{
45+
AppendQualifiedSymbolName(builder, symbol);
46+
}
47+
48+
return ObjectPools.StringBuilderPool.ReturnAndFree(builder);
49+
}
50+
51+
private static bool AppendQualifiedSymbolName(StringBuilder builder, ISymbol symbol)
52+
{
53+
switch (symbol)
54+
{
55+
case IArrayTypeSymbol arraySymbol:
56+
AppendQualifiedSymbolName(builder, arraySymbol.ElementType);
57+
builder
58+
.Append("[")
59+
.Append(',', arraySymbol.Rank - 1)
60+
.Append("]");
61+
return true;
62+
63+
case INamespaceSymbol namespaceSymbol:
64+
if (namespaceSymbol.IsGlobalNamespace)
65+
{
66+
return false;
67+
}
68+
69+
builder.Append(namespaceSymbol.ToDisplayString());
70+
return true;
71+
72+
case INamedTypeSymbol namedTypeSymbol:
73+
if (AppendQualifiedSymbolName(builder, symbol.ContainingSymbol))
74+
{
75+
builder.Append(".");
76+
}
4277

43-
if (namedTypeSymbol.IsGenericType)
78+
builder.Append(symbol.Name);
79+
if (namedTypeSymbol.IsGenericType && !namedTypeSymbol.TypeArguments.IsEmpty)
4480
{
4581
builder.Append(GenericTypeParametersOpen);
4682

@@ -61,39 +97,12 @@ public static string ToQualifiedString(this ISymbol symbol)
6197
builder.Remove(builder.Length - GenericSeparator.Length, GenericSeparator.Length);
6298
builder.Append(GenericTypeParametersClose);
6399
}
64-
}
65-
else
66-
{
67-
AppendQualifiedSymbolName(builder, symbol);
68-
}
69-
70-
return ObjectPools.StringBuilderPool.ReturnAndFree(builder);
71-
}
72100

73-
private static void AppendQualifiedSymbolName(StringBuilder builder, ISymbol symbol)
74-
{
75-
switch (symbol)
76-
{
77-
case IArrayTypeSymbol arraySymbol:
78-
builder
79-
.Append(arraySymbol.ElementType.ContainingNamespace.ToDisplayString())
80-
.Append(".")
81-
.Append(arraySymbol.ElementType.Name)
82-
.Append("[")
83-
.Append(',', arraySymbol.Rank - 1)
84-
.Append("]");
85-
break;
101+
return true;
86102

87103
default:
88-
if (!symbol.ContainingNamespace.IsGlobalNamespace)
89-
{
90-
builder
91-
.Append(symbol.ContainingNamespace.ToDisplayString())
92-
.Append(".");
93-
}
94-
95104
builder.Append(symbol.Name);
96-
break;
105+
return true;
97106
}
98107
}
99108
}

0 commit comments

Comments
 (0)