public static void AnalyzeAsExpression(SyntaxNodeAnalysisContext context) { var binaryExpression = (BinaryExpressionSyntax)context.Node; AsExpressionInfo info = SyntaxInfo.AsExpressionInfo(binaryExpression); if (!info.Success) { return; } ITypeSymbol typeSymbol = context.SemanticModel.GetTypeSymbol(info.Type, context.CancellationToken); if (typeSymbol?.IsErrorType() != false) { return; } Conversion conversion = context.SemanticModel.ClassifyConversion(info.Expression, typeSymbol); if (!conversion.IsIdentity) { return; } context.ReportDiagnostic( DiagnosticDescriptors.RemoveRedundantAsOperator, Location.Create(binaryExpression.SyntaxTree, TextSpan.FromBounds(binaryExpression.OperatorToken.SpanStart, info.Type.Span.End))); }
public static bool IsFixable( BinaryExpressionSyntax binaryExpression, SemanticModel semanticModel, CancellationToken cancellationToken = default(CancellationToken)) { AsExpressionInfo info = SyntaxInfo.AsExpressionInfo(binaryExpression); if (!info.Success) { return(false); } ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(info.Type, cancellationToken); if (typeSymbol == null) { return(false); } if (!semanticModel.IsExplicitConversion(info.Expression, typeSymbol)) { return(false); } return(true); }
private static Task <Document> RefactorAsync( Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken) { VariableDeclaratorSyntax variableDeclarator = localDeclaration .Declaration .Variables .Single(); ExpressionSyntax expression = variableDeclarator .Initializer .Value .WalkDownParentheses(); AsExpressionInfo asExpressionInfo = SyntaxInfo.AsExpressionInfo(expression); PrefixUnaryExpressionSyntax newCondition = LogicalNotExpression( ParenthesizedExpression( IsPatternExpression( asExpressionInfo.Expression, DeclarationPattern( asExpressionInfo.Type, SingleVariableDesignation(variableDeclarator.Identifier))))); StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(localDeclaration); int index = statementsInfo.IndexOf(localDeclaration); var ifStatement = (IfStatementSyntax)statementsInfo[index + 1]; SyntaxTriviaList leadingTrivia = statementsInfo.Parent .DescendantTrivia(TextSpan.FromBounds(localDeclaration.SpanStart, ifStatement.SpanStart)) .ToSyntaxTriviaList() .EmptyIfWhitespace(); leadingTrivia = localDeclaration.GetLeadingTrivia().AddRange(leadingTrivia); StatementSyntax newStatement = ifStatement.Statement; if (ifStatement.SingleNonBlockStatementOrDefault() is ReturnStatementSyntax returnStatement && returnStatement.Expression?.WalkDownParentheses() is IdentifierNameSyntax identifierName && string.Equals(identifierName.Identifier.ValueText, variableDeclarator.Identifier.ValueText, System.StringComparison.Ordinal)) { newStatement = newStatement.ReplaceNode(returnStatement.Expression, NullLiteralExpression().WithTriviaFrom(returnStatement.Expression)); } IfStatementSyntax newIfStatement = ifStatement .WithCondition(newCondition.WithTriviaFrom(ifStatement.Condition)) .WithStatement(newStatement) .WithLeadingTrivia(leadingTrivia) .WithFormatterAnnotation(); SyntaxList <StatementSyntax> newStatements = statementsInfo.Statements.ReplaceRange(index, 2, newIfStatement); return(document.ReplaceStatementsAsync(statementsInfo, newStatements, cancellationToken)); }
private static Task <Document> RefactorAsync( Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken) { VariableDeclaratorSyntax variableDeclarator = localDeclaration .Declaration .Variables .Single(); ExpressionSyntax expression = variableDeclarator .Initializer .Value .WalkDownParentheses(); AsExpressionInfo asExpressionInfo = SyntaxInfo.AsExpressionInfo(expression); PrefixUnaryExpressionSyntax newCondition = LogicalNotExpression( ParenthesizedExpression( IsPatternExpression( asExpressionInfo.Expression, DeclarationPattern( asExpressionInfo.Type, SingleVariableDesignation(variableDeclarator.Identifier))))); StatementListInfo statements = SyntaxInfo.StatementListInfo(localDeclaration); int index = statements.IndexOf(localDeclaration); var ifStatement = (IfStatementSyntax)statements[index + 1]; SyntaxTriviaList leadingTrivia = localDeclaration .DescendantTrivia(TextSpan.FromBounds(localDeclaration.SpanStart, ifStatement.SpanStart)) .ToSyntaxTriviaList() .EmptyIfWhitespace(); leadingTrivia = localDeclaration.GetLeadingTrivia().AddRange(leadingTrivia); IfStatementSyntax newIfStatement = ifStatement .WithCondition(newCondition.WithTriviaFrom(ifStatement.Condition)) .WithLeadingTrivia(leadingTrivia); StatementListInfo newStatements = statements.RemoveAt(index).ReplaceAt(index, newIfStatement); return(document.ReplaceStatementsAsync(statements, newStatements, cancellationToken)); }
public static Task <Document> RefactorAsync( Document document, SyntaxNode node, CancellationToken cancellationToken) { NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(node); AsExpressionInfo asExpressionInfo = SyntaxInfo.AsExpressionInfo(nullCheck.Expression); ExpressionSyntax newNode = IsExpression(asExpressionInfo.Expression, asExpressionInfo.Type); if (nullCheck.IsCheckingNull) { newNode = LogicalNotExpression(newNode.WithoutTrivia().Parenthesize()).WithTriviaFrom(newNode); } newNode = newNode .Parenthesize() .WithFormatterAnnotation(); return(document.ReplaceNodeAsync(node, newNode, cancellationToken)); }
private static void Analyze(SyntaxNodeAnalysisContext context, SyntaxNode node) { if (node.SpanContainsDirectives()) { return; } NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(node); if (!nullCheck.Success) { return; } AsExpressionInfo asExpressionInfo = SyntaxInfo.AsExpressionInfo(nullCheck.Expression); if (!asExpressionInfo.Success) { return; } DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.UseIsOperatorInsteadOfAsOperator, node); }
private static void AnalyzeAsExpression(SyntaxNodeAnalysisContext context) { var asExpression = (BinaryExpressionSyntax)context.Node; AsExpressionInfo asExpressionInfo = SyntaxInfo.AsExpressionInfo(asExpression); if (!asExpressionInfo.Success) { return; } SingleLocalDeclarationStatementInfo localInfo = SyntaxInfo.SingleLocalDeclarationStatementInfo(asExpression); if (!localInfo.Success) { return; } if (localInfo.Statement.SpanOrTrailingTriviaContainsDirectives()) { return; } if (!(localInfo.Statement.NextStatement() is IfStatementSyntax ifStatement)) { return; } if (!ifStatement.IsSimpleIf()) { return; } if (ifStatement.SpanOrLeadingTriviaContainsDirectives()) { return; } StatementSyntax statement = ifStatement.SingleNonBlockStatementOrDefault(); if (statement == null) { return; } if (!CSharpFacts.IsJumpStatement(statement.Kind())) { return; } NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(ifStatement.Condition, NullCheckStyles.EqualsToNull | NullCheckStyles.IsNull); if (!nullCheck.Success) { return; } if (!string.Equals(localInfo.IdentifierText, (nullCheck.Expression as IdentifierNameSyntax)?.Identifier.ValueText, StringComparison.Ordinal)) { return; } if (!localInfo.Type.IsVar) { SemanticModel semanticModel = context.SemanticModel; CancellationToken cancellationToken = context.CancellationToken; ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(asExpressionInfo.Type, cancellationToken); if (typeSymbol.IsNullableType()) { return; } if (!SymbolEqualityComparer.Default.Equals(semanticModel.GetTypeSymbol(localInfo.Type, cancellationToken), typeSymbol)) { return; } } DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.UsePatternMatchingInsteadOfAsAndNullCheck, localInfo.Statement); }
public static void AnalyzeAsExpression(SyntaxNodeAnalysisContext context) { var asExpression = (BinaryExpressionSyntax)context.Node; AsExpressionInfo asExpressionInfo = SyntaxInfo.AsExpressionInfo(asExpression); if (!asExpressionInfo.Success) { return; } SingleLocalDeclarationStatementInfo localInfo = SyntaxInfo.SingleLocalDeclarationStatementInfo(asExpression); if (!localInfo.Success) { return; } if (localInfo.Statement.SpanOrTrailingTriviaContainsDirectives()) { return; } if (!(localInfo.Statement.NextStatement() is IfStatementSyntax ifStatement)) { return; } if (!ifStatement.IsSimpleIf()) { return; } if (ifStatement.SpanOrLeadingTriviaContainsDirectives()) { return; } StatementSyntax statement = ifStatement.SingleNonBlockStatementOrDefault(); if (statement == null) { return; } if (!CSharpFacts.IsJumpStatement(statement.Kind())) { return; } NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(ifStatement.Condition, NullCheckStyles.EqualsToNull | NullCheckStyles.IsNull); if (!nullCheck.Success) { return; } if (!string.Equals(localInfo.IdentifierText, (nullCheck.Expression as IdentifierNameSyntax)?.Identifier.ValueText, StringComparison.Ordinal)) { return; } context.ReportDiagnostic(DiagnosticDescriptors.UsePatternMatchingInsteadOfAsAndNullCheck, localInfo.Statement); }