本文整理汇总了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()));
}
}
}
示例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;
}