private void AnalyzeInvocationExpression(SyntaxNodeAnalysisContext context) { if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true) { return; } var invocation = (InvocationExpressionSyntax)context.Node; if (invocation.Expression?.IsKind(SyntaxKind.SimpleMemberAccessExpression) == true && invocation.ArgumentList?.Arguments.Count == 0) { var memberAccess = (MemberAccessExpressionSyntax)invocation.Expression; string methodName = memberAccess.Name?.Identifier.ValueText; switch (methodName) { case "Any": { ProcessWhere(context, invocation, memberAccess, methodName); ProcessAny(context, invocation, memberAccess); break; } case "Count": { ProcessWhere(context, invocation, memberAccess, methodName); ProcessCount(context, invocation, memberAccess); break; } case "First": case "FirstOrDefault": case "Last": case "LastOrDefault": case "LongCount": case "Single": case "SingleOrDefault": { ProcessWhere(context, invocation, memberAccess, methodName); break; } } } if (ReplaceHasFlagWithBitwiseOperationRefactoring.CanRefactor(invocation, context.SemanticModel, context.CancellationToken)) { context.ReportDiagnostic( DiagnosticDescriptors.UseBitwiseOperationInsteadOfHasFlagMethod, invocation.GetLocation()); } }
private void AnalyzeInvocationExpression(SyntaxNodeAnalysisContext context) { if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true) { return; } var invocation = (InvocationExpressionSyntax)context.Node; ExpressionSyntax expression = invocation.Expression; if (expression?.IsKind(SyntaxKind.SimpleMemberAccessExpression) == true) { var memberAccess = (MemberAccessExpressionSyntax)expression; ArgumentListSyntax argumentList = invocation.ArgumentList; if (argumentList?.IsMissing == false) { int argumentCount = argumentList.Arguments.Count; string methodName = memberAccess.Name?.Identifier.ValueText; if (argumentCount == 0) { switch (methodName) { case "Any": { SimplifyLinqMethodChainRefactoring.Analyze(context, invocation, memberAccess, methodName); ReplaceAnyMethodWithCountOrLengthPropertyRefactoring.Analyze(context, invocation, memberAccess); break; } case "Cast": { ReplaceWhereAndCastWithOfTypeRefactoring.Analyze(context, invocation); break; } case "Count": { SimplifyLinqMethodChainRefactoring.Analyze(context, invocation, memberAccess, methodName); ReplaceCountMethodRefactoring.Analyze(context, invocation, memberAccess); break; } case "First": case "FirstOrDefault": case "Last": case "LastOrDefault": case "LongCount": case "Single": case "SingleOrDefault": { SimplifyLinqMethodChainRefactoring.Analyze(context, invocation, memberAccess, methodName); break; } } } else if (argumentCount == 1) { switch (methodName) { case "Select": { ReplaceSelectWithCastRefactoring.Analyze(context, invocation, memberAccess); break; } case "Where": { CombineEnumerableWhereMethodChainRefactoring.Analyze(context, invocation, memberAccess); break; } } } } } if (ReplaceHasFlagWithBitwiseOperationRefactoring.CanRefactor(invocation, context.SemanticModel, context.CancellationToken)) { context.ReportDiagnostic( DiagnosticDescriptors.UseBitwiseOperationInsteadOfHasFlagMethod, invocation.GetLocation()); } if (RemoveRedundantToStringCallRefactoring.CanRefactor(invocation, context.SemanticModel, context.CancellationToken)) { var memberAccess = (MemberAccessExpressionSyntax)expression; TextSpan span = TextSpan.FromBounds(memberAccess.OperatorToken.Span.Start, invocation.Span.End); if (!invocation.ContainsDirectives(span)) { context.ReportDiagnostic( DiagnosticDescriptors.RemoveRedundantToStringCall, Location.Create(invocation.SyntaxTree, span)); } } if (RemoveRedundantStringToCharArrayCallRefactoring.CanRefactor(invocation, context.SemanticModel, context.CancellationToken)) { var memberAccess = (MemberAccessExpressionSyntax)invocation.Expression; TextSpan span = TextSpan.FromBounds(memberAccess.OperatorToken.Span.Start, invocation.Span.End); if (!invocation.ContainsDirectives(span)) { context.ReportDiagnostic( DiagnosticDescriptors.RemoveRedundantStringToCharArrayCall, Location.Create(invocation.SyntaxTree, span)); } } }