Exemple #1
0
        public static ICollection <ISymbol> GetMissingEnumMembers(ISwitchExpressionOperation operation)
        {
            var switchExpression     = operation.Value;
            var switchExpressionType = switchExpression?.Type;

            var enumMembers = new Dictionary <long, ISymbol>();

            // Check if the type of the expression is a nullable INamedTypeSymbol
            // if the type is both nullable and an INamedTypeSymbol extract the type argument from the nullable
            // and check if it is of enum type
            if (switchExpressionType != null)
            {
                switchExpressionType = switchExpressionType.IsNullable(out var underlyingType) ? underlyingType : switchExpressionType;
            }

            if (switchExpressionType?.TypeKind == TypeKind.Enum)
            {
                if (!PopulateSwitchStatementHelpers.TryGetAllEnumMembers(switchExpressionType, enumMembers) ||
                    !TryRemoveExistingEnumMembers(operation, enumMembers))
                {
                    return(SpecializedCollections.EmptyCollection <ISymbol>());
                }
            }

            return(enumMembers.Values);
        }
        public override SingleNamespaceOrTypeDeclaration VisitDelegateDeclaration(DelegateDeclarationSyntax node)
        {
            var declFlags = node.AttributeLists.Any()
                ? SingleTypeDeclaration.TypeDeclarationFlags.HasAnyAttributes
                : SingleTypeDeclaration.TypeDeclarationFlags.None;

            var diagnostics = DiagnosticBag.GetInstance();

            if (node.Arity == 0)
            {
                Symbol.ReportErrorIfHasConstraints(node.ConstraintClauses, diagnostics);
            }

            declFlags |= SingleTypeDeclaration.TypeDeclarationFlags.HasAnyNontypeMembers;

            var modifiers = node.Modifiers.ToDeclarationModifiers(diagnostics: diagnostics);

            return(new SingleTypeDeclaration(
                       kind: DeclarationKind.Delegate,
                       name: node.Identifier.ValueText,
                       modifiers: modifiers,
                       declFlags: declFlags,
                       arity: node.Arity,
                       syntaxReference: _syntaxTree.GetReference(node),
                       nameLocation: new SourceLocation(node.Identifier),
                       memberNames: SpecializedCollections.EmptyCollection <string>(),
                       children: ImmutableArray <SingleTypeDeclaration> .Empty,
                       diagnostics: diagnostics.ToReadOnlyAndFree()));
        }
        private void SyntaxNodeAction(SyntaxNodeAnalysisContext context)
        {
            var cancellationToken = context.CancellationToken;
            var semanticModel     = context.SemanticModel;
            var syntaxTree        = semanticModel.SyntaxTree;

            // "x is Type y" is only available in C# 7.0 and above.  Don't offer this refactoring
            // in projects targeting a lesser version.
            if (((CSharpParseOptions)syntaxTree.Options).LanguageVersion < LanguageVersion.CSharp7)
            {
                return;
            }

            var isExpression = (BinaryExpressionSyntax)context.Node;

            var options   = context.Options as WorkspaceAnalyzerOptions;
            var workspace = options?.Services.Workspace;

            if (workspace == null)
            {
                return;
            }

            var optionSet = options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();

            if (optionSet == null)
            {
                return;
            }

            var styleOption = optionSet.GetOption(CSharpCodeStyleOptions.PreferPatternMatchingOverIsWithCastCheck);

            if (!styleOption.Value)
            {
                // User has disabled this feature.
                return;
            }

            // See if this is an 'is' expression that would be handled by the analyzer.  If so
            // we don't need to do anything.
            if (CSharpIsAndCastCheckDiagnosticAnalyzer.Instance.TryGetPatternPieces(
                    isExpression, out _, out _, out _, out _))
            {
                return;
            }

            var(matches, _) = AnalyzeExpression(workspace, semanticModel, isExpression, cancellationToken);
            if (matches == null || matches.Count == 0)
            {
                return;
            }

            context.ReportDiagnostic(
                DiagnosticHelper.Create(
                    this.Descriptor, isExpression.GetLocation(),
                    styleOption.Notification.Severity,
                    SpecializedCollections.EmptyCollection <Location>(),
                    ImmutableDictionary <string, string> .Empty));
        }
 private static async Task <ICollection <JObject> > FindReferencesByMonikerAsync(ICodeIndexProvider codeIndexProvider, ImmutableArray <ISymbolMoniker> monikers, int pageIndex, CancellationToken cancellationToken)
 {
     try
     {
         return(await codeIndexProvider.FindReferencesByMonikerAsync(
                    monikers, includeDeclaration : false, pageIndex : pageIndex, cancellationToken : cancellationToken).ConfigureAwait(false));
     }
     catch (Exception e) when(FatalError.ReportWithoutCrashUnlessCanceled(e))
     {
         return(SpecializedCollections.EmptyCollection <JObject>());
     }
 }
        public static ICollection <T> Create <TOrig>(ImmutableArray <TOrig> collections, Func <TOrig, ICollection <T> > selector)
        {
            Debug.Assert(collections.All(c => selector(c).IsReadOnly));

            switch (collections.Length)
            {
            case 0:
                return(SpecializedCollections.EmptyCollection <T>());

            case 1:
                return(selector(collections[0]));

            default:
                return(new UnionCollection <T>(ImmutableArray.CreateRange(collections, selector)));
            }
        }
Exemple #6
0
        public static ICollection <ISymbol> GetMissingEnumMembers(ISwitchOperation switchStatement)
        {
            var switchExpression     = switchStatement.Value;
            var switchExpressionType = switchExpression?.Type;

            var enumMembers = new Dictionary <long, ISymbol>();

            if (switchExpressionType?.TypeKind == TypeKind.Enum)
            {
                if (!TryGetAllEnumMembers(switchExpressionType, enumMembers) ||
                    !TryRemoveExistingEnumMembers(switchStatement, enumMembers))
                {
                    return(SpecializedCollections.EmptyCollection <ISymbol>());
                }
            }

            return(enumMembers.Values);
        }
Exemple #7
0
        public override SingleNamespaceOrTypeDeclaration VisitDelegateDeclaration(DelegateDeclarationSyntax node)
        {
            SingleTypeDeclaration.TypeDeclarationFlags declFlags = node.AttributeLists.Any() ?
                                                                   SingleTypeDeclaration.TypeDeclarationFlags.HasAnyAttributes :
                                                                   SingleTypeDeclaration.TypeDeclarationFlags.None;

            declFlags |= SingleTypeDeclaration.TypeDeclarationFlags.HasAnyNontypeMembers;

            return(new SingleTypeDeclaration(
                       kind: DeclarationKind.Delegate,
                       name: node.Identifier.ValueText,
                       modifiers: node.Modifiers.ToDeclarationModifiers(),
                       declFlags: declFlags,
                       arity: node.Arity,
                       syntaxReference: syntaxTree.GetReference(node),
                       nameLocation: new SourceLocation(node.Identifier),
                       memberNames: SpecializedCollections.EmptyCollection <string>(),
                       children: ImmutableArray <SingleTypeDeclaration> .Empty));
        }
 protected static IEnumerable <TypeInferenceInfo> CreateResult(ITypeSymbol type)
 => type == null
         ? SpecializedCollections.EmptyCollection <TypeInferenceInfo>()
         : SpecializedCollections.SingletonEnumerable(new TypeInferenceInfo(type));