Exemplo n.º 1
0
        private bool TryAnalyzeAddInvocation(
            TExpressionStatementSyntax statement,
            out SyntaxNode instance)
        {
            instance = null;
            var invocationExpression = _syntaxFacts.GetExpressionOfExpressionStatement(statement) as TInvocationExpressionSyntax;

            if (invocationExpression == null)
            {
                return(false);
            }

            var arguments = _syntaxFacts.GetArgumentsOfInvocationExpression(invocationExpression);

            if (arguments.Count < 1)
            {
                return(false);
            }

            foreach (var argument in arguments)
            {
                if (!_syntaxFacts.IsSimpleArgument(argument))
                {
                    return(false);
                }
            }

            var memberAccess = _syntaxFacts.GetExpressionOfInvocationExpression(invocationExpression) as TMemberAccessExpressionSyntax;

            if (memberAccess == null)
            {
                return(false);
            }

            if (!_syntaxFacts.IsSimpleMemberAccessExpression(memberAccess))
            {
                return(false);
            }

            SyntaxNode memberName;

            _syntaxFacts.GetPartsOfMemberAccessExpression(memberAccess, out instance, out memberName);

            string name;
            int    arity;

            _syntaxFacts.GetNameAndArityOfSimpleName(memberName, out name, out arity);

            if (arity != 0 || !name.Equals(nameof(IList.Add)))
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        private void FixAllExpressionValueIsUnusedDiagnostics(
            IOrderedEnumerable <Diagnostic> diagnostics,
            SemanticModel semanticModel,
            SyntaxNode root,
            UnusedValuePreference preference,
            UniqueVariableNameGenerator nameGenerator,
            SyntaxEditor editor,
            ISyntaxFactsService syntaxFacts,
            CancellationToken cancellationToken)
        {
            // This method applies the code fix for diagnostics reported for expression statement dropping values.
            // We replace each flagged expression statement with an assignment to a discard variable or a new unused local,
            // based on the user's preference.

            foreach (var diagnostic in diagnostics)
            {
                var expressionStatement = root.FindNode(diagnostic.Location.SourceSpan).FirstAncestorOrSelf <TExpressionStatementSyntax>();
                if (expressionStatement == null)
                {
                    continue;
                }

                var expression = syntaxFacts.GetExpressionOfExpressionStatement(expressionStatement);
                switch (preference)
                {
                case UnusedValuePreference.DiscardVariable:
                    Debug.Assert(semanticModel.Language != LanguageNames.VisualBasic);
                    var discardAssignmentExpression = (TExpressionSyntax)editor.Generator.AssignmentStatement(
                        left: editor.Generator.IdentifierName(AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer.DiscardVariableName),
                        right: expression.WithoutTrivia())
                                                      .WithTriviaFrom(expression)
                                                      .WithAdditionalAnnotations(Simplifier.Annotation, Formatter.Annotation);
                    editor.ReplaceNode(expression, discardAssignmentExpression);
                    break;

                case UnusedValuePreference.UnusedLocalVariable:
                    // Add Simplifier annotation so that 'var'/explicit type is correctly added based on user options.
                    var localDecl = editor.Generator.LocalDeclarationStatement(
                        name: nameGenerator.GenerateUniqueNameAtSpanStart(expressionStatement),
                        initializer: expression.WithoutLeadingTrivia())
                                    .WithTriviaFrom(expressionStatement)
                                    .WithAdditionalAnnotations(Simplifier.Annotation, Formatter.Annotation);
                    editor.ReplaceNode(expressionStatement, localDecl);
                    break;
                }
            }
        }