From 9948e3d4c1ed5714139e5c28f7d0979e69660c19 Mon Sep 17 00:00:00 2001 From: Joey Robichaud Date: Tue, 11 Jun 2019 09:46:27 -0700 Subject: [PATCH] Filter out missing import fixes that would add references (#36251) * Filter out missing import fixes that would add references * Remove superfluous IsEmpty check --- .../AbstractAddMissingImportsFeatureService.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Features/Core/Portable/CodeRefactorings/AddMissingImports/AbstractAddMissingImportsFeatureService.cs b/src/Features/Core/Portable/CodeRefactorings/AddMissingImports/AbstractAddMissingImportsFeatureService.cs index 3df3b2ca280..9d265b448d0 100644 --- a/src/Features/Core/Portable/CodeRefactorings/AddMissingImports/AbstractAddMissingImportsFeatureService.cs +++ b/src/Features/Core/Portable/CodeRefactorings/AddMissingImports/AbstractAddMissingImportsFeatureService.cs @@ -48,7 +48,10 @@ public async Task AddMissingImportsAsync(Document document, TextSpan te } // Find fixes for the diagnostic where there is only a single fix. - var usableFixes = await GetUnambiguousFixesAsync(document, diagnostics, cancellationToken).ConfigureAwait(false); + var unambiguousFixes = await GetUnambiguousFixesAsync(document, diagnostics, cancellationToken).ConfigureAwait(false); + + // We do not want to add project or framework references without the user's input, so filter those out. + var usableFixes = unambiguousFixes.WhereAsArray(fixData => DoesNotAddReference(fixData, document.Project.Id)); if (usableFixes.IsEmpty) { return document.Project; @@ -59,6 +62,13 @@ public async Task AddMissingImportsAsync(Document document, TextSpan te return newDocument.Project; } + private bool DoesNotAddReference(AddImportFixData fixData, ProjectId currentProjectId) + { + return (fixData.ProjectReferenceToAdd is null || fixData.ProjectReferenceToAdd == currentProjectId) + && (fixData.PortableExecutableReferenceProjectId is null || fixData.PortableExecutableReferenceProjectId == currentProjectId) + && string.IsNullOrEmpty(fixData.AssemblyReferenceAssemblyName); + } + private async Task> GetDiagnosticsAsync(Document document, TextSpan textSpan, CancellationToken cancellationToken) { var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); -- GitLab