示例#1
0
        private static void AnalyzeVariableDeclaration(SyntaxNodeAnalysisContext context)
        {
            var variableDeclaration = (VariableDeclarationSyntax)context.Node;

            if (CSharpTypeAnalysis.IsImplicitThatCanBeExplicit(variableDeclaration, context.SemanticModel, TypeAppearance.Obvious, context.CancellationToken))
            {
                ReportDiagnostic(context, variableDeclaration.Type);
            }
        }
        private static void AnalyzeForEachStatement(SyntaxNodeAnalysisContext context)
        {
            var forEachStatement = (ForEachStatementSyntax)context.Node;

            if (CSharpTypeAnalysis.IsImplicitThatCanBeExplicit(forEachStatement, context.SemanticModel))
            {
                DiagnosticHelpers.ReportDiagnostic(context,
                                                   DiagnosticDescriptors.UseExplicitTypeInsteadOfVarInForEach,
                                                   forEachStatement.Type);
            }
        }
示例#3
0
        private static void AnalyzeDeclarationExpression(SyntaxNodeAnalysisContext context)
        {
            var declarationExpression = (DeclarationExpressionSyntax)context.Node;

            if (CSharpTypeAnalysis.IsImplicitThatCanBeExplicit(declarationExpression, context.SemanticModel, context.CancellationToken))
            {
                DiagnosticHelpers.ReportDiagnostic(context,
                                                   DiagnosticDescriptors.UseExplicitTypeInsteadOfVarWhenTypeIsNotObvious,
                                                   declarationExpression.Type);
            }
        }
示例#4
0
        private static void AnalyzeVariableDeclaration(SyntaxNodeAnalysisContext context)
        {
            var variableDeclaration = (VariableDeclarationSyntax)context.Node;

            if (CSharpTypeAnalysis.IsImplicitThatCanBeExplicit(variableDeclaration, context.SemanticModel, TypeAppearance.NotObvious, context.CancellationToken))
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.UseExplicitTypeInsteadOfVarWhenTypeIsNotObvious,
                    variableDeclaration.Type);
            }
        }
示例#5
0
        private static void AnalyzeDeclarationExpression(SyntaxNodeAnalysisContext context)
        {
            var declarationExpression = (DeclarationExpressionSyntax)context.Node;

            if (declarationExpression.IsParentKind(SyntaxKind.ForEachVariableStatement))
            {
                return;
            }

            if (CSharpTypeAnalysis.IsImplicitThatCanBeExplicit(declarationExpression, context.SemanticModel, context.CancellationToken))
            {
                ReportDiagnostic(context, declarationExpression.Type);
            }
        }
        private static void AnalyzeForEachVariableStatement(SyntaxNodeAnalysisContext context)
        {
            var forEachStatement = (ForEachVariableStatementSyntax)context.Node;

            switch (forEachStatement.Variable)
            {
            case DeclarationExpressionSyntax declarationExpression:
            {
                if (CSharpTypeAnalysis.IsImplicitThatCanBeExplicit(forEachStatement, context.SemanticModel))
                {
                    ReportDiagnostic(context, declarationExpression.Type);
                }

                break;
            }

            case TupleExpressionSyntax tupleExpression:
            {
                foreach (ArgumentSyntax argument in tupleExpression.Arguments)
                {
                    if (!(argument.Expression is DeclarationExpressionSyntax declarationExpression))
                    {
                        continue;
                    }

                    if (CSharpTypeAnalysis.IsImplicitThatCanBeExplicit(declarationExpression, context.SemanticModel, context.CancellationToken))
                    {
                        ReportDiagnostic(context, declarationExpression.Type);
                    }
                }

                break;
            }

            default:
            {
                Debug.Assert(forEachStatement.ContainsDiagnostics, forEachStatement.Variable.Kind().ToString());
                break;
            }
            }
        }