protected override void Initialize(SonarAnalysisContext context) { context.RegisterSyntaxNodeActionInNonGenerated( c => { var methodCall = (InvocationExpressionSyntax) c.Node; var methodParameterLookup = new MethodParameterLookup(methodCall, c.SemanticModel); var argumentMappings = methodParameterLookup.GetAllArgumentParameterMappings() .ToList(); var methodSymbol = methodParameterLookup.MethodSymbol; if (methodSymbol == null) { return; } foreach (var argumentMapping in argumentMappings.Where(argumentMapping => ArgumentHasDefaultValue(argumentMapping, c.SemanticModel))) { var argument = argumentMapping.Argument; var parameter = argumentMapping.Parameter; c.ReportDiagnostic(Diagnostic.Create(Rule, argument.GetLocation(), parameter.Name)); } }, SyntaxKind.InvocationExpression); }
protected override void Initialize(SonarAnalysisContext context) { context.RegisterSyntaxNodeActionInNonGenerated( c => { var methodCall = (InvocationExpressionSyntax)c.Node; var methodParameterLookup = new MethodParameterLookup(methodCall, c.SemanticModel); var argumentMappings = methodParameterLookup.GetAllArgumentParameterMappings() .ToList(); var methodSymbol = methodParameterLookup.MethodSymbol; if (methodSymbol == null) { return; } foreach (var argumentMapping in argumentMappings) { var parameter = argumentMapping.Parameter; var argument = argumentMapping.Argument; var callerInfoAttributeDataOnCall = GetCallerInfoAttribute(parameter); if (callerInfoAttributeDataOnCall == null) { continue; } var symbolForArgument = c.SemanticModel.GetSymbolInfo(argument.Expression).Symbol as IParameterSymbol; if (symbolForArgument != null && object.Equals(callerInfoAttributeDataOnCall.AttributeClass, GetCallerInfoAttribute(symbolForArgument)?.AttributeClass)) { continue; } c.ReportDiagnostic(Diagnostic.Create(Rule, argument.GetLocation())); } }, SyntaxKind.InvocationExpression); }
protected sealed override async Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context) { var diagnostic = context.Diagnostics.First(); var invocation = GetInvocation(root, diagnostic.Location.SourceSpan); if (invocation == null) { return; } var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken); var methodParameterLookup = new MethodParameterLookup(invocation, semanticModel); var argumentMappings = methodParameterLookup.GetAllArgumentParameterMappings() .ToList(); var methodSymbol = methodParameterLookup.MethodSymbol; if (methodSymbol == null) { return; } var argumentsWithDefaultValues = new List<ArgumentSyntax>(); var argumentsCanBeRemovedWithoutNamed = new List<ArgumentSyntax>(); var canBeRemovedWithoutNamed = true; var reversedMappings = new List<MethodParameterLookup.ArgumentParameterMapping>(argumentMappings); reversedMappings.Reverse(); foreach (var argumentMapping in reversedMappings) { var argument = argumentMapping.Argument; if (RedundantArgument.ArgumentHasDefaultValue(argumentMapping, semanticModel)) { argumentsWithDefaultValues.Add(argument); if (canBeRemovedWithoutNamed) { argumentsCanBeRemovedWithoutNamed.Add(argument); } } else { if (argument.NameColon == null) { canBeRemovedWithoutNamed = false; } } } if (argumentsCanBeRemovedWithoutNamed.Any()) { context.RegisterCodeFix( CodeAction.Create( TitleRemove, c => RemoveArgumentsAsync(context.Document, argumentsCanBeRemovedWithoutNamed, c), TitleRemove), context.Diagnostics); } var cannotBeRemoved = argumentsWithDefaultValues.Except(argumentsCanBeRemovedWithoutNamed); if (cannotBeRemoved.Any()) { context.RegisterCodeFix( CodeAction.Create( TitleRemoveWithNameAdditions, c => RemoveArgumentsAndAddNecessaryNamesAsync(context.Document, invocation.ArgumentList, argumentMappings, argumentsWithDefaultValues, semanticModel, c), TitleRemoveWithNameAdditions), context.Diagnostics); } }
protected override void Initialize(SonarAnalysisContext context) { context.RegisterSyntaxNodeActionInNonGenerated( c => { var methodCall = (InvocationExpressionSyntax) c.Node; var methodParameterLookup = new MethodParameterLookup(methodCall, c.SemanticModel); var argumentParameterMappings = methodParameterLookup.GetAllArgumentParameterMappings() .ToDictionary(pair => pair.Argument, pair => pair.Parameter); var methodSymbol = methodParameterLookup.MethodSymbol; if (methodSymbol == null) { return; } var parameterNames = argumentParameterMappings.Values .Select(symbol => symbol.Name) .Distinct() .ToList(); var identifierArguments = GetIdentifierArguments(methodCall); var identifierNames = identifierArguments .Select(p => p.IdentifierName) .ToList(); if (!parameterNames.Intersect(identifierNames).Any()) { return; } var methodCallHasIssue = false; for (var i = 0; !methodCallHasIssue && i < identifierArguments.Count; i++) { var identifierArgument = identifierArguments[i]; var identifierName = identifierArgument.IdentifierName; var parameter = argumentParameterMappings[identifierArgument.ArgumentSyntax]; var parameterName = parameter.Name; if (string.IsNullOrEmpty(identifierName) || !parameterNames.Contains(identifierName)) { continue; } var positional = identifierArgument as PositionalIdentifierArgument; if (positional != null && (parameter.IsParams || !identifierNames.Contains(parameterName) || identifierName == parameterName)) { continue; } var named = identifierArgument as NamedIdentifierArgument; if (named != null && (!identifierNames.Contains(named.DeclaredName) || named.DeclaredName == named.IdentifierName)) { continue; } methodCallHasIssue = true; } if (methodCallHasIssue) { var memberAccess = methodCall.Expression as MemberAccessExpressionSyntax; var reportLocation = memberAccess == null ? methodCall.Expression.GetLocation() : memberAccess.Name.GetLocation(); c.ReportDiagnostic(Diagnostic.Create(Rule, reportLocation, methodSymbol.Name)); } }, SyntaxKind.InvocationExpression); }