From b3e51833cc8c2e7c7bfe6d9347f0f01b9806c290 Mon Sep 17 00:00:00 2001 From: Joey Robichaud Date: Mon, 8 Feb 2021 11:25:26 -0800 Subject: [PATCH] Implement GetDocumentDiagnosticsAsync in CodeFix DiagnosticProvider --- src/Analyzers/SolutionCodeFixApplier.cs | 5 +- .../ThirdPartyAnalyzerFormatterTests.cs | 46 +++++++++++++++++++ .../analyzer_project/NuGet.config | 8 ++++ .../analyzer_project/analyzer_project.csproj | 1 + 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 tests/projects/for_analyzer_formatter/analyzer_project/NuGet.config 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..3d463bd730 100644 --- a/tests/Analyzers/ThirdPartyAnalyzerFormatterTests.cs +++ b/tests/Analyzers/ThirdPartyAnalyzerFormatterTests.cs @@ -118,5 +118,51 @@ void M() await AssertCodeChangedAsync(testCode, expectedCode, editorConfig, fixCategory: FixCategory.Analyzers, analyzerReferences: analyzerReferences); } + + [Fact] + public async Task TestIDisposableAnalyzer_Loads() + { + 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); + } } } 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..b106a32018 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,7 @@ all runtime; build; native; contentfiles; analyzers +