CA1012DiagnosticAnalyzer.cs 3.0 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3 4 5 6 7

using System;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.Diagnostics;
8 9
using Microsoft.AnalyzerPowerPack.Utilities;
using Microsoft.CodeAnalysis;
P
Pilchie 已提交
10

11
namespace Microsoft.AnalyzerPowerPack.Design
P
Pilchie 已提交
12 13 14 15
{
    /// <summary>
    /// CA1012: Abstract classes should not have public constructors
    /// </summary>
16
    [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
17
    public sealed class CA1012DiagnosticAnalyzer : AbstractNamedTypeAnalyzer
P
Pilchie 已提交
18 19
    {
        internal const string RuleId = "CA1012";
20 21
        private static readonly LocalizableString s_localizableTitle = new LocalizableResourceString(nameof(AnalyzerPowerPackRulesResources.AbstractTypesShouldNotHavePublicConstructors), AnalyzerPowerPackRulesResources.ResourceManager, typeof(AnalyzerPowerPackRulesResources));
        private static readonly LocalizableString s_localizableMessage = new LocalizableResourceString(nameof(AnalyzerPowerPackRulesResources.TypeIsAbstractButHasPublicConstructors), AnalyzerPowerPackRulesResources.ResourceManager, typeof(AnalyzerPowerPackRulesResources));
22

P
Pilchie 已提交
23
        internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(RuleId,
24 25
                                                                         s_localizableTitle,
                                                                         s_localizableMessage,
26
                                                                         AnalyzerPowerPackDiagnosticCategory.Design,
27
                                                                         DiagnosticSeverity.Warning,
28
                                                                         isEnabledByDefault: false,
29
                                                                         helpLinkUri: "http://msdn.microsoft.com/library/ms182126.aspx",
30
                                                                         customTags: DiagnosticCustomTags.Microsoft);
P
Pilchie 已提交
31 32 33 34 35 36 37 38 39

        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
        {
            get
            {
                return ImmutableArray.Create(Rule);
            }
        }

40
        protected override void AnalyzeSymbol(INamedTypeSymbol symbol, Compilation compilation, Action<Diagnostic> addDiagnostic, AnalyzerOptions options, CancellationToken cancellationToken)
P
Pilchie 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        {
            if (symbol.IsAbstract)
            {
                // TODO: Should we also check symbol.GetResultantVisibility() == SymbolVisibility.Public?

                var hasAnyPublicConstructors =
                    symbol.InstanceConstructors.Any(
                        (constructor) => constructor.DeclaredAccessibility == Accessibility.Public);

                if (hasAnyPublicConstructors)
                {
                    addDiagnostic(symbol.CreateDiagnostic(Rule, symbol.Name));
                }
            }
        }
    }
}