NullableFlowStateExtensions.cs 2.4 KB
Newer Older
1 2
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

F
Fredric Silberberg 已提交
3
using Roslyn.Utilities;
4

5
namespace Microsoft.CodeAnalysis.CSharp
6 7 8
{
    internal static class NullableFlowStateExtensions
    {
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
        public static bool MayBeNull(this NullableFlowState state) => state == NullableFlowState.MaybeNull;

        public static bool IsNotNull(this NullableFlowState state) => state == NullableFlowState.NotNull;

        /// <summary>
        /// Join nullable flow states from distinct branches during flow analysis.
        /// The result is <see cref="NullableFlowState.MaybeNull"/> if either operand is that.
        /// </summary>
        public static NullableFlowState Join(this NullableFlowState a, NullableFlowState b) => (a > b) ? a : b;

        /// <summary>
        /// Meet two nullable flow states from distinct states for the meet (union) operation in flow analysis.
        /// The result is <see cref="NullableFlowState.NotNull"/> if either operand is that.
        /// </summary>
        public static NullableFlowState Meet(this NullableFlowState a, NullableFlowState b) => (a < b) ? a : b;
24

F
Fredric Silberberg 已提交
25 26 27 28 29 30 31 32
#pragma warning disable IDE0055 // Fix formatting. This formatting is correct, need 16.1 for the updated formatter to not flag
        internal static CodeAnalysis.NullableFlowState ToPublicFlowState(this CSharp.NullableFlowState nullableFlowState) =>
            nullableFlowState switch
            {
                CSharp.NullableFlowState.NotNull => CodeAnalysis.NullableFlowState.NotNull,
                CSharp.NullableFlowState.MaybeNull => CodeAnalysis.NullableFlowState.MaybeNull,
                _ => throw ExceptionUtilities.UnexpectedValue(nullableFlowState)
            };
F
Fredric Silberberg 已提交
33

34
        // https://github.com/dotnet/roslyn/issues/35035: remove if possible
F
Fredric Silberberg 已提交
35 36 37
        public static CSharp.NullableFlowState ToInternalFlowState(this CodeAnalysis.NullableFlowState flowState) =>
            flowState switch
            {
38
                CodeAnalysis.NullableFlowState.None => CSharp.NullableFlowState.NotNull,
F
Fredric Silberberg 已提交
39 40 41 42 43
                CodeAnalysis.NullableFlowState.NotNull => CSharp.NullableFlowState.NotNull,
                CodeAnalysis.NullableFlowState.MaybeNull => CSharp.NullableFlowState.MaybeNull,
                _ => throw ExceptionUtilities.UnexpectedValue(flowState)
            };
#pragma warning restore IDE0055 // Fix formatting
44 45
    }
}