Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for diagnostic suppressors #2182

Merged
merged 7 commits into from Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.json
Expand Up @@ -49,7 +49,8 @@
"NetCore31Project",
"Net50Project",
"ProjectWithAnalyzersAndEditorConfig",
"ProjectWithParentEditorConfig"
"ProjectWithParentEditorConfig",
"TwoProjectsWithAnalyzerSuppressor"
],
"CakeTestAssets": [
"CakeProject"
Expand Down
Expand Up @@ -291,6 +291,7 @@ private async Task<ImmutableArray<Diagnostic>> AnalyzeDocument(Project project,

diagnostics = semanticDiagnosticsWithAnalyzers
.Concat(syntaxDiagnosticsWithAnalyzers)
.Where(d => !d.IsSuppressed)
.Concat(documentSemanticModel.GetDiagnostics())
.ToImmutableArray();
}
Expand Down
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.3.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Lib\Lib.csproj">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<OutputItemType>Analyzer</OutputItemType>
</ProjectReference>
</ItemGroup>


</Project>
@@ -0,0 +1,12 @@
using System;

namespace App
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
@@ -0,0 +1,23 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Immutable;

namespace Lib
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class MySuppressor : DiagnosticSuppressor
{
public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions { get; } = new[]
{
new SuppressionDescriptor("SUPP3662", "SA1200", "Dummy suppression")
}.ToImmutableArray();

public override void ReportSuppressions(SuppressionAnalysisContext context)
{
foreach (Diagnostic diagnostic in context.ReportedDiagnostics)
{
context.ReportSuppression(Suppression.Create(SupportedSuppressions[0], diagnostic));
}
}
}
}
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.3.1" />
</ItemGroup>

</Project>
@@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lib", "Lib\Lib.csproj", "{7120F661-4FCB-4D55-921F-6FBFC3746443}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "App", "App\App.csproj", "{8CC04E3C-AF7D-40F1-BEB4-BACF30A895C2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7120F661-4FCB-4D55-921F-6FBFC3746443}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7120F661-4FCB-4D55-921F-6FBFC3746443}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7120F661-4FCB-4D55-921F-6FBFC3746443}.Debug|x64.ActiveCfg = Debug|Any CPU
{7120F661-4FCB-4D55-921F-6FBFC3746443}.Debug|x64.Build.0 = Debug|Any CPU
{7120F661-4FCB-4D55-921F-6FBFC3746443}.Debug|x86.ActiveCfg = Debug|Any CPU
{7120F661-4FCB-4D55-921F-6FBFC3746443}.Debug|x86.Build.0 = Debug|Any CPU
{7120F661-4FCB-4D55-921F-6FBFC3746443}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7120F661-4FCB-4D55-921F-6FBFC3746443}.Release|Any CPU.Build.0 = Release|Any CPU
{7120F661-4FCB-4D55-921F-6FBFC3746443}.Release|x64.ActiveCfg = Release|Any CPU
{7120F661-4FCB-4D55-921F-6FBFC3746443}.Release|x64.Build.0 = Release|Any CPU
{7120F661-4FCB-4D55-921F-6FBFC3746443}.Release|x86.ActiveCfg = Release|Any CPU
{7120F661-4FCB-4D55-921F-6FBFC3746443}.Release|x86.Build.0 = Release|Any CPU
{8CC04E3C-AF7D-40F1-BEB4-BACF30A895C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8CC04E3C-AF7D-40F1-BEB4-BACF30A895C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8CC04E3C-AF7D-40F1-BEB4-BACF30A895C2}.Debug|x64.ActiveCfg = Debug|Any CPU
{8CC04E3C-AF7D-40F1-BEB4-BACF30A895C2}.Debug|x64.Build.0 = Debug|Any CPU
{8CC04E3C-AF7D-40F1-BEB4-BACF30A895C2}.Debug|x86.ActiveCfg = Debug|Any CPU
{8CC04E3C-AF7D-40F1-BEB4-BACF30A895C2}.Debug|x86.Build.0 = Debug|Any CPU
{8CC04E3C-AF7D-40F1-BEB4-BACF30A895C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8CC04E3C-AF7D-40F1-BEB4-BACF30A895C2}.Release|Any CPU.Build.0 = Release|Any CPU
{8CC04E3C-AF7D-40F1-BEB4-BACF30A895C2}.Release|x64.ActiveCfg = Release|Any CPU
{8CC04E3C-AF7D-40F1-BEB4-BACF30A895C2}.Release|x64.Build.0 = Release|Any CPU
{8CC04E3C-AF7D-40F1-BEB4-BACF30A895C2}.Release|x86.ActiveCfg = Release|Any CPU
{8CC04E3C-AF7D-40F1-BEB4-BACF30A895C2}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
26 changes: 26 additions & 0 deletions tests/OmniSharp.MSBuild.Tests/ProjectWithAnalyzersTests.cs
Expand Up @@ -302,6 +302,32 @@ public async Task WhenNewAnalyzerReferenceIsAdded_ThenAutomaticallyUseItWithoutR
}
}

[Fact]
public async Task WhenProjectIsLoadedThenItRespectsDiagnosticSuppressors()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("TwoProjectsWithAnalyzerSuppressor"))
using (var host = CreateMSBuildTestHost(testProject.Directory, configurationData: TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true)))
{
var project = host.Workspace.CurrentSolution.Projects.First(p => p.Name == "App");

// by default Stylecop reported diagnostics should be:
// - The file header is missing or not located at the top of the file. [App] SA1633
// - Elements should be documented [App] SA1600
// - Element 'Program' should declare an access modifier [App] SA1400
// - Element 'Main' should declare an access modifier [App] SA1400
// However, SA1200 should be suppressed

var diagnostics = await host.RequestCodeCheckAsync(Path.Combine(testProject.Directory, "App", "Program.cs"));

Assert.NotEmpty(diagnostics.QuickFixes);

Assert.Contains(diagnostics.QuickFixes.OfType<DiagnosticLocation>(), x => x.Id == "SA1633" && x.LogLevel == "Warning");
Assert.Contains(diagnostics.QuickFixes.OfType<DiagnosticLocation>(), x => x.Id == "SA1600" && x.LogLevel == "Warning");
Assert.Contains(diagnostics.QuickFixes.OfType<DiagnosticLocation>(), x => x.Id == "SA1400" && x.LogLevel == "Warning");
Assert.DoesNotContain(diagnostics.QuickFixes.OfType<DiagnosticLocation>(), x => x.Id == "SA1200");
}
}

private string ModifyXmlFileInPlace(string file, Action<XDocument> docUpdateAction)
{
var xmlFile = XDocument.Load(file);
Expand Down