當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。