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

Do not apply analyzer bulk configuration to compiler diagnostics #42132

Merged
merged 1 commit into from
Mar 4, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down Expand Up @@ -458,5 +460,50 @@ void M()
analyzerDiagnostics.Verify(expected);
Assert.False(analyzerDiagnostics.Single().IsSuppressed);
}

[Fact]
[WorkItem(42116, "https://github.com/dotnet/roslyn/issues/42116")]
public async Task TestAnalyzerConfigurationDoesNotAffectCompilerDiagnostics()
{
var source = @"
class C
{
void M()
{
// warning CS0168: The variable 'x' is declared but never used.
int x;
}
}
";
// Verify CS0168 reported from 'Compilation.GetDiagnostics'
var compilation = CreateCompilation(source);
var compilerDiagnostics = compilation.GetDiagnostics();
verifyDiagnostics(compilerDiagnostics);

// Verify CS0168 reported from 'CSharpCompilerDiagnosticAnalyzer', i.e. the diagnostic analyzer used in the IDE layer to report live compiler diagnostics.
var analyzers = ImmutableArray.Create<DiagnosticAnalyzer>(new CSharpCompilerDiagnosticAnalyzer());
var compilationWithAnalyzers = compilation.WithAnalyzers(analyzers, new AnalyzerOptions(ImmutableArray<AdditionalText>.Empty));
var analyzerDiagnostics = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync();
verifyDiagnostics(analyzerDiagnostics);

// Verify CS0168 reported by CSharpCompilerDiagnosticAnalyzer is not affected by "dotnet_analyzer_diagnostic = none"
var analyzerConfigOptions = new CompilerAnalyzerConfigOptions(ImmutableDictionary<string, string>.Empty.Add("dotnet_analyzer_diagnostic.severity", "none"));
var analyzerConfigOptionsProvider = new CompilerAnalyzerConfigOptionsProvider(
ImmutableDictionary<object, AnalyzerConfigOptions>.Empty.Add(compilation.SyntaxTrees.Single(), analyzerConfigOptions));
var analyzerOptions = new AnalyzerOptions(ImmutableArray<AdditionalText>.Empty, analyzerConfigOptionsProvider);
compilationWithAnalyzers = compilation.WithAnalyzers(analyzers, analyzerOptions);
analyzerDiagnostics = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync();
verifyDiagnostics(analyzerDiagnostics);

static void verifyDiagnostics(ImmutableArray<Diagnostic> diagnostics)
{
var expected = Diagnostic(ErrorCode.WRN_UnreferencedVar, "x").WithArguments("x").WithLocation(7, 13);
diagnostics.Verify(expected);

var diagnostic = diagnostics.Single();
Assert.Equal(DiagnosticSeverity.Warning, diagnostic.Severity);
Assert.False(diagnostic.IsSuppressed);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1679,9 +1679,9 @@ private static Diagnostic GetFilteredDiagnostic(Diagnostic diagnostic, Compilati
{
diagnostic = compilation.Options.FilterDiagnostic(diagnostic);

// Apply bulk configuration from analyzer options, if applicable.
// Apply bulk configuration from analyzer options for analyzer diagnostics, if applicable.
var tree = diagnostic?.Location.SourceTree;
if (tree == null || analyzerOptions == null)
if (tree == null || analyzerOptions == null || diagnostic.CustomTags.Contains(WellKnownDiagnosticTags.Compiler))
{
return diagnostic;
}
Expand Down