Example #1
0
        protected sealed override void Initialize(SonarAnalysisContext context)
        {
            context.RegisterCompilationStartAction(
                compilationStartContext =>
            {
                var allNamedTypeSymbols   = compilationStartContext.Compilation.GlobalNamespace.GetAllNamedTypes();
                var typeInterfaceMappings = allNamedTypeSymbols.Select(type =>
                                                                       new
                {
                    Type       = type.OriginalDefinition,
                    Interfaces = type.OriginalDefinition.AllInterfaces.Select(i => i.OriginalDefinition)
                });

                var interfaceImplementerMappings = new Dictionary <INamedTypeSymbol, HashSet <INamedTypeSymbol> >();
                foreach (var typeInterfaceMapping in typeInterfaceMappings)
                {
                    if (typeInterfaceMapping.Type.IsInterface())
                    {
                        if (!interfaceImplementerMappings.ContainsKey(typeInterfaceMapping.Type))
                        {
                            interfaceImplementerMappings.Add(typeInterfaceMapping.Type, new HashSet <INamedTypeSymbol>());
                        }

                        interfaceImplementerMappings[typeInterfaceMapping.Type].Add(typeInterfaceMapping.Type);
                    }

                    foreach (var @interface in typeInterfaceMapping.Interfaces)
                    {
                        if (!interfaceImplementerMappings.ContainsKey(@interface))
                        {
                            interfaceImplementerMappings.Add(@interface, new HashSet <INamedTypeSymbol>());
                        }

                        interfaceImplementerMappings[@interface].Add(typeInterfaceMapping.Type);
                    }
                }

                compilationStartContext.RegisterSyntaxNodeActionInNonGenerated(
                    c =>
                {
                    var cast           = (CastExpressionSyntax)c.Node;
                    var interfaceType  = c.SemanticModel.GetTypeInfo(cast.Type).Type as INamedTypeSymbol;
                    var expressionType = c.SemanticModel.GetTypeInfo(cast.Expression).Type as INamedTypeSymbol;

                    CheckTypesForInvalidCast(interfaceType, expressionType, interfaceImplementerMappings,
                                             cast.Type.GetLocation(), c);
                },
                    SyntaxKind.CastExpression);
            });

            context.RegisterExplodedGraphBasedAnalysis((e, c) => CheckEmptyNullableCast(e, c));
        }
Example #2
0
 protected override void Initialize(SonarAnalysisContext context)
 {
     context.RegisterExplodedGraphBasedAnalysis(Analyze);
 }
 protected sealed override void Initialize(SonarAnalysisContext context)
 {
     context.RegisterExplodedGraphBasedAnalysis((e, c) => CheckEmptyNullableAccess(e, c));
 }
 protected override void Initialize(SonarAnalysisContext context)
 {
     context.RegisterExplodedGraphBasedAnalysis((e, c) => CheckForNullDereference(e, c));
 }
 protected sealed override void Initialize(SonarAnalysisContext context)
 {
     context.RegisterExplodedGraphBasedAnalysis(CheckForMultipleDispose);
 }
 protected override void Initialize(SonarAnalysisContext context)
 {
     context.RegisterExplodedGraphBasedAnalysis((e, c) => CheckForRedundantConditions(e, c));
 }
Example #7
0
 protected override void Initialize(SonarAnalysisContext context)
 {
     context.RegisterExplodedGraphBasedAnalysis(CheckForEmptyCollectionAccess);
 }
        protected override void Initialize(SonarAnalysisContext context)
        {
            context.RegisterCompilationStartAction(
                compilationStartContext =>
                {
                    var allNamedTypeSymbols = compilationStartContext.Compilation.GlobalNamespace.GetAllNamedTypes();
                    var typeInterfaceMappings = allNamedTypeSymbols.Select(type =>
                        new
                        {
                            Type = type.OriginalDefinition,
                            Interfaces = type.OriginalDefinition.AllInterfaces.Select(i => i.OriginalDefinition)
                        });

                    var interfaceImplementerMappings = new Dictionary<INamedTypeSymbol, HashSet<INamedTypeSymbol>>();
                    foreach (var typeInterfaceMapping in typeInterfaceMappings)
                    {
                        if (typeInterfaceMapping.Type.IsInterface())
                        {
                            if (!interfaceImplementerMappings.ContainsKey(typeInterfaceMapping.Type))
                            {
                                interfaceImplementerMappings.Add(typeInterfaceMapping.Type, new HashSet<INamedTypeSymbol>());
                            }

                            interfaceImplementerMappings[typeInterfaceMapping.Type].Add(typeInterfaceMapping.Type);
                        }

                        foreach (var @interface in typeInterfaceMapping.Interfaces)
                        {
                            if (!interfaceImplementerMappings.ContainsKey(@interface))
                            {
                                interfaceImplementerMappings.Add(@interface, new HashSet<INamedTypeSymbol>());
                            }

                            interfaceImplementerMappings[@interface].Add(typeInterfaceMapping.Type);
                        }
                    }

                    compilationStartContext.RegisterSyntaxNodeActionInNonGenerated(
                        c =>
                        {
                            var cast = (CastExpressionSyntax)c.Node;
                            var interfaceType = c.SemanticModel.GetTypeInfo(cast.Type).Type as INamedTypeSymbol;
                            var expressionType = c.SemanticModel.GetTypeInfo(cast.Expression).Type as INamedTypeSymbol;

                            CheckTypesForInvalidCast(interfaceType, expressionType, interfaceImplementerMappings,
                                cast.Type.GetLocation(), c);
                        },
                        SyntaxKind.CastExpression);

                    compilationStartContext.RegisterSyntaxNodeAction(
                        c =>
                        {
                            var cast = (BinaryExpressionSyntax)c.Node;
                            var interfaceType = c.SemanticModel.GetTypeInfo(cast.Right).Type as INamedTypeSymbol;
                            var expressionType = c.SemanticModel.GetTypeInfo(cast.Left).Type as INamedTypeSymbol;

                            CheckTypesForInvalidCast(interfaceType, expressionType, interfaceImplementerMappings,
                                cast.Right.GetLocation(), c);
                        },
                        SyntaxKind.AsExpression,
                        SyntaxKind.IsExpression);
                });

            context.RegisterExplodedGraphBasedAnalysis((e, c) => CheckEmptyNullableCast(e, c));
        }
 protected override void Initialize(SonarAnalysisContext context)
 {
     context.RegisterExplodedGraphBasedAnalysis((e, c) => CheckForNullDereference(e, c));
 }
 protected override void Initialize(SonarAnalysisContext context)
 {
     context.RegisterExplodedGraphBasedAnalysis((e, c) => CheckEmptyNullableAccess(e, c));
 }
 protected override void Initialize(SonarAnalysisContext context)
 {
     context.RegisterExplodedGraphBasedAnalysis((e, c) => CheckForRedundantConditions(e, c));
 }