当前位置: 首页>>代码示例>>C#>>正文


C# BinaryExpressionSyntax.GetLocation方法代码示例

本文整理汇总了C#中BinaryExpressionSyntax.GetLocation方法的典型用法代码示例。如果您正苦于以下问题:C# BinaryExpressionSyntax.GetLocation方法的具体用法?C# BinaryExpressionSyntax.GetLocation怎么用?C# BinaryExpressionSyntax.GetLocation使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BinaryExpressionSyntax的用法示例。


在下文中一共展示了BinaryExpressionSyntax.GetLocation方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CheckFollowingExpressions

        private static void CheckFollowingExpressions(SyntaxNodeAnalysisContext context, int currentExpressionIndex,
            List<ExpressionSyntax> expressionsInChain,
            ExpressionSyntax expressionComparedToNull, BinaryExpressionSyntax comparisonToNull)
        {
            for (var j = currentExpressionIndex + 1; j < expressionsInChain.Count; j++)
            {
                var descendantNodes = expressionsInChain[j].DescendantNodes()
                    .Where(descendant =>
                        descendant.IsKind(expressionComparedToNull.Kind()) &&
                        EquivalenceChecker.AreEquivalent(expressionComparedToNull, descendant))
                        .Where(descendant =>
                    (descendant.Parent is MemberAccessExpressionSyntax &&
                        EquivalenceChecker.AreEquivalent(expressionComparedToNull,
                            ((MemberAccessExpressionSyntax) descendant.Parent).Expression)) ||
                    (descendant.Parent is ElementAccessExpressionSyntax &&
                        EquivalenceChecker.AreEquivalent(expressionComparedToNull,
                            ((ElementAccessExpressionSyntax) descendant.Parent).Expression)))
                    .ToList();

                if (descendantNodes.Any())
                {
                    context.ReportDiagnostic(Diagnostic.Create(Rule, comparisonToNull.GetLocation(),
                        expressionComparedToNull.ToString()));
                }
            }
        }
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:26,代码来源:ShortCircuitNullPointerDereference.cs

示例2: CheckNullComparison

        static bool CheckNullComparison(SyntaxNodeAnalysisContext nodeContext, BinaryExpressionSyntax binaryOperatorExpression, ExpressionSyntax right, ExpressionSyntax nullNode, ref Diagnostic diagnostic)
        {
            if (!binaryOperatorExpression.IsKind(SyntaxKind.EqualsExpression) && !binaryOperatorExpression.IsKind(SyntaxKind.NotEqualsExpression))
                return false;
            // note null == null is checked by similiar expression comparison.
            var expr = right.SkipParens();

            var rr = nodeContext.SemanticModel.GetTypeInfo(expr);
            if (rr.Type == null)
                return false;
            var returnType = rr.Type;
            if (returnType != null && returnType.IsValueType && !returnType.IsPointerType())
            {
                // nullable check
                if (returnType.IsNullableType())
                    return false;

                var conversion = nodeContext.SemanticModel.GetConversion(nullNode);
                if (conversion.IsUserDefined)
                    return false;
                // check for user operators
                foreach (IMethodSymbol op in returnType.GetMembers().OfType<IMethodSymbol>().Where(m => m.MethodKind == MethodKind.UserDefinedOperator && m.Parameters.Length == 2))
                {
                    if (op.Parameters[0].Type.IsReferenceType == false && op.Parameters[1].Type.IsReferenceType == false)
                        continue;
                    if (binaryOperatorExpression.IsKind(SyntaxKind.EqualsExpression) && op.Name == "op_Equality")
                        return false;
                    if (binaryOperatorExpression.IsKind(SyntaxKind.NotEqualsExpression) && op.Name == "op_Inequality")
                        return false;
                }
                diagnostic = Diagnostic.Create(
                    descriptor,
                    binaryOperatorExpression.GetLocation(),
                    !binaryOperatorExpression.IsKind(SyntaxKind.EqualsExpression) ? "true" : "false"
                );
                return true;
            }
            return false;
        }
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:39,代码来源:ConditionIsAlwaysTrueOrFalseAnalyzer.cs


注:本文中的BinaryExpressionSyntax.GetLocation方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。