diff --git a/src/Analyzers/AnalyzerReferenceInformationProvider.cs b/src/Analyzers/AnalyzerReferenceInformationProvider.cs index eaae51abed..9ab076289d 100644 --- a/src/Analyzers/AnalyzerReferenceInformationProvider.cs +++ b/src/Analyzers/AnalyzerReferenceInformationProvider.cs @@ -23,10 +23,8 @@ internal class AnalyzerReferenceInformationProvider : IAnalyzerInformationProvid private AnalyzersAndFixers GetAnalyzersAndFixers(Project project) { - var context = new AnalyzerLoadContext(); - var analyzerAssemblies = project.AnalyzerReferences - .Select(reference => TryLoadAssemblyFrom(reference.FullPath, context)) + .Select(reference => TryLoadAssemblyFrom(reference.FullPath, new AnalyzerLoadContext())) .OfType() .ToImmutableArray(); diff --git a/src/Analyzers/SolutionCodeFixApplier.cs b/src/Analyzers/SolutionCodeFixApplier.cs index 0f565c0324..c9ae6bfa6b 100644 --- a/src/Analyzers/SolutionCodeFixApplier.cs +++ b/src/Analyzers/SolutionCodeFixApplier.cs @@ -109,9 +109,10 @@ public override Task> GetAllDiagnosticsAsync(Project pro return GetProjectDiagnosticsAsync(project, cancellationToken); } - public override Task> GetDocumentDiagnosticsAsync(Document document, CancellationToken cancellationToken) + public override async Task> GetDocumentDiagnosticsAsync(Document document, CancellationToken cancellationToken) { - throw new NotImplementedException(); + var projectDiagnostics = await GetProjectDiagnosticsAsync(document.Project, cancellationToken); + return projectDiagnostics.Where(diagnostic => diagnostic.Location.SourceTree?.FilePath == document.FilePath).ToImmutableArray(); } public override Task> GetProjectDiagnosticsAsync(Project project, CancellationToken cancellationToken) diff --git a/tests/Analyzers/ThirdPartyAnalyzerFormatterTests.cs b/tests/Analyzers/ThirdPartyAnalyzerFormatterTests.cs index 7893f46d03..f430e9d6af 100644 --- a/tests/Analyzers/ThirdPartyAnalyzerFormatterTests.cs +++ b/tests/Analyzers/ThirdPartyAnalyzerFormatterTests.cs @@ -118,5 +118,98 @@ void M() await AssertCodeChangedAsync(testCode, expectedCode, editorConfig, fixCategory: FixCategory.Analyzers, analyzerReferences: analyzerReferences); } + + [Fact] + public async Task TestIDisposableAnalyzer_AddsUsing() + { + var analyzerReferences = GetAnalyzerReferences("IDisposable"); + + var testCode = @" +using System.IO; + +class C +{ + void M() + { + var stream = File.OpenRead(string.Empty); + var b = stream.ReadByte(); + stream.Dispose(); + } +} +"; + + var expectedCode = @" +using System.IO; + +class C +{ + void M() + { + using (var stream = File.OpenRead(string.Empty)) + { + var b = stream.ReadByte(); + } + } +} +"; + + var editorConfig = new Dictionary() + { + // Turn off all diagnostics analyzers + ["dotnet_analyzer_diagnostic.severity"] = "none", + + // Prefer using. IDISP017 + ["dotnet_diagnostic.IDISP017.severity"] = "error", + }; + + await AssertCodeChangedAsync(testCode, expectedCode, editorConfig, fixCategory: FixCategory.Analyzers, analyzerReferences: analyzerReferences); + } + + [Fact] + public async Task TestLoadingAllAnalyzers_LoadsDependenciesFromAllSearchPaths() + { + // Loads all analyzer references. + var analyzerReferences = _analyzerReferencesProject.AnalyzerReferences; + + var testCode = @" +using System.IO; + +class C +{ + void M() + { + var stream = File.OpenRead(string.Empty); + var b = stream.ReadByte(); + stream.Dispose(); + } +} +"; + + var expectedCode = @" +using System.IO; + +class C +{ + void M() + { + using (var stream = File.OpenRead(string.Empty)) + { + var b = stream.ReadByte(); + } + } +} +"; + + var editorConfig = new Dictionary() + { + // Turn off all diagnostics analyzers + ["dotnet_analyzer_diagnostic.severity"] = "none", + + // Prefer using. IDISP017 + ["dotnet_diagnostic.IDISP017.severity"] = "error", + }; + + await AssertCodeChangedAsync(testCode, expectedCode, editorConfig, fixCategory: FixCategory.Analyzers, analyzerReferences: analyzerReferences); + } } } diff --git a/tests/projects/for_analyzer_formatter/analyzer_project/NuGet.config b/tests/projects/for_analyzer_formatter/analyzer_project/NuGet.config new file mode 100644 index 0000000000..b7fb18432b --- /dev/null +++ b/tests/projects/for_analyzer_formatter/analyzer_project/NuGet.config @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/tests/projects/for_analyzer_formatter/analyzer_project/analyzer_project.csproj b/tests/projects/for_analyzer_formatter/analyzer_project/analyzer_project.csproj index 5f0d40f60c..ad9d39ed6d 100644 --- a/tests/projects/for_analyzer_formatter/analyzer_project/analyzer_project.csproj +++ b/tests/projects/for_analyzer_formatter/analyzer_project/analyzer_project.csproj @@ -9,6 +9,8 @@ all runtime; build; native; contentfiles; analyzers + +