-
Notifications
You must be signed in to change notification settings - Fork 464
/
ProvideObsoleteAttributeMessage.cs
83 lines (73 loc) · 3.69 KB
/
ProvideObsoleteAttributeMessage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Analyzer.Utilities;
using System.Linq;
using Analyzer.Utilities.Extensions;
namespace Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines
{
using static MicrosoftCodeQualityAnalyzersResources;
/// <summary>
/// CA1041: <inheritdoc cref="ProvideObsoleteAttributeMessageTitle"/>
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class ProvideObsoleteAttributeMessageAnalyzer : DiagnosticAnalyzer
{
internal const string RuleId = "CA1041";
internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create(
RuleId,
CreateLocalizableResourceString(nameof(ProvideObsoleteAttributeMessageTitle)),
CreateLocalizableResourceString(nameof(ProvideObsoleteAttributeMessageMessage)),
DiagnosticCategory.Design,
RuleLevel.IdeSuggestion,
description: CreateLocalizableResourceString(nameof(ProvideObsoleteAttributeMessageDescription)),
isPortedFxCopRule: true,
isDataflowRule: false);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterCompilationStartAction(compilationContext =>
{
INamedTypeSymbol? obsoleteAttributeType = compilationContext.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemObsoleteAttribute);
if (obsoleteAttributeType == null)
{
return;
}
compilationContext.RegisterSymbolAction(sc => AnalyzeSymbol(sc, obsoleteAttributeType),
SymbolKind.NamedType,
SymbolKind.Method,
SymbolKind.Field,
SymbolKind.Property,
SymbolKind.Event);
});
}
private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol obsoleteAttributeType)
{
// FxCop compat: only analyze externally visible symbols by default
if (!context.Options.MatchesConfiguredVisibility(Rule, context.Symbol, context.Compilation))
{
return;
}
ImmutableArray<AttributeData> attributes = context.Symbol.GetAttributes();
foreach (AttributeData attribute in attributes)
{
if (attribute.AttributeClass.Equals(obsoleteAttributeType))
{
// ObsoleteAttribute has a constructor that takes no params and two
// other constructors that take a message as the first param.
// If there are no arguments specified or if the message argument is empty
// then report a diagnostic.
if (attribute.ConstructorArguments.IsEmpty ||
string.IsNullOrEmpty(attribute.ConstructorArguments.First().Value as string))
{
SyntaxNode node = attribute.ApplicationSyntaxReference.GetSyntax(context.CancellationToken);
context.ReportDiagnostic(node.CreateDiagnostic(Rule, context.Symbol.Name));
}
}
}
}
}
}