本文整理汇总了C#中BinaryExpressionSyntax.IsKind方法的典型用法代码示例。如果您正苦于以下问题:C# BinaryExpressionSyntax.IsKind方法的具体用法?C# BinaryExpressionSyntax.IsKind怎么用?C# BinaryExpressionSyntax.IsKind使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinaryExpressionSyntax
的用法示例。
在下文中一共展示了BinaryExpressionSyntax.IsKind方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddIsNaNIssue
static CodeAction AddIsNaNIssue(Document document, SemanticModel semanticModel, SyntaxNode root, BinaryExpressionSyntax node, ExpressionSyntax argExpr, string floatType)
{
return CodeActionFactory.Create(node.Span, DiagnosticSeverity.Warning, string.Format(node.IsKind(SyntaxKind.EqualsExpression) ? "Replace with '{0}.IsNaN(...)' call" : "Replace with '!{0}.IsNaN(...)' call", floatType), token =>
{
SyntaxNode newRoot;
ExpressionSyntax expr;
var arguments = new SeparatedSyntaxList<ArgumentSyntax>();
arguments = arguments.Add(SyntaxFactory.Argument(argExpr));
expr = SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.ParseExpression(floatType),
SyntaxFactory.IdentifierName("IsNaN")
),
SyntaxFactory.ArgumentList(
arguments
)
);
if (node.IsKind(SyntaxKind.NotEqualsExpression))
expr = SyntaxFactory.PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, expr);
expr = expr.WithAdditionalAnnotations(Formatter.Annotation);
newRoot = root.ReplaceNode((SyntaxNode)node, expr);
return Task.FromResult(document.WithSyntaxRoot(newRoot));
});
}
开发者ID:Kavignon,项目名称:RefactoringEssentials,代码行数:25,代码来源:CompareOfFloatsByEqualityOperatorCodeFixProvider.cs
示例2: CreateEquals
SyntaxNode CreateEquals(SemanticModel model, BinaryExpressionSyntax node)
{
var expr = SyntaxFactory.InvocationExpression(
GenerateTarget(model, node),
SyntaxFactory.ArgumentList(
new SeparatedSyntaxList<ArgumentSyntax>()
.Add(SyntaxFactory.Argument(node.Left))
.Add(SyntaxFactory.Argument(node.Right))
)
);
if (node.IsKind(SyntaxKind.NotEqualsExpression))
return SyntaxFactory.PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, expr).WithAdditionalAnnotations(Formatter.Annotation);
return expr.WithAdditionalAnnotations(Formatter.Annotation);
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:14,代码来源:ConvertEqualityOperatorToEqualsCodeRefactoringProvider.cs
示例3: TryGetNewRoot
private static bool TryGetNewRoot(SyntaxNode root, Diagnostic diagnostic, InvocationExpressionSyntax invocation, BinaryExpressionSyntax binary, out SyntaxNode newRoot)
{
newRoot = null;
if (invocation != null)
{
newRoot = ChangeInvocation(root, diagnostic, invocation);
}
else if (binary.IsKind(SyntaxKind.IsExpression))
{
newRoot = ChangeIsExpressionToNullCheck(root, binary);
}
else
{
ExpressionSyntax expression;
if (TryGetRefactoredExpression(binary, out expression))
{
newRoot = root.ReplaceNode(binary, expression.WithAdditionalAnnotations(Formatter.Annotation));
}
}
return newRoot != null;
}
示例4: GetRefactoredExpression
private static ExpressionSyntax GetRefactoredExpression(BinaryExpressionSyntax binary)
{
var typeofExpression = binary.Left as TypeOfExpressionSyntax;
var getTypeSide = binary.Right;
if (typeofExpression == null)
{
typeofExpression = (TypeOfExpressionSyntax)binary.Right;
getTypeSide = binary.Left;
}
var isOperatorCall = GetIsOperatorCall(typeofExpression, getTypeSide, true);
return binary.IsKind(SyntaxKind.EqualsExpression)
? GetExpressionWithParensIfNeeded(isOperatorCall, binary.Parent)
: SyntaxFactory.PrefixUnaryExpression(
SyntaxKind.LogicalNotExpression,
SyntaxFactory.ParenthesizedExpression(isOperatorCall));
}
示例5: TryGetRefactoredExpression
private static bool TryGetRefactoredExpression(BinaryExpressionSyntax binary, out ExpressionSyntax expression)
{
TypeOfExpressionSyntax typeofExpression;
ExpressionSyntax getTypeSide;
BinaryExpressionSyntax asExpression;
bool noNegationRequired = binary.IsKind(SyntaxKind.EqualsExpression);
ExpressionSyntax newExpression;
if (TryGetTypeOfComparison(binary, out typeofExpression, out getTypeSide))
{
newExpression = GetIsExpression(typeofExpression, getTypeSide, shouldRemoveGetType: true);
}
else if (TryGetAsOperatorComparisonToNull(binary, out asExpression))
{
newExpression = GetIsExpression(asExpression);
noNegationRequired = !noNegationRequired;
}
else
{
expression = null;
return false;
}
expression = noNegationRequired
? GetExpressionWithParensIfNeeded(newExpression, binary.Parent)
: SyntaxFactory.PrefixUnaryExpression(
SyntaxKind.LogicalNotExpression,
SyntaxFactory.ParenthesizedExpression(newExpression));
return true;
}
示例6: FindNodeToKeep
private static SyntaxNode FindNodeToKeep(BinaryExpressionSyntax binary)
{
#region logical and false, logical or true
if (binary.IsKind(SyntaxKind.LogicalAndExpression) &&
(EquivalenceChecker.AreEquivalent(binary.Left, BooleanLiteralUnnecessary.FalseExpression) ||
EquivalenceChecker.AreEquivalent(binary.Right, BooleanLiteralUnnecessary.FalseExpression)))
{
return BooleanLiteralUnnecessary.FalseExpression;
}
if (binary.IsKind(SyntaxKind.LogicalOrExpression) &&
(EquivalenceChecker.AreEquivalent(binary.Left, BooleanLiteralUnnecessary.TrueExpression) ||
EquivalenceChecker.AreEquivalent(binary.Right, BooleanLiteralUnnecessary.TrueExpression)))
{
return BooleanLiteralUnnecessary.TrueExpression;
}
#endregion
#region ==/!= both sides booleans
if (binary.IsKind(SyntaxKind.EqualsExpression) &&
TwoSidesAreDifferentBooleans(binary))
{
return BooleanLiteralUnnecessary.FalseExpression;
}
if (binary.IsKind(SyntaxKind.EqualsExpression) &&
TwoSidesAreSameBooleans(binary))
{
return BooleanLiteralUnnecessary.TrueExpression;
}
if (binary.IsKind(SyntaxKind.NotEqualsExpression) &&
TwoSidesAreSameBooleans(binary))
{
return BooleanLiteralUnnecessary.FalseExpression;
}
if (binary.IsKind(SyntaxKind.NotEqualsExpression) &&
TwoSidesAreDifferentBooleans(binary))
{
return BooleanLiteralUnnecessary.TrueExpression;
}
#endregion
if (EquivalenceChecker.AreEquivalent(binary.Left, BooleanLiteralUnnecessary.TrueExpression) ||
EquivalenceChecker.AreEquivalent(binary.Left, BooleanLiteralUnnecessary.FalseExpression))
{
return binary.Right;
}
return binary.Left;
}
示例7: CheckNotEqualsNullExpr
static bool CheckNotEqualsNullExpr(BinaryExpressionSyntax expr, ParameterSyntax param)
{
if (expr == null)
return false;
if (!expr.IsKind(SyntaxKind.NotEqualsExpression))
return false;
if (expr.Right.IsKind(SyntaxKind.NullLiteralExpression) && CompareNames(param, expr.Left as IdentifierNameSyntax))
return true;
if (expr.Left.IsKind(SyntaxKind.NullLiteralExpression) && CompareNames(param, expr.Right as IdentifierNameSyntax))
return true;
return false;
}
示例8: IsIndirectEquality
private static bool IsIndirectEquality(BinaryExpressionSyntax binaryExpression, BinaryExpressionSyntax right, BinaryExpressionSyntax left, SyntaxNodeAnalysisContext c)
{
return binaryExpression.IsKind(SyntaxKind.LogicalAndExpression) &&
HasAppropriateOperatorsForEquality(right, left) &&
HasFloatingType(right.Right, right.Left, c.SemanticModel);
}
示例9: 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;
}
示例10: TryGetNotEqualsCondition
private static bool TryGetNotEqualsCondition(IfStatementSyntax ifStatement, out BinaryExpressionSyntax condition)
{
condition = ifStatement.Condition?.RemoveParentheses() as BinaryExpressionSyntax;
return condition != null &&
condition.IsKind(SyntaxKind.NotEqualsExpression);
}
示例11: BindSimpleBinaryOperator
private BoundExpression BindSimpleBinaryOperator(BinaryExpressionSyntax node, DiagnosticBag diagnostics)
{
// The simple binary operators are left-associative, and expressions of the form
// a + b + c + d .... are relatively common in machine-generated code. The parser can handle
// creating a deep-on-the-left syntax tree no problem, and then we promptly blow the stack during
// semantic analysis. Here we build an explicit stack to handle the left-hand recursion.
Debug.Assert(IsSimpleBinaryOperator(node.Kind()));
var syntaxNodes = ArrayBuilder<BinaryExpressionSyntax>.GetInstance();
ExpressionSyntax current = node;
while (IsSimpleBinaryOperator(current.Kind()))
{
var binOp = (BinaryExpressionSyntax)current;
syntaxNodes.Push(binOp);
current = binOp.Left;
}
int compoundStringLength = 0;
BoundExpression result = BindExpression(current, diagnostics);
if (node.IsKind(SyntaxKind.SubtractExpression)
&& current.IsKind(SyntaxKind.ParenthesizedExpression))
{
if (result.Kind == BoundKind.TypeExpression
&& !((ParenthesizedExpressionSyntax)current).Expression.IsKind(SyntaxKind.ParenthesizedExpression))
{
Error(diagnostics, ErrorCode.ERR_PossibleBadNegCast, node);
}
else if (result.Kind == BoundKind.BadExpression)
{
var parenthesizedExpression = (ParenthesizedExpressionSyntax) current;
if (parenthesizedExpression.Expression.IsKind(SyntaxKind.IdentifierName)
&& ((IdentifierNameSyntax) parenthesizedExpression.Expression).Identifier.ValueText == "dynamic")
{
Error(diagnostics, ErrorCode.ERR_PossibleBadNegCast, node);
}
}
}
while (syntaxNodes.Count > 0)
{
BinaryExpressionSyntax syntaxNode = syntaxNodes.Pop();
BindValueKind bindValueKind = GetBinaryAssignmentKind(syntaxNode.Kind());
BoundExpression left = CheckValue(result, bindValueKind, diagnostics);
BoundExpression right = BindValue(syntaxNode.Right, diagnostics, BindValueKind.RValue);
BoundExpression boundOp = BindSimpleBinaryOperator(syntaxNode, diagnostics, left, right, ref compoundStringLength);
result = boundOp;
}
syntaxNodes.Free();
return result;
}
示例12: AddCompareIssue
static CodeAction AddCompareIssue(Document document, SemanticModel semanticModel, SyntaxNode root, BinaryExpressionSyntax node, string floatType)
{
return CodeActionFactory.Create(node.Span, DiagnosticSeverity.Warning, "Fix floating point number comparison", token =>
{
SyntaxNode newRoot;
ExpressionSyntax expr;
var arguments = new SeparatedSyntaxList<ArgumentSyntax>();
arguments = arguments.Add(SyntaxFactory.Argument(SyntaxFactory.BinaryExpression(SyntaxKind.SubtractExpression, node.Left, node.Right)));
expr = SyntaxFactory.BinaryExpression(
node.IsKind(SyntaxKind.EqualsExpression) ? SyntaxKind.LessThanExpression : SyntaxKind.GreaterThanExpression,
SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.ParseExpression("System.Math"),
SyntaxFactory.IdentifierName("Abs")
),
SyntaxFactory.ArgumentList(
arguments
)
),
SyntaxFactory.IdentifierName("EPSILON")
);
expr = expr.WithAdditionalAnnotations(Formatter.Annotation);
newRoot = root.ReplaceNode((SyntaxNode)node, expr);
return Task.FromResult(document.WithSyntaxRoot(newRoot));
});
}
开发者ID:Kavignon,项目名称:RefactoringEssentials,代码行数:27,代码来源:CompareOfFloatsByEqualityOperatorCodeFixProvider.cs