protected override void Initialize(SonarAnalysisContext context) =>
        context.RegisterSyntaxNodeActionInNonGenerated(c =>
        {
            if (c.ContainingSymbol.Kind != SymbolKind.NamedType)
            {
                return;
            }

            var typeDeclarationSyntax = (TypeDeclarationSyntax)c.Node;
            var declarationIdentifier = typeDeclarationSyntax.Identifier;
            var checker = new DisposableChecker(typeDeclarationSyntax.BaseList,
                                                declarationIdentifier,
                                                c.SemanticModel.GetDeclaredSymbol(typeDeclarationSyntax),
                                                c.Node.IsKind(SyntaxKind.ClassDeclaration) ? "class" : "record",
                                                c.SemanticModel);

            var locations = checker.GetIssueLocations();
            if (locations.Any())
            {
                c.ReportDiagnosticWhenActive(Diagnostic.Create(Rule, declarationIdentifier.GetLocation(),
                                                               locations.ToAdditionalLocations(),
                                                               locations.ToProperties()));
            }
        },
                                                       SyntaxKind.ClassDeclaration,
                                                       SyntaxKindEx.RecordDeclaration);
Пример #2
0
        protected override void Initialize(SonarAnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(c =>
            {
                var classDeclaration = (ClassDeclarationSyntax)c.Node;

                var checker   = new DisposableChecker(classDeclaration, c.SemanticModel);
                var locations = checker.GetIssueLocations();
                if (locations.Any())
                {
                    c.ReportDiagnosticWhenActive(Diagnostic.Create(rule, classDeclaration.Identifier.GetLocation(),
                                                                   locations.ToAdditionalLocations(),
                                                                   locations.ToProperties()));
                }
            },
                                                           SyntaxKind.ClassDeclaration);
        }