Skip to content

Commit a3227c6

Browse files
Have 'use explicit type' add usings as necessary to minimally qualify the type (#79574)
2 parents f137091 + a8cc9e2 commit a3227c6

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/Analyzers/CSharp/CodeFixes/UseImplicitOrExplicitType/UseExplicitTypeCodeFixProvider.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.CodeAnalysis.LanguageService;
1818
using Microsoft.CodeAnalysis.PooledObjects;
1919
using Microsoft.CodeAnalysis.Shared.Extensions;
20+
using Microsoft.CodeAnalysis.Simplification;
2021
using Roslyn.Utilities;
2122

2223
namespace Microsoft.CodeAnalysis.CSharp.TypeStyle;
@@ -224,13 +225,14 @@ private static ExpressionSyntax GenerateTupleDeclaration(
224225
.WithTrailingTrivia(parensDesignation.GetTrailingTrivia());
225226
}
226227

227-
private static SyntaxNode GenerateTypeDeclaration(TypeSyntax typeSyntax, ITypeSymbol newTypeSymbol)
228+
private static TypeSyntax GenerateTypeDeclaration(TypeSyntax typeSyntax, ITypeSymbol newTypeSymbol)
228229
{
229230
// We're going to be passed through the simplifier. Tell it to not just convert this back to var (as
230231
// that would defeat the purpose of this refactoring entirely).
231232
var newTypeSyntax = newTypeSymbol
232-
.GenerateTypeSyntax(allowVar: false)
233-
.WithTriviaFrom(typeSyntax);
233+
.GenerateTypeSyntax(allowVar: false)
234+
.WithAdditionalAnnotations(Simplifier.AddImportsAnnotation)
235+
.WithTriviaFrom(typeSyntax);
234236

235237
return newTypeSyntax;
236238
}

src/Analyzers/CSharp/Tests/UseImplicitOrExplicitType/UseExplicitTypeTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,7 +2653,9 @@ void M()
26532653
}
26542654
""", new TestParameters(options: ExplicitTypeEverywhere()));
26552655

2656-
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/23907")]
2656+
[Fact]
2657+
[WorkItem("https://github.com/dotnet/roslyn/issues/23907")]
2658+
[WorkItem("https://github.com/dotnet/roslyn/issues/24034")]
26572659
public async Task WithNormalFuncSynthesizedLambdaType()
26582660
{
26592661
var before = """
@@ -2666,11 +2668,13 @@ void Method()
26662668
}
26672669
""";
26682670
var after = """
2671+
using System;
2672+
26692673
class Program
26702674
{
26712675
void Method()
26722676
{
2673-
System.Func<int, string> x = (int i) => i.ToString();
2677+
Func<int, string> x = (int i) => i.ToString();
26742678
}
26752679
}
26762680
""";

0 commit comments

Comments
 (0)