Skip to content

Commit 2967e94

Browse files
committed
Fixed UsingCodeFixProvider
1 parent 185d58b commit 2967e94

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.UsingsSorter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace StyleCop.Analyzers.OrderingRules
1313
using Microsoft.CodeAnalysis.CSharp;
1414
using Microsoft.CodeAnalysis.CSharp.Syntax;
1515
using StyleCop.Analyzers.Helpers;
16+
using StyleCop.Analyzers.Helpers.ObjectPools;
1617
using StyleCop.Analyzers.Lightup;
1718
using StyleCop.Analyzers.Settings.ObjectModel;
1819

@@ -377,7 +378,7 @@ private UsingDirectiveSyntax QualifyUsingDirective(UsingDirectiveSyntax usingDir
377378
}
378379
else if (namedTypeSymbol.IsTupleType())
379380
{
380-
fullName = namedTypeSymbol.TupleUnderlyingType().ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
381+
fullName = namedTypeSymbol.TupleUnderlyingTypeOrSelf().ToFullyQualifiedValueTupleDisplayString();
381382
}
382383
else
383384
{

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/NamedTypeHelpers.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace StyleCop.Analyzers.Helpers
99
using Microsoft.CodeAnalysis.CSharp;
1010
using Microsoft.CodeAnalysis.CSharp.Syntax;
1111

12+
using StyleCop.Analyzers.Lightup;
13+
1214
internal static class NamedTypeHelpers
1315
{
1416
internal static bool IsNativeMethodsClass(INamedTypeSymbol type)
@@ -179,5 +181,8 @@ internal static bool IsImplementingAnInterfaceMember(ISymbol memberSymbol)
179181
.Select(typeSymbol.FindImplementationForInterfaceMember)
180182
.Any(x => memberSymbol.Equals(x));
181183
}
184+
185+
internal static INamedTypeSymbol TupleUnderlyingTypeOrSelf(this INamedTypeSymbol tupleSymbol)
186+
=> tupleSymbol.TupleUnderlyingType() ?? tupleSymbol;
182187
}
183188
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace StyleCop.Analyzers.Helpers
77
using Microsoft.CodeAnalysis;
88
using Microsoft.CodeAnalysis.CSharp;
99
using Microsoft.CodeAnalysis.CSharp.Syntax;
10+
1011
using StyleCop.Analyzers.Lightup;
1112

1213
/// <summary>
@@ -35,6 +36,40 @@ public static string ToQualifiedString(this ISymbol symbol, NameSyntax name)
3536
return ObjectPools.StringBuilderPool.ReturnAndFree(builder);
3637
}
3738

39+
/// <summary>
40+
/// Generates the fully qualified System.ValueTuple based name for the given tuple type.
41+
/// </summary>
42+
/// <param name="tupleSymbol">The tuple symbol.</param>
43+
/// <returns>The generated fully qualified display string.</returns>
44+
public static string ToFullyQualifiedValueTupleDisplayString(this INamedTypeSymbol tupleSymbol)
45+
{
46+
var tupleElements = tupleSymbol.TupleElements();
47+
if (tupleElements.IsDefault)
48+
{
49+
return tupleSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
50+
}
51+
else
52+
{
53+
var builder = ObjectPools.StringBuilderPool.Allocate();
54+
55+
builder.Append("global::System.ValueTuple<");
56+
57+
for (var i = 0; i < tupleElements.Length; i++)
58+
{
59+
if (i > 0)
60+
{
61+
builder.Append(", ");
62+
}
63+
64+
builder.Append(tupleElements[i].Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
65+
}
66+
67+
builder.Append(">");
68+
69+
return ObjectPools.StringBuilderPool.ReturnAndFree(builder);
70+
}
71+
}
72+
3873
private static bool AppendQualifiedSymbolName(StringBuilder builder, ISymbol symbol, TypeSyntax type)
3974
{
4075
switch (symbol.Kind)

0 commit comments

Comments
 (0)