Ejemplo n.º 1
0
        private static void AnalyzeParenthesizedLambdaExpression(SyntaxNodeAnalysisContext context)
        {
            var lambda = (ParenthesizedLambdaExpressionSyntax)context.Node;

            if (lambda.ContainsDiagnostics)
            {
                return;
            }

            ParameterInfo parameterInfo = SyntaxInfo.ParameterInfo(lambda);

            if (!parameterInfo.Success)
            {
                return;
            }

            var methodSymbol = (IMethodSymbol)context.SemanticModel.GetSymbol(lambda, context.CancellationToken);

            if (methodSymbol == null)
            {
                return;
            }

            if (SymbolUtility.IsEventHandlerMethod(methodSymbol))
            {
                return;
            }

            Analyze(context, parameterInfo);
        }
Ejemplo n.º 2
0
        private static void AnalyzeAnonymousMethodExpression(SyntaxNodeAnalysisContext context)
        {
            var anonymousMethod = (AnonymousMethodExpressionSyntax)context.Node;

            if (anonymousMethod.ContainsDiagnostics)
            {
                return;
            }

            ParameterInfo parameterInfo = SyntaxInfo.ParameterInfo(anonymousMethod);

            if (!parameterInfo.Success)
            {
                return;
            }

            var methodSymbol = (IMethodSymbol)context.SemanticModel.GetSymbol(anonymousMethod, context.CancellationToken);

            if (methodSymbol == null)
            {
                return;
            }

            if (SymbolUtility.IsEventHandlerMethod(methodSymbol))
            {
                return;
            }

            Analyze(context, parameterInfo);
        }
Ejemplo n.º 3
0
        private static void AnalyzeLocalFunctionStatement(SyntaxNodeAnalysisContext context)
        {
            var localFunctionStatement = (LocalFunctionStatementSyntax)context.Node;

            if (localFunctionStatement.ContainsDiagnostics)
            {
                return;
            }

            ParameterInfo parameterInfo = SyntaxInfo.ParameterInfo(localFunctionStatement);

            if (!parameterInfo.Success)
            {
                return;
            }

            IMethodSymbol methodSymbol = context.SemanticModel.GetDeclaredSymbol(localFunctionStatement, context.CancellationToken);

            if (methodSymbol == null)
            {
                return;
            }

            if (SymbolUtility.IsEventHandlerMethod(methodSymbol))
            {
                return;
            }

            Analyze(context, parameterInfo);
        }
Ejemplo n.º 4
0
        private static void AnalyzeIndexerDeclaration(SyntaxNodeAnalysisContext context)
        {
            var indexerDeclaration = (IndexerDeclarationSyntax)context.Node;

            if (indexerDeclaration.ContainsDiagnostics)
            {
                return;
            }

            if (!indexerDeclaration.IsParentKind(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.RecordDeclaration, SyntaxKind.RecordStructDeclaration))
            {
                return;
            }

            if (indexerDeclaration.Modifiers.ContainsAny(SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword, SyntaxKind.OverrideKeyword))
            {
                return;
            }

            ParameterInfo parameterInfo = SyntaxInfo.ParameterInfo(indexerDeclaration);

            if (!parameterInfo.Success)
            {
                return;
            }

            if (ContainsOnlyThrowNewExpression(parameterInfo.Body))
            {
                return;
            }

            IPropertySymbol propertySymbol = context.SemanticModel.GetDeclaredSymbol(indexerDeclaration, context.CancellationToken);

            if (propertySymbol?.ExplicitInterfaceImplementations.IsDefaultOrEmpty != true)
            {
                return;
            }

            if (propertySymbol.ImplementsInterfaceMember(allInterfaces: true))
            {
                return;
            }

            Analyze(context, parameterInfo, isIndexer: true);
        }
Ejemplo n.º 5
0
        public static void AnalyzeConstructorDeclaration(
            SyntaxNodeAnalysisContext context,
            INamedTypeSymbol serializationInfoSymbol,
            INamedTypeSymbol streamingContextSymbol)
        {
            var constructorDeclaration = (ConstructorDeclarationSyntax)context.Node;

            if (constructorDeclaration.ContainsDiagnostics)
            {
                return;
            }

            ParameterInfo parameterInfo = SyntaxInfo.ParameterInfo(constructorDeclaration);

            if (!parameterInfo.Success)
            {
                return;
            }

            if (ContainsOnlyThrowNewExpression(parameterInfo.Body))
            {
                return;
            }

            // Skip a constructor that is required by ISerializable interface
            if (serializationInfoSymbol != null &&
                parameterInfo.Parameters.Count == 2)
            {
                IMethodSymbol symbol = context.SemanticModel.GetDeclaredSymbol(constructorDeclaration, context.CancellationToken);

                if (symbol != null)
                {
                    ImmutableArray <IParameterSymbol> parameters = symbol.Parameters;

                    if (parameters.Length == 2 &&
                        parameters[0].Type.Equals(serializationInfoSymbol) &&
                        parameters[1].Type.Equals(streamingContextSymbol))
                    {
                        return;
                    }
                }
            }

            Analyze(context, parameterInfo);
        }
Ejemplo n.º 6
0
        private static void AnalyzeConversionOperatorDeclaration(SyntaxNodeAnalysisContext context)
        {
            var conversionOperatorDeclaration = (ConversionOperatorDeclarationSyntax)context.Node;

            if (conversionOperatorDeclaration.ContainsDiagnostics)
            {
                return;
            }

            ParameterInfo parameterInfo = SyntaxInfo.ParameterInfo(conversionOperatorDeclaration);

            if (!parameterInfo.Success)
            {
                return;
            }

            if (ContainsOnlyThrowNewExpression(parameterInfo.Body))
            {
                return;
            }

            Analyze(context, parameterInfo);
        }
Ejemplo n.º 7
0
        private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
        {
            var methodDeclaration = (MethodDeclarationSyntax)context.Node;

            if (methodDeclaration.ContainsDiagnostics)
            {
                return;
            }

            if (!methodDeclaration.IsParentKind(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.RecordDeclaration, SyntaxKind.RecordStructDeclaration))
            {
                return;
            }

            if (methodDeclaration.Modifiers.ContainsAny(
                    SyntaxKind.AbstractKeyword,
                    SyntaxKind.VirtualKeyword,
                    SyntaxKind.OverrideKeyword,
                    SyntaxKind.PartialKeyword))
            {
                return;
            }

            ParameterInfo parameterInfo = SyntaxInfo.ParameterInfo(methodDeclaration);

            if (!parameterInfo.Success)
            {
                return;
            }

            if (ContainsOnlyThrowNewExpression(parameterInfo.Body))
            {
                return;
            }

            IMethodSymbol methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclaration, context.CancellationToken);

            if (methodSymbol == null)
            {
                return;
            }

            if (SymbolUtility.IsEventHandlerMethod(methodSymbol))
            {
                return;
            }

            if (!methodSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty)
            {
                return;
            }

            if (methodSymbol.ImplementsInterfaceMember(allInterfaces: true))
            {
                return;
            }

            UnusedParameterWalker walker = null;

            try
            {
                walker = UnusedParameterWalker.GetInstance();

                walker.SetValues(context.SemanticModel, context.CancellationToken);

                FindUnusedNodes(parameterInfo, walker);

                if (walker.Nodes.Count > 0 &&
                    !MethodReferencedAsMethodGroupWalker.IsReferencedAsMethodGroup(methodDeclaration, methodSymbol, context.SemanticModel, context.CancellationToken))
                {
                    foreach (KeyValuePair <string, NodeSymbolInfo> kvp in walker.Nodes)
                    {
                        ReportDiagnostic(context, kvp.Value.Node);
                    }
                }
            }
            finally
            {
                if (walker != null)
                {
                    UnusedParameterWalker.Free(walker);
                }
            }
        }
Ejemplo n.º 8
0
        public static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context, INamedTypeSymbol eventArgsSymbol)
        {
            var methodDeclaration = (MethodDeclarationSyntax)context.Node;

            if (methodDeclaration.ContainsDiagnostics)
            {
                return;
            }

            if (methodDeclaration.IsParentKind(SyntaxKind.InterfaceDeclaration))
            {
                return;
            }

            if (methodDeclaration.Modifiers.ContainsAny(SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword, SyntaxKind.OverrideKeyword))
            {
                return;
            }

            ParameterInfo parameterInfo = SyntaxInfo.ParameterInfo(methodDeclaration);

            if (!parameterInfo.Success)
            {
                return;
            }

            if (ContainsOnlyThrowNewExpression(parameterInfo.Body))
            {
                return;
            }

            IMethodSymbol methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclaration, context.CancellationToken);

            if (methodSymbol == null)
            {
                return;
            }

            if (SymbolUtility.IsEventHandlerMethod(methodSymbol, eventArgsSymbol))
            {
                return;
            }

            if (!methodSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty)
            {
                return;
            }

            if (methodSymbol.ImplementsInterfaceMember(allInterfaces: true))
            {
                return;
            }

            Dictionary <string, NodeSymbolInfo> unusedNodes = FindUnusedNodes(context, parameterInfo);

            if (unusedNodes.Count == 0)
            {
                return;
            }

            if (IsReferencedAsMethodGroup(context, methodDeclaration))
            {
                return;
            }

            foreach (KeyValuePair <string, NodeSymbolInfo> kvp in unusedNodes)
            {
                ReportDiagnostic(context, kvp.Value.Node);
            }

            unusedNodes.Clear();
        }