Skip to content

Commit 0340bbd

Browse files
committed
Work around dotnet/roslyn#7446
1 parent d391039 commit 0340bbd

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers
5+
{
6+
using System;
7+
using System.Collections.Immutable;
8+
using System.Reflection;
9+
using Microsoft.CodeAnalysis;
10+
using Microsoft.CodeAnalysis.Diagnostics;
11+
12+
[DiagnosticAnalyzer(LanguageNames.CSharp)]
13+
internal sealed class Roslyn7446WorkaroundAnalyzer : DiagnosticAnalyzer
14+
{
15+
/// <summary>
16+
/// The ID for diagnostics produced by the <see cref="Roslyn7446WorkaroundAnalyzer"/> analyzer.
17+
/// </summary>
18+
public const string DiagnosticId = "Roslyn7446WorkaroundAnalyzer";
19+
private const string Title = "Roslyn Bug 7446 Workaround";
20+
private const string MessageFormat = "Roslyn Bug 7446 Workaround";
21+
private const string Description = "Roslyn Bug 7446 Workaround";
22+
private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/Roslyn7446WorkaroundAnalyzer.md";
23+
24+
private static readonly DiagnosticDescriptor Descriptor =
25+
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.SpacingRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink, WellKnownDiagnosticTags.NotConfigurable);
26+
27+
private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
28+
29+
private static readonly bool CallGetDeclarationDiagnostics;
30+
31+
static Roslyn7446WorkaroundAnalyzer()
32+
{
33+
// dotnet/roslyn#7446 was fixed for Roslyn 1.2
34+
CallGetDeclarationDiagnostics = typeof(AdditionalText).GetTypeInfo().Assembly.GetName().Version < new Version(1, 2, 0, 0);
35+
}
36+
37+
/// <inheritdoc/>
38+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
39+
ImmutableArray.Create(Descriptor);
40+
41+
/// <inheritdoc/>
42+
public override void Initialize(AnalysisContext context)
43+
{
44+
if (!CallGetDeclarationDiagnostics)
45+
{
46+
return;
47+
}
48+
49+
context.RegisterCompilationStartAction(CompilationStartAction);
50+
}
51+
52+
#pragma warning disable RS1012 // Start action has no registered actions.
53+
private static void HandleCompilationStart(CompilationStartAnalysisContext context)
54+
#pragma warning restore RS1012 // Start action has no registered actions.
55+
{
56+
context.Compilation.GetDeclarationDiagnostics(context.CancellationToken);
57+
}
58+
}
59+
}

StyleCop.Analyzers/StyleCop.Analyzers/StyleCop.Analyzers.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@
268268
<Compile Include="ReadabilityRules\SA1133DoNotCombineAttributes.cs" />
269269
<Compile Include="ReadabilityRules\SA1134AttributesMustNotShareLine.cs" />
270270
<Compile Include="ReadabilityRules\SX1101DoNotPrefixLocalMembersWithThis.cs" />
271+
<Compile Include="Roslyn7446WorkaroundAnalyzer.cs" />
271272
<Compile Include="Settings\ObjectModel\DocumentationSettings.cs" />
272273
<Compile Include="Settings\ObjectModel\EndOfFileHandling.cs" />
273274
<Compile Include="Settings\ObjectModel\FileNamingConvention.cs" />

0 commit comments

Comments
 (0)